缩放
import cv2from matplotlib import pyplot as pltimage = cv2.imread("1.jpg")#转换颜色通道b, g, r = cv2.split(image)image = cv2.merge([r, g, b])#图像水平翻转flipped1 = cv2.flip(image,1)#图像垂直翻转flipped2 = cv2.flip(image,0)#图像水平垂直翻转flipped3 = cv2.flip(image,-1)#显示图像plt.subplot(141),plt.imshow(image),plt.title('Input')plt.xticks([]),plt.yticks([])plt.subplot(142),plt.imshow(flipped1),plt.title('Output1')plt.xticks([]),plt.yticks([])plt.subplot(143),plt.imshow(flipped2),plt.title('Output2')plt.xticks([]),plt.yticks([])plt.subplot(144),plt.imshow(flipped3),plt.title('Output3')plt.xticks([]),plt.yticks([])plt.show()
平移
import cv2import numpy as npimg = cv2.imread('1.jpg')M = np.float32([[1, 0, 30], [0, 1, 50]])res = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))while 1: # 显示图像 cv2.imshow('img', img) cv2.imshow('res', res) k = cv2.waitKey(5) & 0xFF if k == 27: break# 关闭窗口cv2.destroyAllWindows()
旋转和缩放
import cv2image = cv2.imread("1.jpg")cv2.imshow("Original",image)cv2.waitKey(0)(h,w) = image.shape[:2]center = (w / 2,h / 2)#旋转45度,缩放0.75M = cv2.getRotationMatrix2D(center,45,0.75)rotated = cv2.warpAffine(image,M,(w,h))cv2.imshow("Rotated by 45 Degrees",rotated)cv2.waitKey(0)#旋转-45度。缩放1.25M = cv2.getRotationMatrix2D(center,-45,1.25)rotated = cv2.warpAffine(image,M,(w,h))cv2.imshow("Rotated by -90 Degrees",rotated)cv2.waitKey(0)
仿射和透视变换矩阵
import cv2 as cvfrom matplotlib import pyplot as pltimport numpy as npimg = cv.imread('1.jpg')rows,cols,ch = img.shape#指定对应变换点pts1 = np.float32([[50,50],[200,50],[50,200]])pts2 = np.float32([[10,100],[200,50],[100,250]])#得到仿射矩阵M = cv.getAffineTransform(pts1,pts2)#实现仿射变换dst = cv.warpAffine(img,M,(cols,rows))#输出显示原图和变换图像plt.subplot(121),plt.imshow(img),plt.title('Input')plt.subplot(122),plt.imshow(dst),plt.title('Output')plt.show()
import cv2 as cvimport numpy as npimage=cv.imread('1.jpg')# 得到图片的高和宽height,width=image.shape[:2]# 定义对应的点points1 = np.float32([[75,55], [340,55], [33,435], [400,433]])points2 = np.float32([[0,0], [360,0], [0,420], [360,420]])# 计算得到转换矩阵M = cv.getPerspectiveTransform(points1, points2)# 实现透视变换转换processed = cv.warpPerspective(image,M,(360, 420))cv.imshow('original',image)cv.imshow('perspective', processed)cv.waitKey(0)cv.destroyAllWindows()
后续
点点 在看
编写不易,感谢支持。