image - 单击左键后如何在拖动鼠标的同时继续绘制矩形?

标签 image opencv events

我已经了解如何捕获鼠标移动来绘制矩形,方法是先单击左键,然后拖动鼠标,然后释放左键。我的代码如下。但是,即使在单击左键后将鼠标移动到图像上,我也想继续绘制矩形。

不幸的是,现在只有在我最后释放左键后才会显示矩形。

# import the necessary packages
import argparse
import cv2

# initialize the list of reference points and boolean indicating
# whether cropping is being performed or not
refPt = []
cropping = False

def click_and_crop(event, x, y, flags, param):
    # grab references to the global variables
global refPt, cropping, image
drag = 0
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
    refPt = [(x, y)]
        drag = 1
    cropping = True

# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
    # record the ending (x, y) coordinates and indicate that
    # the cropping operation is finished
    refPt.append((x, y))
    cropping = False

    # draw a rectangle around the region of interest
    cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
    cv2.imshow("image", image)

elif event == cv2.EVENT_MOUSEMOVE and drag == 1:
    #image = clone.copy()
    cv2.rectangle(image, refPt[0], (x, y), (0, 255, 0), 2)

# load the image, clone it, and setup the mouse callback function
image = cv2.imread("image_00001.jpg")
clone = image.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", click_and_crop)

# keep looping until the 'q' key is pressed
while True:
# display the image and wait for a keypress
    cv2.imshow("image", image)
    key = cv2.waitKey(1) & 0xFF

# if the 'r' key is pressed, reset the cropping region
if key == ord("r"):
    image = clone.copy()

# if the 'c' key is pressed, break from the loop
elif key == ord("c"):
    break

# if there are two reference points, then crop the region of interest
# from teh image and display it
if len(refPt) == 2:
    roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
    cv2.imshow("ROI", roi)
    cv2.waitKey(0)

# close all open windows
cv2.destroyAllWindows()

最佳答案

尝试下面的代码:


import cv2

refPt = []

final_boundaries = []
image = None


def click_and_crop(event, x, y, flags, param):
    global refPt, image
    if event == cv2.EVENT_LBUTTONDOWN:
        refPt = [(x, y)]
    elif event == cv2.EVENT_LBUTTONUP:
        refPt.append((x, y))
        final_boundaries.append((refPt[0],refPt[1]))
        cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
        cv2.imshow("image", image)
    elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
        clone = image.copy()
        cv2.rectangle(clone, refPt[0], (x, y), (0, 255, 0), 2)
        cv2.imshow("image", clone)


def main(image_name):
    global image
    image = cv2.imread("image.png") #convert to image boundary
    cv2.namedWindow("image")
    cv2.setMouseCallback("image", click_and_crop)
    cv2.imshow("image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return (final_boundaries)

关于image - 单击左键后如何在拖动鼠标的同时继续绘制矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944102/

相关文章:

c++ - SDL2_image渲染无法正常工作

java - 从文本文件读取数组中的图像和字符串

python - 如何在 Python 中绘制半椭圆?

c++ - UMat 是否有 Mat 算术 + 运算符替代方案?

python - 如何在 OpenCV 中校正裁剪后的立体图像?

javascript - 如何从类型化数组中的二进制数据创建 PNG blob?

android - 项目图像显示在图库中

javascript - 如何在 ExtJS 面板上监听 keydown 事件

Javascript Html 事件处理

javascript - 更改值时如何防止输入框事件传播?