如何在图像上绘制可拖放的线条?
How to draw a drag and droppable line on an image?
假设我在下图中画一条直线,使用
img = cv2.line(img, (0, 20), (400, 20), (0, 0, 255), 9)
现在,如果我想使用鼠标事件移动图像上的红线(可拖放)——这可能吗?如果是这样,请分享您的想法。
OpenCV用于处理和分析图像。
您正在寻找的是一种计算机图形功能,您可以在其中自由操作图像。这是更多 OpenGL's 区域而不是 OpenCV。
我用一些高度为 9
的矩形替换了你的线,这使事情变得更容易。
在回调中,检查是否在矩形内单击了鼠标左键。如果是,保存鼠标位置w.r.t。 “线”,并设置一些标志以指示“拖动”处于活动状态。在移动鼠标的同时,重新绘制直线 w.r.t。保存的鼠标位置。如果释放了鼠标左键,则取消设置“拖动”的标志。
这是一些代码:
import cv2
# Actual mouse callback function
def move_line(event, x, y, flags, param):
# Controls and image need to be global
global diff, img, hold, l, t
# Left mouse button down: Save mouse position where line was dragged
if (event == cv2.EVENT_LBUTTONDOWN) and \
(x >= l) and \
(x <= l + 400) and \
(y >= t) and \
(y <= t + 9):
diff = (x - l, y - t)
hold = True
# Left mouse button up: Stop dragging
elif event == cv2.EVENT_LBUTTONUP:
hold = False
# During dragging: Update line w.r.t. mouse position; show image
if hold:
l, t = (x - diff[0], y - diff[1])
img_copy = img.copy()
cv2.rectangle(img_copy, (l, t), (l + 400, t + 9), (0, 0, 255), cv2.FILLED)
cv2.imshow('image', img_copy)
# Initialize controls
diff = (0, 0)
hold = False
l, t = (0, 100)
# Set up some image; work on copy
img = cv2.imread('path/to/your/image.png')
img_copy = img.copy()
# Initalize line; show image
cv2.rectangle(img_copy, (l, t), (l + 400, t + 9), (0, 0, 255), cv2.FILLED)
cv2.imshow('image', img_copy)
cv2.setMouseCallback('image', move_line)
# Loop until the 'c' key is pressed
while True:
# Wait for keypress
key = cv2.waitKey(1) & 0xFF
# If 'c' key is pressed, break from loop
if key == ord('c'):
break
使用矩形的优势在于,您可以轻松地从图像中捕获感兴趣区域 (ROI),然后您可以将其提供给 OCR(例如查看评论)。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
OpenCV: 4.5.1
----------------------------------------
假设我在下图中画一条直线,使用
img = cv2.line(img, (0, 20), (400, 20), (0, 0, 255), 9)
现在,如果我想使用鼠标事件移动图像上的红线(可拖放)——这可能吗?如果是这样,请分享您的想法。
OpenCV用于处理和分析图像。
您正在寻找的是一种计算机图形功能,您可以在其中自由操作图像。这是更多 OpenGL's 区域而不是 OpenCV。
我用一些高度为 9
的矩形替换了你的线,这使事情变得更容易。
在回调中,检查是否在矩形内单击了鼠标左键。如果是,保存鼠标位置w.r.t。 “线”,并设置一些标志以指示“拖动”处于活动状态。在移动鼠标的同时,重新绘制直线 w.r.t。保存的鼠标位置。如果释放了鼠标左键,则取消设置“拖动”的标志。
这是一些代码:
import cv2
# Actual mouse callback function
def move_line(event, x, y, flags, param):
# Controls and image need to be global
global diff, img, hold, l, t
# Left mouse button down: Save mouse position where line was dragged
if (event == cv2.EVENT_LBUTTONDOWN) and \
(x >= l) and \
(x <= l + 400) and \
(y >= t) and \
(y <= t + 9):
diff = (x - l, y - t)
hold = True
# Left mouse button up: Stop dragging
elif event == cv2.EVENT_LBUTTONUP:
hold = False
# During dragging: Update line w.r.t. mouse position; show image
if hold:
l, t = (x - diff[0], y - diff[1])
img_copy = img.copy()
cv2.rectangle(img_copy, (l, t), (l + 400, t + 9), (0, 0, 255), cv2.FILLED)
cv2.imshow('image', img_copy)
# Initialize controls
diff = (0, 0)
hold = False
l, t = (0, 100)
# Set up some image; work on copy
img = cv2.imread('path/to/your/image.png')
img_copy = img.copy()
# Initalize line; show image
cv2.rectangle(img_copy, (l, t), (l + 400, t + 9), (0, 0, 255), cv2.FILLED)
cv2.imshow('image', img_copy)
cv2.setMouseCallback('image', move_line)
# Loop until the 'c' key is pressed
while True:
# Wait for keypress
key = cv2.waitKey(1) & 0xFF
# If 'c' key is pressed, break from loop
if key == ord('c'):
break
使用矩形的优势在于,您可以轻松地从图像中捕获感兴趣区域 (ROI),然后您可以将其提供给 OCR(例如查看评论)。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
OpenCV: 4.5.1
----------------------------------------