Affine Transformation

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html 

 

 

Affine Transformation

In affine transformation, all parallel lines in the original image will still be parallel in the output image. To find the transformation matrix, we need three points from input image and their corresponding locations in output image. Then cv2.getAffineTransform will create a 2x3 matrix which is to be passed to cv2.warpAffine.

Check below example, and also look at the points I selected (which are marked in Green color):

img = cv2.imread('drawing.png')
rows,cols,ch = img.shape

pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])

M = cv2.getAffineTransform(pts1,pts2)

dst = cv2.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 glob
import cv2
import numpy as np
import random,sys


image_dir='./'

for pics in glob.glob(image_dir + '7700.jpg'):

     picName =  pics.split('/')[1]


     img = cv2.imread(pics)
     rows,cols,ch = img.shape

     pts1 = np.float32([[20,10],[40,10],[20,30]])
     pts2 = np.float32([[18,10],[40,10],[20,30]])
     pts3 = np.float32([[17,10],[40,10],[20,30]])
     pts4 = np.float32([[16,10],[40,10],[20,30]])
     pts5 = np.float32([[15,10],[40,10],[20,30]])
     pts6 = np.float32([[14,10],[40,10],[20,30]])

     '''
     pts2 = np.float32([[23,10],[40,10],[20,30]])
     pts3 = np.float32([[24,10],[40,10],[20,30]])
     pts4 = np.float32([[25,10],[40,10],[20,30]])
     pts5 = np.float32([[26,10],[40,10],[20,30]])
     pts6 = np.float32([[27,10],[40,10],[20,30]])
     '''

     M2 = cv2.getAffineTransform(pts1,pts2)
     M3 = cv2.getAffineTransform(pts1,pts3)
     M4 = cv2.getAffineTransform(pts1,pts4)
     M5 = cv2.getAffineTransform(pts1,pts5)
     M6 = cv2.getAffineTransform(pts1,pts6)

     dst2 = cv2.warpAffine(img,M2,(cols,rows))
     dst3 = cv2.warpAffine(img,M3,(cols,rows))
     dst4 = cv2.warpAffine(img,M4,(cols,rows))
     dst5 = cv2.warpAffine(img,M5,(cols,rows))
     dst6 = cv2.warpAffine(img,M6,(cols,rows))

     cv2.imwrite(image_dir + "2warp" + picName, dst2)
     cv2.imwrite(image_dir + "3warp" + picName, dst3)
     cv2.imwrite(image_dir + "4warp" + picName, dst4)
     cv2.imwrite(image_dir + "5warp" + picName, dst5)
     cv2.imwrite(image_dir + "6warp" + picName, dst6)

  

 

posted on 2021-03-01 00:06  cdekelon  阅读(86)  评论(0编辑  收藏  举报

导航