Ask Your Question
0

I can not draw the rectangles with the for loop.

asked 2019-01-23 18:11:10 -0600

Firelife gravatar image

updated 2019-01-23 18:15:09 -0600

I set up an np.array and was able to print all the elements in python console.

a =np.array([[14,137,80,166],[14,174,80,204],[14,214,80,244],[14,252,80,282],
[14,290,80,320],[14,328,80,358],[14,366,80,396],[14,402,80,432]])
for (x1,y1,x2,y2)in a:
   print(x1,y1,x2,y2)

image description

When I set up the for loop to draw the rectangles I get the last one in the array draw on image.

rect = np.array([[14,137,80,166],[14,174,80,204],[14,214,80,244],[14,252,80,282],
[14,290,80,320],[14,328,80,358],[14,366,80,396],[14,402,80,432]])

for (x1,y1,x2,y2)in rect:
  #Create the basic black image
  mask = np.zeros(blur.shape[:2], dtype="uint8")
  cv2.rectangle(mask,(x1,y1),(x2,y2), (COLOR_WHITE), -1)

image description

Any help greatly appreciated. Thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-01-24 01:18:38 -0600

berak gravatar image

updated 2019-01-24 01:19:14 -0600

you have to create your mask outside the loop.

#Create the basic black image
mask = np.zeros(blur.shape[:2], dtype="uint8")
for (x1,y1,x2,y2)in rect:
  cv2.rectangle(mask,(x1,y1),(x2,y2), (COLOR_WHITE), -1)

(silly noob error, isn't it ?)

edit flag offensive delete link more

Comments

Thank you for your help! Yes, it probably is silly.

Firelife gravatar imageFirelife ( 2019-01-24 05:48:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-01-23 18:11:10 -0600

Seen: 3,618 times

Last updated: Jan 24 '19