本次小节学习了图像的变换,主要应用到如下方法: cv2.resize(), cv2.warpAffine(), cv2.getRotationMatrix2D(), cv2.getAffineTransform(), cv2.getPerspectiveTransform(), cv2.warpPerspective().

#scale

img = cv2.imread('woman.jpg')
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows() '''
cv2.resize(inputArray src, OutputArray dst, Size, fx, fy, interpolation)
InputArray src: 输入图片
OutputArray dst: 输出图片
Size: 输出图片尺寸
fx, fy: 沿x轴,y轴的缩放系数
interpolation: 插入方式
interpolation 的值为:
INTER_NEAREST: 最近邻插值
INTER_LINEAR: 双线性插值
INTER_AREA: 使用像素区域关系进行重采样
INTER_CUBIC: 4*4像素邻域的双三次插值
INTER_LANCZOS4: 8*8像素邻域的Lanczos插值
'''
res = cv2.resize(img, None, fx=2,fy=2,interpolation=cv2.INTER_CUBIC) cv2.imshow('dst',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
#OR
height, width=img.shape[:2] res = cv2.resize(img, (2*width,2*height),interpolation=cv2.INTER_CUBIC) cv2.imshow('dst1',res)
cv2.waitKey(0)
cv2.destroyAllWindows() #Tranlation or Rotation
img = cv2.imread('ball.png',0)
rows, cols = img.shape
M = np.float32([[1,0,100],[0,1,50]])
'''
cv2.getRotationMatrix2D(Point2f center, double angle, double scale):图像旋转
Point2f center: 表示旋转的中心点
double angle: 表示旋转的角度
double scale:图像缩放的因子
'''
#M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
'''
cv2.warpAffine(src, M, dsize[,dst[,flags[,borderModel[, borderValue]]]]
src: 输入图像
M: 变换矩阵[仿射变化矩阵,一般反应平移或旋转的关系,为2*3变换矩阵]
dsize: 输出图像的大小
flags: 插值方法的组合[值与上述的interpolation一样]
borderModel: 边界像素模式
borderValue: 边界填充值。默认0
'''
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
#透视
img = cv2.imread('man.jpg')
rows, cols, ch = img.shape pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
'''
getPerspectiveTransform(const CvPoint2D32f *src, const CvPoint2D32f *dst, CvMat *map_matrix)[由四对点计算透视变换]
src: 输入图像的四边形顶点坐标
dst: 输出图像的相应的四边形顶点坐标
map_matrix: 指向3*3输出矩阵的指针
'''
M = cv2.getPerspectiveTransform(pts1,pts2)
'''
warpPerspective(const CvArr* src, CvArr* dst, const CvMat* map_matrix, int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, CvScalar fillval=cvScalarAll(0)):对图像进行透视变换
src: 输入图像
dst: 输出图像
map_matrix: 3*3变换矩阵
flags:插值方法[CV_WARP_FILL_OUTLIERS:填充所有缩小图像的像素。
CV_WARP_INVERSE_MAP:指定matrix是输出图像到输入图像的反变换。
fillval用于填充边界外的值]
'''
dst = cv2.warpPerspective(img,M,(300,300)) plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
												

最新文章

  1. C#开发微信公众平台-就这么简单(附Demo)
  2. [Q&A] 类Range的PasteSpecial方法无效
  3. asp.net mvc HandleErrorAttribute 异常错误处理 无效!
  4. css-@keyframes动画
  5. MySQL ALTER语法的运用方法 && 操作索引和字段
  6. BestCoder Round #68 (div.2) tree(hdu 5606)
  7. bzoj 4004: [JLOI2015]装备购买 拟阵 && 高消
  8. Primefaces的fileUpload组件使用
  9. 52、css属性操作
  10. Pandas系列之入门篇——HDF5
  11. CSS属性操作/下
  12. Java永久代去哪儿了
  13. 使用CSS3的clip-path(裁剪路径)实现剪贴区域的显示以及实例实现图片渐变
  14. 0412ooday01.txt=============对象和类(上)
  15. linux中.nfsxxxx引起的文件无法删除
  16. face detection[Multi-view face detection&& MTCNN]
  17. centos 6.5 安装jdk1.8
  18. CSS: Grid homework redact.
  19. python mysql program
  20. windows Server2012 IIS8.0配置安装完整教程

热门文章

  1. 【NOI2019模拟2019.7.1】为了部落 (生成森林计数,动态规划)
  2. NX二次开发-NXOPEN自动切换到工程图模块
  3. [JZOJ 5791] 阶乘
  4. 2018-2019-2-20175323 java实验三敏捷开发与XP实践
  5. Centos搭建http代理服务器(无密码验证)
  6. 剑指offer——03替换空格
  7. selenium+python 绕过登录进行测试
  8. jquery控件的学习
  9. python学习4—数据结构之列表、元组与字典
  10. 哈理工赛 H-小乐乐学数学 /// 筛法得素数表+树状数组