【问题标题】:How to make ROI line in open cv movable or let user draw it with his mouse如何使开放式 cv 中的 ROI 线可移动或让用户用鼠标绘制它
【发布时间】:2025-11-21 19:20:14
【问题描述】:

我使用this 作为基础来制作更好的车辆计数器,但 roi 线在屏幕中间是硬编码的:cv.line(input_frame, (0, int(height/2)), (int (width), int(height/2)), (0, 0xFF, 0), 5) 像这样,我正在寻找一种方法,两种使其动态化,或者让用户绘制它,如果只找到rectangles 的教程和答案,或者通过鼠标点击使其在屏幕上上下移动。 提前谢谢你。

【问题讨论】:

  • 问“我从哪里开始?”的问题通常过于宽泛,不适合本网站。人们有自己解决问题的方法,因此不可能有正确的答案。仔细阅读 Where to Startedit 您的帖子。
  • 但我知道从哪里开始我确切地知道我需要更改哪些元素但我只能找到更改或绘制矩形的方法

标签: python python-3.x opencv tensorflow


【解决方案1】:

看看here 您需要捕获鼠标坐标,然后使用 cv.line

def click_and_crop(event, x, y, flags, param):
# grab references to the global variables
global refPt, cropping

# 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)]
    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
    # y = height x = width
    refPt.append((x, y))
    cropping = False
    # draw a rectangle around the region of interest
    #cv.line(input_frame, (0, int(height/2)), (int (width), int(height/2)), (0, 0xFF, 0), 5)
    cv2.line(image,(0, int(y)), (int(width), int(y)), (0, 255, 0), 2)
    cv2.imshow("image", image)

【讨论】:

  • 谢谢你正是我想要的。
最近更新 更多