How to count 2d grid squares using python and opencv?

asked 2019-12-30 20:16:16 -0600

fracv gravatar image

updated 2019-12-31 03:45:01 -0600

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.

like this:

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
edit retag flag offensive close merge delete

Comments

can you show us, what you've done so far ?

berak gravatar imageberak ( 2019-12-31 03:25:11 -0600 )edit
1

here you are, my code and thank you.

fracv gravatar imagefracv ( 2019-12-31 03:45:32 -0600 )edit

You have 2 variables row = []. Actually, remove second variable.

supra56 gravatar imagesupra56 ( 2019-12-31 03:53:30 -0600 )edit

Sorry, I didn’t get what you mean.

fracv gravatar imagefracv ( 2019-12-31 13:48:42 -0600 )edit