opencv笔记(2):图像剪切和图像移位

栏目: 编程工具 · 发布时间: 6年前

内容简介:生活就像大海,我就像一条咸鱼,在浩瀚的海洋中边浪边学,这是opencv笔记系列中的「图像剪切」和「图像移位」。更多可关注「浪学」公众 ~世间万图,皆可剪切和移位。这一篇以很咸鱼的方式把它们记录下来。首先载入图像

生活就像大海,我就像一条咸鱼,在浩瀚的海洋中边浪边学,这是opencv笔记系列中的「图像剪切」和「图像移位」。更多可关注「浪学」公众 ~

世间万图,皆可剪切和移位。这一篇以很咸鱼的方式把它们记录下来。

首先载入图像

import cv2
import numpy as np
from matplotlib.pyplot import imshow
%matplotlib inline

img = cv2.imread('image.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
#  显示原图
imshow(img)
复制代码

原图如下:

opencv笔记(2):图像剪切和图像移位

图像剪切:图像剪切的操作比较简单, 只需要对图片的像素矩阵进行切片操作就行了。

dst = img[100:200, 100:300]
imshow(dst)
复制代码

得到剪切后的图像显示如下

opencv笔记(2):图像剪切和图像移位

图像移位:

1)第一种方法,建立偏移矩阵, 然后用矩阵映射

# 方法1
matShift = np.float32([[1,0,100],[0,1,200]])  # 偏移矩阵
dst = cv2.warpAffine(img, matShift, (height, width))  # 映射
复制代码

2)第二种方法,直接像素操作

# 方法2
dst = np.zeros(img.shape, np.uint8)
# 像素操作
for i in range(height):
    for j in range(width-100):
        dst[i, j+100] = img[i,j]
复制代码

两种结果的输出如下

opencv笔记(2):图像剪切和图像移位

今天的笔记就记录这么多了,阿浪已经泡好了咖啡,换个姿势,继续晒太阳。。。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volume 3

The Art of Computer Programming, Volume 3

Donald E. Knuth / Addison-Wesley Professional / 1998-05-04 / USD 74.99

Finally, after a wait of more than thirty-five years, the first part of Volume 4 is at last ready for publication. Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and ......一起来看看 《The Art of Computer Programming, Volume 3》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具