cv图像翻转_图像变换

缩放

f78fa64c77e05d5c85aeb78338348625.png

6787457191510a04fb40363efb67b6db.png

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()

89f4d8a92e8e6311dfe64fc019927f6d.png

平移

9f1e2e0ef3a16c2106c8c4b0c21ea865.png

7a82ba414bb4f3b86fa08fd00aa6af39.png

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()

bd13c35cd5f6d02fe06795f2db174e61.png

旋转和缩放

03f611f9eba14832d639143a1126eed6.png

88b69033983b3c5f51baab1b811c86be.png

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)

4efb5a67c82f3dc5608de78b3c487f28.png

de3163c81029d226f8e1628c6de36745.png

仿射和透视变换矩阵

ee101890b383a937f225462fc7240544.png

0778eebea050a24e600298912de67589.png

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()

8f44d3dcbb433bbcf2eea37c3120ec5b.png

ee101890b383a937f225462fc7240544.png

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()

b6349190463054869803f674d6fe67ba.png


后续
点点 在看a6af9afcbbdb678b27dc16c0f733e321.png

编写不易,感谢支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值