Opencv 在一张图片中绘制一个矩形从未显示

Opencv draw a rectangle in a picture were never shown

大家好,我在使用 opencv 3.x 和 python 3.x 时遇到了一些问题。 我想要做的是在图片中绘制一个基本矩形,但永远不会绘制矩形。 我读过这个类似的帖子,但它并没有帮助我解决我的错误。 Python OpenCV: mouse callback for drawing rectangle

如果有人能给我提示就好了。

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

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

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