I'm trying to place a grid on top of the following image which was captured through an optical lens system.
I have the follow script which I've written in python using opencv after some googling (i'm a newbie)
import cv2
GRID_SIZE = 24
img = cv2.imread('input.jpg')
height, width, channel = img.shape
for y in range(40, height - GRID_SIZE + 1, GRID_SIZE):
for x in range(40, width - GRID_SIZE + 1, GRID_SIZE):
cv2.rectangle(img, pt1=(x,y), pt2=(x+GRID_SIZE,y+GRID_SIZE), color=(255, 0, 255), thickness=1)
cv2.imshow('Output', img)
key = cv2.waitKey(0)
The resulting image looks like:
As you can see i'm not able to place the grid perfectly due to camera distortion. I did come across something called camera caliberation but i do not have any information related to focal length or optical centres. Would it be possible to align the grid lines without this information? I had manually calculated each square is 24 pixels which is why I've set the GRID_SIZE value to 24. Would also be interested to know if there are ways to calculate this automatically and place a grid on top.
Expected result:
I have manually added the gridlines for few rows and columns. Later I compute whether the square is a GREEN or BLACK region and I mark them as 1 & 0 respectively. Resulting in a matrix consisting of 1s & 0s.


