Perspective correction in OpenCV using python

后端 未结 3 1842
独厮守ぢ
独厮守ぢ 2020-12-07 16:14

I am trying to do a perspective correction of a tilted rectangle ( a credit card), which is tilted in all the 4 directions. I could find its four corners and the respective

3条回答
  •  盖世英雄少女心
    2020-12-07 16:33

    I'm writing the answer provided by @Haris in python.

    import cv2
    import math
    import numpy as np
    import matplotlib.pyplot as plt
    
    img = cv2.imread('test.jpg')
    rows,cols,ch = img.shape
    
    pts1 = np.float32([[360,50],[2122,470],[2264, 1616],[328,1820]])
    
    ratio=1.6
    cardH=math.sqrt((pts1[2][0]-pts1[1][0])*(pts1[2][0]-pts1[1][0])+(pts1[2][1]-pts1[1][1])*(pts1[2][1]-pts1[1][1]))
    cardW=ratio*cardH;
    pts2 = np.float32([[pts1[0][0],pts1[0][1]], [pts1[0][0]+cardW, pts1[0][1]], [pts1[0][0]+cardW, pts1[0][1]+cardH], [pts1[0][0], pts1[0][1]+cardH]])
    
    M = cv2.getPerspectiveTransform(pts1,pts2)
    
    offsetSize=500
    transformed = np.zeros((int(cardW+offsetSize), int(cardH+offsetSize)), dtype=np.uint8);
    dst = cv2.warpPerspective(img, M, transformed.shape)
    
    plt.subplot(121),plt.imshow(img),plt.title('Input')
    plt.subplot(122),plt.imshow(dst),plt.title('Output')
    plt.show()
    
    

提交回复
热议问题