opencv-python 透视变换小例

opencv 透视变换小例

用透视变换,将kindle中的文字框重新放正


import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

count=0
def draw_circle(event,x,y,flags,param):
    global  count
    if event==cv.EVENT_LBUTTONDBLCLK:
        print("x: ",x,'y: ',y)
        pts1[count]=[x,y]
        count+=1
        cv.circle(img,(x,y),5,(255,0,0),-1)


img=cv.imread('tst3.jpg',cv.IMREAD_COLOR)
rows,cols,ch=img.shape

pts1 = np.float32([[563,503],[698,888],[305,819],[541,1195]])
pts2 = np.float32([[0,0],[400,0],[0,300],[400,300]])

M=cv.getPerspectiveTransform(pts1,pts2)
dst=cv.warpPerspective(img,M,(300,300))

cv.namedWindow('xixi')
cv.setMouseCallback('xixi',draw_circle)
while(cv.waitKey(20)):
     cv.imshow('xixi',img)
     if cv.waitKey(20)&0xff==ord('q'):
         break

cv.destroyAllWindows('xixi')


plt.subplot(1,2,1,title='Input')
plt.imshow(img)
plt.subplot(1,2,2,title='Output')
plt.imshow(dst)

plt.show()

最后效果:
opencv-python 透视变换小例_第1张图片

你可能感兴趣的:(数字图像处理)