I’m currently working with a neuroscience lab and need help coding a behavioral test. Where I’m stuck at is struggling with grid detection. I’m a bit of a newbie when it comes to CV so guidance would be appreciated. Here’s an example of the image I’m working with:
Base image
I want to detect the lines in the grid here and have tried various approaches (Hough transform + Canny edge detection, Sobel edge detection, even color-based detection). When I try Hough transform + Canny, here’s the result I get:
Result
Basically not very accurate lol. It also classifies some of the silver lining incorrectly and is pretty poor when it comes to trying to detect some of the gridlines that are in the shadows near the top and sides of the image. Here is my code for how I’m implementing it right now:
import cv2
import numpy as np
img = cv2.imread('grid_image.png')
gray_img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_img, 50, 150)
lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi/180, threshold=100, minLineLength=100, maxLineGap=10)
line_img = np.zeros_like(img)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(line_img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# combine images
result = cv2.addWeighted(img, 0.8, line_img, 1, 0)
# Display the result
cv2.imshow('Grid Detection', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
I’m a little bit lost and would like some high-level guidance on what I can do to improve/change my approach. Please let me know, help would be appreciated.
codemaster0102 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.