Search code examples
python-3.xcallbackmouseopencv3.0drawrectangle

Opencv draw a rectangle in a picture were never shown


Hey everybody i have some trouble using opencv 3.x and python 3.x. What i want to do is to draw a basic rectangle in a picture but the rectangle will never be drawn. I read this similar thread but it doesn't helped me with my fault. Python OpenCV: mouse callback for drawing rectangle

It would be nice if someone could give me a hint.

#!/usr/bin/env python3
import cv2
import numpy as np
Path = 'picture.jpg'
image_float_size = 400.0
image_int_size = int(image_float_size)
color = [0,255,0]
rectangle = False

def on_event(event,x,y,flags,param):
    global startpointx,startpointy,rectangle
    if event == cv2.EVENT_LBUTTONDOWN:
        rectangle = True
        startpointx = x
        startpointy = y
        print('Down',x,y) #debugging
        cv2.rectangle(resized,(x,y),(x,y),(0,255,0),-1)

    elif event == cv2.EVENT_LBUTTONUP:
        rectangle = False
        print('Up',x,y)
        cv2.rectangle(resized,(startpointx,startpointy),(x,y),(0,255,0),-1)

    elif event == cv2.EVENT_MOUSEMOVE:
        if rectangle:
            print('Move',startpointx,startpointy,x,y)#debugging
            cv2.rectangle(resized,(startpointx,startpointy),(x,y),(0,255,0),-1)

# Read the image and convert it into gray
image = cv2.imread(Path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# resize the image
ration = image_float_size / gray_image.shape[1]
dim = (image_int_size,int(gray_image.shape[0]*ration))
resized = cv2.resize(gray_image, dim, interpolation = cv2.INTER_AREA)

# set window for the image
cv2.namedWindow('window')

# mouse callback
cv2.setMouseCallback('window',on_event)

# wait forever for user to press any key, after key pressed close all windows
while True:
    cv2.imshow('window',resized)
    if cv2.waitKey(0):
        break
        cv2.destroyAllWindows()

Solution

  • You perform drawing (displaying of an image by using cv2.imshow) only once because cv2.waitKey(0) waits indefinitely. If you use some non-zero argument it will wait for that number of milliseconds. But notice that you're constantly rewriting/modifying an image. This is probably not what you want. I think you need to create a temporary (drawing) copy of an image first and restore it each time from original one before new drawing (rectangle).

    #!/usr/bin/env python3
    import cv2
    import numpy as np
    Path = 'data/lena.jpg'
    image_float_size = 400.0
    image_int_size = int(image_float_size)
    color = [0,255,0]
    rectangle = False
    
    def on_event(event,x,y,flags,param):
        global draw_image
        global startpointx,startpointy,rectangle
        if event == cv2.EVENT_LBUTTONDOWN:
            rectangle = True
            startpointx = x
            startpointy = y
            print('Down',x,y) #debugging
            draw_image = resized.copy()
            cv2.rectangle(draw_image,(x,y),(x,y),(0,255,0))
    
        elif event == cv2.EVENT_LBUTTONUP:
            rectangle = False
            print('Up',x,y)
            draw_image = resized.copy()
            cv2.rectangle(draw_image,(startpointx,startpointy),(x,y),(0,255,0))
    
        elif event == cv2.EVENT_MOUSEMOVE:
            if rectangle:
                print('Move',startpointx,startpointy,x,y)#debugging
                draw_image = resized.copy()
                cv2.rectangle(draw_image,(startpointx,startpointy),(x,y),(0,255,0))
    
    # Read the image and convert it into gray
    image = cv2.imread(Path)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # resize the image
    ration = image_float_size / gray_image.shape[1]
    dim = (image_int_size,int(gray_image.shape[0]*ration))
    resized = cv2.resize(gray_image, dim, interpolation = cv2.INTER_AREA)
    draw_image = resized.copy()
    
    # set window for the image
    cv2.namedWindow('window')
    
    # mouse callback
    cv2.setMouseCallback('window',on_event)
    
    while True:
        cv2.imshow('window', draw_image)
        ch = 0xFF & cv2.waitKey(1)
        if ch == 27:
            break
    
    cv2.destroyAllWindows()