opencv学习——仿射变换
2017-10-26 16:43
211 查看
import cv2 import numpy as np import matplotlib.pylab as plt import scipy.misc as misc img = cv2.imread('images/33.jpg') rows, cols = img.shape[:2] def weighted(): # the two images must be same size img1 = cv2.imread('images/31.jpg') img2 = cv2.imread('images/32.jpg') dst = cv2.addWeighted(img1,0.3,img2,0.7,0) return dst def scaling(img): ''' cv2.INTER_AREA for shrinking and cv2.INTER_CUBIC (slow) & cv2.INTER_LINEAR for zooming. By default, interpolation method used is cv2.INTER_LINEAR for all resizing purposes. ''' # res = cv2.resize(img,(2*h,2*w),interpolation=cv2.INTER_CUBIC) res = cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC) return res def translation(img): # moving (100,50), # Remember width = number of columns, and height = number of rows M = np.float32([[1,0,100],[0,1,50]]) dst = cv2.warpAffine(img,M,(rows,cols)) return dst def rotation(img): # center,angle,scaling, 长方形旋转后会有损失 # M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1) # dst = cv2.warpAffine(img,M,(cols,rows)) # 这种方法针对长方形图像,旋转之后不会有遮挡 M = np.float32([[0,1,0],[-1,0,cols]]) dst = cv2.warpAffine(img,M,(rows,cols)) return dst def affine(img): pts1 = np.float32([[50,50],[200,50],[50,200]]) pts2 = np.float32([[10,100],[200,50],[100,250]]) M = cv2.getAffineTransform(pts1,pts2) #2*2 dst = cv2.warpAffine(img,M,(cols,rows)) return dst def perspective(img): pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]]) pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]]) M = cv2.getPerspectiveTransform(pts1,pts2) #3*3 dst = cv2.warpPerspective(img,M,(cols,rows)) return dst,M if __name__ == '__main__': dst,M = perspective(img) cv2.imshow('result.jpg', dst) cv2.waitKey(0) cv2.destroyAllWindows() cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1)
相关文章推荐
- opencv学习(三十五)之仿射变换warpAffine
- OpenCV Python 学习笔记(三) 仿射变换
- Opencv学习——仿射变换和透视变换
- 学习OpenCV范例(十六)——重映射和仿射变换
- OpenCV学习六:findContours、drawContours、仿射变换、RotatedRect区域矫正及获取
- Opencv学习之仿射变换、直方图均衡化
- opencv document 仿射变换学习
- opencv学习(6)---仿射变换
- 【OpenCV学习笔记】7.4仿射变换warpAffine()
- OpenCV入门学习(二)Affine Transformations(仿射变换)
- 学习OpenCV范例(十六)——重映射和仿射变换
- OpenCV学习--仿射变换(affine)
- opencv学习笔记:由三对点计算仿射变换
- OpenCV学习记录3//仿射变换与重映射
- opencv学习-imgprocess-仿射变换warpAffine
- OpenCV学习26--仿射变换
- 【OpenCV学习】错误处理机制
- 学习opencv----绘图
- 深度学习2--安装opencv2.4(材料都在百度云上)
- 学习OpenCV第四章第一题答案