该案例基于opencv4.x版本编写
代码地址:github.com/gudepeng/st…
1.图像翻转
img = cv2.imread("./img/opencv.jpg", cv2.IMREAD_COLOR)
# 图片翻转
plt.subplot(2, 2, 1)
plt.imshow(img)
plt.title("first")
img1 = cv2.flip(img, 0)
plt.subplot(2, 2, 2)
plt.imshow(img1)
plt.title("x-flip")
img2 = cv2.flip(img, 1)
plt.subplot(2, 2, 3)
plt.imshow(img2)
plt.title("y-flip")
img3 = cv2.flip(img, -1)
plt.subplot(2, 2, 4)
plt.imshow(img3)
plt.title("xy-flip")
plt.show()
复制代码
flip(src, flipCode, dst=None)
- src:要反转的图像
- flipCode:翻转方式
- >0: 沿y-轴翻转
- 0: 沿x-轴翻转
- <0: x、y轴同时翻转
图像缩放
# 图像缩放1
img = cv2.imread("./img/opencv.jpg", cv2.IMREAD_COLOR)
(h, w, c) = img.shape
dstHeight = int(h * 0.5)
dstWidth = int(w * 0.5)
dst = cv2.resize(img, (dstWidth, dstHeight))
cv2.imshow('img1', dst)
cv2.waitKey(0)
# 图像缩放2
img = cv2.imread("./img/opencv.jpg", cv2.IMREAD_COLOR)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dstHeight = int(height / 2)
dstWidth = int(width / 2)
dstImage = np.zeros((dstHeight, dstWidth, 3), np.uint8)
for i in range(0, dstHeight):
for j in range(0, dstWidth):
iNew = int(i * (height * 1.0 / dstHeight))
jNew = int(j * (width * 1.0 / dstWidth))
dstImage[i, j] = img[iNew, jNew]
cv2.imshow('img2', dstImage)
cv2.waitKey(0)
# 图像缩放3
img = cv2.imread("./img/opencv.jpg", cv2.IMREAD_COLOR)
(h, w, c) = img.shape
matScale = np.float32([[0.5, 0, 0], [0, 0.5, 0]])
dst = cv2.warpAffine(img, matScale, (int(w / 2), int(h / 2)))
cv2.imshow('dst', dst)
cv2.waitKey(0)
复制代码
- 图像缩放1:resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None)
- src:要缩放的图像
- dsiz:要缩放成的大小
- fx:沿水平轴的比例因子
- fy:沿垂直轴的比例因子
- interpolation:插值方法
- 图片缩放2:新建一张想要的图像大小的图像,然后按比例计算像素点
- 图片缩放3:仿射变换,在下面会详细讲解
图像剪切
img = cv2.imread("./img/opencv.jpg", cv2.IMREAD_COLOR)
print(img.shape)
dst = img[250:350, 10:350]
cv2.imshow('image', dst)
cv2.waitKey(0)
复制代码
- 直接对图像矩阵做剪切即可
图像移位
# 图像移位1
img = cv2.imread("./img/opencv.jpg", cv2.IMREAD_COLOR)
(h, w, c) = img.shape
matShift = np.float32([[1, 0, 100], [0, 1, 200]])
dst = cv2.warpAffine(img, matShift, (h, w))
cv2.imshow('dst', dst)
cv2.waitKey(0)
# 图像移位2
img = cv2.imread("./img/opencv.jpg", cv2.IMREAD_COLOR)
(h,w,c) = img.shape
![](https://user-gold-cdn.xitu.io/2019/6/19/16b700993de0787b?w=718&h=702&f=jpeg&s=35870)
dst = np.zeros(img.shape,np.uint8)
for i in range(0,h):
for j in range(0,w-100):
dst[i,j+100]=img[i,j]
cv2.imshow('image',dst)
cv2.waitKey(0)
复制代码
- 图像移位1:仿射变换,在下面会详细讲解
- 图像移位2:新建一个想要得到图像大小的图像,直接对图像矩阵像素操作
仿射变换
img = cv2.imread("./img/opencv.jpg", cv2.IMREAD_COLOR)
(h, w, c) = img.shape
matSrc = np.float32([[0, 0], [0, h - 1], [h - 1, 0]])
matDst = np.float32([[50, 50], [300, h - 200], [h - 300, 100]])
matAffine = cv2.getAffineTransform(matSrc, matDst)
dst = cv2.warpAffine(img, matAffine, (w, h))
cv2.imshow('dst', dst)
cv2.waitKey(0)
复制代码
warpAffine(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None)
- src:要转化的图像
- M:变换矩阵
- dsize:输出图像的大小
- flags:插值方法的组合(int)
- borderMode:边界像素模式(int)
- borderValue:边界填充值,默认为0
getAffineTransform(src, dst)
- src:输入图像的三角形顶点坐标。
- dst:输出图像的相应的三角形顶点坐标。