principle
The contour information that needs perspective transformation generally includes the following four categories:
The contours information obtained by using cv2.findContours() is stored counterclockwise. The easiest cases to correct are (2) and (4). The reasons are as follows (the feature is the most obvious and the easiest to determine, so you need to place the image you want to correct with the code written in it):
Picture preprocessing
Take this picture as an example.
import cv2
import numpy as np
import math
#from matplotlib import pyplot as plt
#图片preprocessing######################## ###########################################
img = cv2.imread( "D:\python_opencv\H_3.jpg")
height=int(img.shape[0])
width=int(img.shape[1])
#print(height,width)
img = cv2.resize(img, (int (width),int(height)), interpolation=cv2.INTER_CUBIC)
imgPP=img.copy()
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#ret, binary = cv2.threshold(grayImage,5,255,cv2. THRESH_BINARY)
gaussImage = cv2.GaussianBlur(grayImage, (3,3), 0)
edgedImage = cv2.Canny(gaussImage,149,149*3) #Adjustable parameters
Find contour
#找四边形的轮廓#################################################################
contours,hierarchy = cv2.findContours(edgedImage.copy(),cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours= sorted(contours, key=cv2.contourArea, reverse=True)[:1]
pentagram=contours[0]
print(pentagram)
cv2.drawContours(img,pentagram,-1,(0,0,255),3)
cv2.imshow(“img”,img)
Find the four key points
#Find the four key points of the quadrilateral######################################## #################
P1 = np.array(pentagram[:,0][pentagram[:,:,0].argmin()])
P2 = np.array( pentagram[:,0][pentagram[:,:,1].argmin()])
P3= np.array(pentagram[:,0][pentagram[:,:,1].argmax()])
P4= np.array(pentagram[:,0][pentagram[:,:,0].argmax()])
cv2.circle(imgPP,tuple(P1), 2, (0,255,0),3)
cv2.circle(imgPP,tuple(P2), 2, (255,0,255),3)
cv2.circle(imgPP,tuple(P3), 2, (0,255,0),3)
cv2.circle(imgPP,tuple(P4), 2, (255,0,255),3)
cv2.imshow(“imgPP”,imgPP)
Perspective transformation correction
# Perspective transformation############################################# ########################
Enter the four vertices of the trapezoid
srcPoints=np.vstack((P1,P2,P3,P4))
srcPoints=np .float32(srcPoints)
print(srcPoints) #The
pixel value size of the target
# Explanation of the following parameters: 96 is the resolution, which can be queried in the detailed information of the properties of the picture.
#29.7, 21 are the length and width of A4 paper (objects that need to be corrected), in cm. 2.54 is the conversion of inches
long=(29.7)*96/2.54
short=(21)*96/2.54 #Set
the size of the target canvas
canvasPoints=np.array([[0,0],[int(long),0] ,[0,int(short)],[int(long),int(short)]])
canvasPoints=np.float32(canvasPoints)
print(canvasPoints) #Calculate the
transformation matrix
perspectiveMatrix=cv2.getPerspectiveTransform(srcPoints,canvasPoints)
# Complete perspective transformation
perspectiveImg=cv2.warpPerspective(imgPP,perspectiveMatrix,(int(long)+100,int(short)+100))
cv2.imshow(“perspectiveImg”,perspectiveImg)
Precautions
- The code contains comment information, please check carefully
- The contour corrected by this program requires you to lean a little, which helps the computer find these four points quickly