So let's take this picture , How to determine the four vertex coordinates of Eileen Chang's picture and extract its picture ?
I can only get its outline , The coordinates cannot be determined
First perform edge detection :
hypothesis
CANNY_THRESH_1 = 90
CANNY_THRESH_2 = 120
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
edges = cv2.dilate(edges, None, iterations=3)
edges = cv2.erode(edges, None, iterations=1)
The edge features are obvious , The coefficients of different pictures have little difference . The current coefficient effect can use . The finer parameters are not adjusted .
In addition, some amplification and corrosion have been done , Filter out some small text areas that you don't need . Easy to filter unwanted contours .
Then extract the contour :
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mask = np.zeros(img.shape)for c in contours:
if (cv2.contourArea(c) < scale ** 2):
continue
cv2.drawContours(mask, [c], 0, (0, 0, 255))cv2.imshow('contours', mask)
About functions findContours The introduction can refer to :https://www.cnblogs.com/yiyi20120822/p/11506970.html
After the contour is extracted, the desired result can be obtained by using the smallest bounding rectangle of a single room .
Finally, extract the rectangle to obtain the vertex :
rect = cv2.minAreaRect(cnt)box = cv2.boxPoints(rect)box = np.int0(box)
You can get the vertex coordinates