经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
Python3+OpenCV2实现图像的几何变换(平移、镜像、缩放、旋转、仿射)
来源:jb51  时间:2019/5/14 8:33:40  对本文有异议

前言

总结一下最近看的关于opencv图像几何变换的一些笔记.

这是原图:

1.平移

  1. import cv2
  2. import numpy as np
  3.  
  4. img = cv2.imread("image0.jpg", 1)
  5. imgInfo = img.shape
  6. height = imgInfo[0]
  7. width = imgInfo[1]
  8. mode = imgInfo[2]
  9.  
  10. dst = np.zeros(imgInfo, np.uint8)
  11.  
  12. for i in range( height ):
  13. for j in range( width - 100 ):
  14. dst[i, j + 100] = img[i, j]
  15.  
  16. cv2.imshow('image', dst)
  17. cv2.waitKey(0)

demo很简单,就是将图像向右平移了100个像素.如图:

2.镜像

  1. import cv2
  2. import numpy as np
  3.  
  4.  
  5. img = cv2.imread('image0.jpg', 1)
  6. cv2.imshow('src', img)
  7. imgInfo = img.shape
  8. height= imgInfo[0]
  9. width = imgInfo[1]
  10. deep = imgInfo[2]
  11.  
  12. dst = np.zeros([height*2, width, deep], np.uint8)
  13.  
  14. for i in range( height ):
  15. for j in range( width ):
  16. dst[i,j] = img[i,j]
  17. dst[height*2-i-1,j] = img[i,j]
  18.  
  19. for i in range(width):
  20. dst[height, i] = (0, 0, 255)
  21. cv2.imshow('image', dst)
  22. cv2.waitKey(0)

demo生成一个如下效果:

3.缩放

  1. import cv2
  2. img = cv2.imread("image0.jpg", 1)
  3. imgInfo = img.shape
  4. print( imgInfo )
  5. height = imgInfo[0]
  6. width = imgInfo[1]
  7. mode = imgInfo[2]
  8.  
  9. # 1 放大 缩小 2 等比例 非等比例
  10. dstHeight = int(height * 0.5)
  11. dstWeight = int(width * 0.5)
  12.  
  13. # 最近邻域插值 双线性插值 像素关系重采样 立方插值
  14. dst = cv2.resize(img, (dstWeight,dstHeight))
  15. print(dst.shape)
  16. cv2.imshow('image', dst)
  17. cv2.waitKey(0)

使用resize直接进行缩放操作,同时还可以使用邻域插值法进行缩放,代码如下:

  1. # 1 info 2 空白模板 3 重新计算x, y
  2. import cv2
  3. import numpy as np
  4. img = cv2.imread('image0.jpg', 1)
  5. imgInfo = img.shape # 先高度,后宽度
  6. height = imgInfo[0]
  7. width = imgInfo[1]
  8. dstHeight = int(height/2)
  9. dstWidth = int(width/2)
  10.  
  11. dstImage = np.zeros([dstHeight, dstWidth, 3], np.uint8)
  12. for i in range( dstHeight ):
  13. for j in range(dstWidth):
  14. iNew = i * ( height * 1.0 / dstHeight )
  15. jNew = j * ( width * 1.0 / dstWidth )
  16.  
  17. dstImage[i,j] = img[int(iNew),int(jNew)]
  18.  
  19. cv2.imshow('image', dstImage)
  20. cv2.waitKey(0)

4.旋转

  1. import cv2
  2.  
  3. img = cv2.imread('image0.jpg', 1)
  4. cv2.imshow('src', img)
  5. imgInfo = img.shape
  6. height= imgInfo[0]
  7. width = imgInfo[1]
  8. deep = imgInfo[2]
  9.  
  10. # 定义一个旋转矩阵
  11. matRotate = cv2.getRotationMatrix2D((height*0.5, width*0.5), 45, 0.7) # mat rotate 1 center 2 angle 3 缩放系数
  12.  
  13. dst = cv2.warpAffine(img, matRotate, (height, width))
  14.  
  15. cv2.imshow('image',dst)
  16. cv2.waitKey(0)

旋转需要先定义一个旋转矩阵,cv2.getRotationMatrix2D(),参数1:需要旋转的中心点.参数2:需要旋转的角度.参数三:需要缩放的比例.效果如下图:

5.仿射

  1. import cv2
  2. import numpy as np
  3.  
  4. img = cv2.imread('image0.jpg', 1)
  5. cv2.imshow('src', img)
  6. imgInfo = img.shape
  7. height= imgInfo[0]
  8. width = imgInfo[1]
  9. deep = imgInfo[2]
  10. # src 3 -> dst 3 (左上角, 左下角,右上角)
  11. matSrc = np.float32([[0,0],[0,height-1],[width-1, 0]]) # 需要注意的是 行列 和 坐标 是不一致的
  12. matDst = np.float32([[50,50],[100, height-50],[width-200,100]])
  13.  
  14. matAffine = cv2.getAffineTransform(matSrc,matDst) #mat 1 src 2 dst 形成组合矩阵
  15. dst = cv2.warpAffine(img, matAffine,(height, width))
  16. cv2.imshow('image',dst)
  17. cv2.waitKey(0)

需要确定图像矩阵的三个点坐标,及(左上角, 左下角,右上角).定义两个矩阵,matSrc 为原图的三个点坐标,matDst为进行仿射的三个点坐标,通过cv2.getAffineTransform()形成组合矩阵.效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号