I'm currently facing an issue. I want to detect the dashed cross lines in this image, but my detection is always affected by other factors. Any advice would be appreciated.
Original Image:
I've tried the following code.
import cv2
import numpy as np
image = cv2.imread(r'D:\\photo\\11.jpg')
height,width,_=image.shape
Gauss = cv2.GaussianBlur(image, (3, 3), 0)
gray = cv2.cvtColor(Gauss, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
cv2.imshow('edges',edges)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 130)
cnt=0
if lines is not None:
for line in lines:
rho, theta = line[0]
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(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
cnt=cnt+1
cv2.imshow('Detected Lines', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Problem: It always detects the gray and white edge lines. Additionally, it only detects one of the dashed cross lines.
Heres the Output:
Here, see what you think of this. I don't think your Gaussian blur helps you at all here, so I've removed it.
I invert the grayscale and rescale it so what was white becomes nearly black. I then do a convolution about the size of the cross in the middle (41x41) that strongly emphasizes only cross shapes. I then pass that convolved image through the Hough lines detection, and it does a pretty good job.
import cv2
import numpy as np
image = cv2.imread('crosstab.png')
height,width,_=image.shape
Gauss = cv2.GaussianBlur(image, (3, 3), 0)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = gray.max()-gray
gray[:,0:260] = 0
kern = np.ones((41,41),dtype=int)*-1
kern[19:21,:] = 20
kern[:,19:21] = 20
kern = kern / kern.sum()
gray = cv2.filter2D(gray, ddepth=-1, kernel=kern )
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
cv2.imshow('edges',edges)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 130)
cnt=0
if lines is not None:
for line in lines:
rho, theta = line[0]
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(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
cnt=cnt+1
cv2.imshow('Detected Lines', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output: