Home>

### python - i want to take 4 edges from the edge image

I would like to take the four points at the end of the black line on the shogi board.
Original image (Fig. 1)Grayscale and canny edge from to output the image and it seems to work (Fig. 2)However, even if I do a Hough transform after that, the line is not drawn just right. How can I get 4 points at the end? If i have any policy, please let me know. Any answer will be helpful, so thank you.

``````import cv2
import numpy as np
img = cv2.imread ('ba-n.jpg')
kernel = np.ones ((2, 2), np.uint8)
gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny (gray, 1, 100, apertureSize = 3)
cv2.imwrite ('houghlines6_edge.jpg', edges)
lines = cv2.HoughLines (edges, 1, np.pi/180, 200)
for line in lines:
rho, theta = line 
a = np.cos (theta)
b = np.sin (theta)
x0 = a * rho
y0 = b * rho
x1 = int (x0 + 1000 * (-b))
y1 = int (y0 + 1000 * (a))
x2 = int (x0 --1000 * (-b))
y2 = int (y0 --1000 * (a))
cv2.line (img, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imwrite ('houghlines3.jpg', img)``````
• Answer # 1

Isn't it easier to detect the line by Hough transform if the line is thickened by the expansion process (deliate)?

If the Hough transform doesn't work,
(1) After expansion processing, find contour is detected by findContours.
(2) Probably the quadrangle you want to detect has the largest area, so calculate the area with the contourArea and detect only the largest one.
③ Detect the coordinates of the four corners with the idea below
・ Lower left: Extract the point with the smallest x coordinate
・ Lower right: Extract the point with the largest x coordinate
・ Upper left: In order to extract the point closest to the upper left, extract the largest point on the coordinate axis rotated by 135 °.
・ Upper right: In order to extract the point closest to the upper right, extract the largest point on the coordinate axis rotated by 45 °.

The link below also detects the points at the four corners of the tennis court.
[Sports image analysis 4 using OpenCV (motion tracking)] Automatic detection of tennis court lines

• Answer # 2

I think it is a standard method as a rough direction.
There are a lot of small noises caused by shogi frames. Is this one of the reasons why the Hough transform does not work?
Why not try removing these noises manually and applying a Hough transform? If it works, you should consider denoising.

Alternatively, another approach might be to detect only the outermost line (scan from the outside).

• Answer # 3

In this image, the reflection around the lower right is severe, but do you generally assume a situation with such a disturbance?
If so, I would consider detection processing in the form of a "9x9 grid".