生活就像大海,我就像一条咸鱼,在浩瀚的海洋中边浪边学,这是opencv笔记系列中的「图像剪切」和「图像移位」。更多可关注「浪学」公众 ~bash
世间万图,皆可剪切和移位。这一篇以很咸鱼的方式把它们记录下来。ui
首先载入图像spa
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)
复制代码
原图以下:3d
图像剪切:图像剪切的操做比较简单, 只须要对图片的像素矩阵进行切片操做就好了。code
dst = img[100:200, 100:300]
imshow(dst)
复制代码
获得剪切后的图像显示以下cdn
图像移位:blog
1)第一种方法,创建偏移矩阵, 而后用矩阵映射图片
# 方法1
matShift = np.float32([[1,0,100],[0,1,200]]) # 偏移矩阵
dst = cv2.warpAffine(img, matShift, (height, width)) # 映射
复制代码
2)第二种方法,直接像素操做string
# 方法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]
复制代码
两种结果的输出以下it
今天的笔记就记录这么多了,阿浪已经泡好了咖啡,换个姿式,继续晒太阳。。。