pythonopencvimage-processingsimplecv

Capture an image using a camera and then draw a 'freeform' 'filled' area over the captured image using Python, SimpleCV & OpenCV


I'm trying to work with Python, opencv and simplecv. I want to capture an image using a camera and then draw a 'freeform' 'filled' area over the captured image using Mouse. Can somebody please help me accomplish this. Here is what I have done till now:

from SimpleCV import Camera, Display, Image, Color

cam = Camera()
display = Display()
img = cam.getImage()
img.save(display)

while not display.isDone():
    if display.mouseLeft:
        img.dl().circle((display.mouseX, display.mouseY), 4, Color.WHITE, filled=True)
        img.save(display)
        img.save("draw.png")

I can draw over the captured Image, but only with circles. That too are placed far too wide if I draw with normal speed. Here's how it looks:

The drawing done over captured image

Whereas I am trying to achieve something like this:

This is how I want to draw the area

Can somebody help me out?


Solution

  • Keep mouse position on list and draw polygon:

    from SimpleCV import Camera, Display, Image, Color
    
    cam = Camera()
    display = Display()
    img = cam.getImage()
    img.save(display)
    
    points = []
    
    while not display.isDone():
        if display.mouseLeft:
            point = (display.mouseX, display.mouseY)
    
            points.append( point )
    
            if len(points) > 2:
                img.dl().polygon(points, filled=True, color=Color.RED)
    
            img.dl().circle(point, 4, Color.WHITE, filled=True)
    
            img.save(display)
            img.save("draw.png")
    

    Read SimpleCV doc: Drawing on Images in SimpleCV