0

大家好,我在使用 opencv 3.x 和 python 3.x 时遇到了一些麻烦。我想做的是在图片中绘制一个基本矩形,但永远不会绘制矩形。我读了这个类似的线程,但它并没有帮助我解决我的错误。 Python OpenCV:绘制矩形的鼠标回调

如果有人能给我一个提示,那就太好了。

#!/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()
4

1 回答 1

1

您只执行一次绘图(使用 cv2.imshow 显示图像),因为 cv2.waitKey(0) 无限期地等待。如果您使用一些非零参数,它将等待该毫秒数。但请注意,您不断地重写/修改图像。这可能不是你想要的。我认为您需要先创建图像的临时(绘图)副本,并在每次新绘图(矩形)之前从原始图像恢复它。

#!/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()
于 2015-05-20T08:08:00.577 回答