I am trying to detect the intersection of grid points on an image. The image is a photograph and contains black lines, but has some random colours in the background:
My goal is to precisely detect the centre point intersection between two lines:
I can't use a line detection because eventually the lines will be deformed (curvy).
My current process involves converting the image to gray, thresholding, and detecting the corners. The next step is to determine the centre of 4 detected corners.
import cv2
import numpy as np
img = cv2.imread('blackgrid.jpg',1)
# convert image to gray
im_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#threshold gray image to b and w
ret,thresh2 = cv2.threshold(im_gray,120,255,cv2.THRESH_BINARY_INV)
# dilate and erode image
kernel = np.ones((5,5), np.uint8)
img_dilation = cv2.dilate(thresh2, kernel, iterations=2)
kernel = np.ones((10,10), np.uint8)
img_erosion = cv2.erode(img_dilation, kernel, iterations=1)
# detect corners
gray = np.float32(img_erosion)
dst = cv2.cornerHarris(gray,5,19,0.07)
dst = cv2.dilate(dst,None,iterations=2)
img[dst>0.01*dst.max()]=[0,0,255]
cv2.imwrite('dst.png', dst)
cv2.imwrite('img2.png', img)
and returns the following result:
I am currently struggling with removing the coloured areas (pink and green lines) so that the corners around these lines are not detected.
Also, if anyone could suggest or point me in the right direction on an efficient way to use the corner detected points to determine the centre of the intersection, it would be greatly appreciated.