OpenCV和Python使用鼠标移动创建矩形,赋值问题

我是python的新手...分配值时遇到问题。我正在尝试用鼠标作为指示器绘制矩形的周长。以下代码是我尝试实现的方法。

import numpy as np
import cv2
from matplotlib import pyplot as plt

start = None
current = None
save = False


def drawRectangle():
    def getPoint(event,x,y,flags,param):
        global start
        global current
        global save
        if event == cv2.EVENT_LBUTTONDOWN:
            start = (x,y)
            save = False
        elif event == cv2.EVENT_MOUSEMOVE:
            current = (x,y)
        elif event == cv2.EVENT_LBUTTONUP:
            save = True
            start = None
            current = None

    img = np.zeros((512,512,3),np.uint8)
    black_img = np.zeros((512,np.uint8)

    cv2.namedwindow('image')
    cv2.setMouseCallback('image',getPoint,img)
    while (1):
        global save
        if start is not None:
            img = black_img
            cv2.rectangle(img,start,current,(0,255,0),1)
        if save is True:
            black_img = img
            save = False
        cv2.imshow('cpy',black_img)
        cv2.imshow('image',img)

        if cv2.waitKey(20) & 0xFF == 27:
            break
    cv2.destroyAllWindows()


if __name__ == '__main__':
    drawRectangle()

问题-在img = black_img行中,black_img被分配了与img相同的值,老实说,我不知道为什么。为了显示问题,我还包括了一个带有black_img的窗口,该窗口与带有img的窗口相同 https://zapodaj.net/images/12be96b535246.png-外观

我设法通过在img = np.zeros((512,np.uint8)内用if start is not None:创建一个新的黑色图像(而不是分配black_img)来解决此问题,但这无法绘制多个矩形。 https://zapodaj.net/images/088c86020ebc1.png-外观

我已经在这个问题上停留了一段时间...请帮助。

kjhsdgfhkjshfuwerytu 回答:OpenCV和Python使用鼠标移动创建矩形,赋值问题

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3136299.html

大家都在问