arccoder / opencvdragrect

Drag a rectangle on an image window using opencv
https://arccoder.github.io/opencvdragrect/
40 stars 21 forks source link

How to draw multiple rectangles in the same image? #7

Closed nithinreddyy closed 2 years ago

nithinreddyy commented 2 years ago

I'm unable to draw multiple rectangles in the same image. Can anyone help me with this?

arccoder commented 2 years ago

Hi @nithinreddyy, This project is limited to dragging a single rectangle on an image. I don't have any plans to add support for multiple rectangles. Thanks, @arccoder

JohnTailor commented 2 years ago

You can draw multiple ones, but only the last one will be shown as follows: Define a global var: drageRects=[]

In Function def dragrect(event, x, y, flags, dragObj) add:

if event == cv2.EVENT_RBUTTONDOWN:
    drageRects.append(copy.deepcopy(dragObj))
    dragObj.reset()
    print("Reset",len(drageRects))
    mouseUp(dragObj)

Add the function reset to Dragrectangle: def reset(self):

Set rect to zero width and height

    self.outRect.x = 0
    self.outRect.y = 0
    self.outRect.w = 0
    self.outRect.h = 0
    self.active =  0

Now, whenever you rightclick the current rectangle is added to the list of drageRects and you can start drawing a new one.