【问题标题】:How to continue drawing a rectangle while dragging the mouse once the left button has been clicked?单击左键后如何在拖动鼠标的同时继续绘制矩形?
【发布时间】:2016-12-21 00:15:28
【问题描述】:

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

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

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

【问题讨论】:

  • 你可以从 this 开始,它是 C++ 代码,但应该可以帮助你
  • 感谢@Miki 我查看了它,但无法在此处对 Python 代码进行相关更改。您能建议我进行相关更改吗?
  • 抱歉,我不能(Python 不是我的菜;D)。我建议按照我的代码结构逐行翻译成 Python。毕竟,代码只是调用 OpenCV 函数,将调用转换为 Python 应该不难
  • 或者你可以看看this,但我不保证;D
  • 只需在 MOUSEMOVE 情况下添加 cv2.imshow("image", image) 和可能的 cv2.waitKey(1) 。并在绘制之前克隆原始图像(或撤消之前的绘制)

标签: image opencv events


【解决方案1】:

试试下面的代码:

导入简历2 参考点 = [] final_boundaries = [] 图片 = 无 def click_and_crop(事件,x,y,标志,参数): 全局参考点,图像 如果事件 == cv2.EVENT_LBUTTONDOWN: 参考点 = [(x, y)] elif 事件 == cv2.EVENT_LBUTTONUP: refPt.append((x, y)) final_boundaries.append((refPt[0],refPt[1])) cv2.rectangle(图像, refPt[0], refPt[1], (0, 255, 0), 2) cv2.imshow(“图像”,图像) elif 事件 == cv2.EVENT_MOUSEMOVE 和标志 == cv2.EVENT_FLAG_LBUTTON: 克隆 = image.copy() cv2.rectangle(克隆, refPt[0], (x, y), (0, 255, 0), 2) cv2.imshow(“图像”,克隆) 定义主(图像名称): 全球形象 image = cv2.imread("image.png") #转换为图片边界 cv2.namedWindow("图像") cv2.setMouseCallback("图像", click_and_crop) cv2.imshow(“图像”,图像) cv2.waitKey(0) cv2.destroyAllWindows() 返回(最终边界)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多