目标效果:
代碼:
import cv2
import numpy as np
img=cv2.imread("img/cards.jpg")
width,height=250,350 #顯示卡片的寬和高
pts1=np.float32([[245,75],[380,80],[215,230],[365,230]]) #截取對片中的哪個區域
pts2=np.float32([[0,0],[width,0],[0,height],[width,height]]) #定義顯示的卡片的坐标
matrix=cv2.getPerspectiveTransform(pts1,pts2) #兩個區域坐标綁定
imgOutput=cv2.warpPerspective(img,matrix,(width,height)) #轉換為圖檔
cv2.imshow("card",imgOutput)
cv2.imshow("image",img)
cv2.waitKey(0) #延時顯示,0代表無限延時
代碼比較短,但是在确定坐标的時候可能比較麻煩,可以先用裁剪的方式擷取大緻坐标,再進行微調。
imgcrop=img[75:245,215:380] #裁剪圖檔