Python OpenCV - 绘制适当的大小调整Rectangle

时间:2017-03-11 13:23:34

标签: python opencv

我一直在尝试这个代码,我有一个图像,然后我可以根据我点击并拖动鼠标时绘制一个矩形。在鼠标按下事件中,我将x和y坐标标记为矩形的初始角。在鼠标移动中,我将矩形从我保存的旧x和y坐标绘制到鼠标所在的新坐标。最后,在鼠标向上,我绘制矩形作为最终。

以下是我正在使用的代码:

import cv2
import numpy as np

drawing = False # True if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix, iy = -1, -1

# mouse callback function
def draw_circle(event, x, y, flags, param):
    global ix, iy, drawing, mode, overlay, output, alpha
    overlay = img.copy()
    output = img.copy()
    alpha = 0.5

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        ix, iy = x, y

    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            if mode == True:
                cv2.rectangle(overlay, (ix, iy), (x, y), (0, 255, 0), -1)
                cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, img)
                cv2.imshow('image', img)
            else:
                cv2.circle(overlay, (x,y),5,(0,0,255),-1)

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        if mode == True:
            cv2.rectangle(overlay, (ix, iy), (x, y), (0, 255, 0), -1)
            cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, img)

        else:
            cv2.circle(overlay, (x, y), 5, (0, 0, 255), -1)


##img = np.zeros((512, 512, 3), np.uint8)
# Get our image
img = cv2.imread("bed_cv.jpg", 1)

#make cv2 windows, set mouse callback
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_circle)

while(1):
    cv2.imshow('image', img)

    # This is where we get the keyboard input
    # Then check if it's "m" (if so, toggle the drawing mode)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('m'):
        mode = not mode
    elif k == 27:
        break

cv2.destroyAllWindows()

当然这里的问题是,如果我从中心开始,然后向右下方拖动,然后回到左下角,第一个矩形不会被删除。当然这是因为我实际上是在图像上绘制矩形而不是清除它,因此在鼠标移动事件中创建的每个矩形都会被绘制出来。

当您在桌面上绘制一个方框时,我希望实现的效果类似,您可以在不制作矩形的情况下改变方向。

我的问题是,是否可以直观地绘制一个矩形但不能将其写入图像呢?

1 个答案:

答案 0 :(得分:0)

想法是制作图像的副本(在本例中为img2),然后将其用作鼠标移动事件中叠加层的输出。然后在按钮启动事件中使用原始图像(在本例中为img):

import cv2
import numpy as np

drawing = False # True if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix, iy = -1, -1

# mouse callback function
def draw_circle(event, x, y, flags, param):
    global ix, iy, drawing, mode, overlay, output, alpha
    overlay = img.copy()
    output = img.copy()
    alpha = 0.5

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        ix, iy = x, y

    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            if mode == True:
                cv2.rectangle(overlay, (ix, iy), (x, y), (0, 255, 0), -1)
                cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, img2)
                cv2.imshow('image', img2)
            else:
                cv2.circle(overlay, (x,y),5,(0,0,255),-1)

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        if mode == True:
            cv2.rectangle(overlay, (ix, iy), (x, y), (0, 255, 0), -1)
            cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, img)

        else:
            cv2.circle(overlay, (x, y), 5, (0, 0, 255), -1)


##img = np.zeros((512, 512, 3), np.uint8)
# Get our image
img = cv2.imread("bed_cv.jpg", 1)
img2 = img.copy()

#make cv2 windows, set mouse callback
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_circle)

while(1):
    cv2.imshow('image', img2)

    # This is where we get the keyboard input
    # Then check if it's "m" (if so, toggle the drawing mode)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('m'):
        mode = not mode
    elif k == 27:
        break

cv2.destroyAllWindows()