How to count 2d grid squares using python and opencv?
I have done that for an image.
The steps I followed: 1-detect the grid contours. 2-sort them
For now, I want to know how to do that for a real-time video.
mask2 = np.full(gray.shape, 255, np.uint8)
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(mask2, (x1, y1), (x2, y2), (0, 0, 0), 3 ,cv2.LINE_AA)
cnts = cv2.findContours(mask2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, _) = contours.sort_contours(cnts, method="top-to-bottom")
cube_rows = []
row = []
for (i, c) in enumerate(cnts, 1):
row.append(c)
if i % 3 == 0:
(cnts, _) = contours.sort_contours(row, method="left-to-right")
cube_rows.append(cnts)
row = []
number = 0
for row in cube_rows:
for c in row:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(mask2, (x, y), (x + w, y + h), (0,0,0), 2)
cv2.putText(mask2, "#{}".format(number + 1), (x,y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,0), 2)
number += 1
can you show us, what you've done so far ?
here you are, my code and thank you.
You have 2 variables
row = []
. Actually, remove second variable.Sorry, I didn’t get what you mean.