gpt4 book ai didi

python - 使用 Python OpenCV 库使用鼠标单击在图像上绘制一条线

转载 作者:行者123 更新时间:2023-12-02 16:27:25 26 4
gpt4 key购买 nike

我正在尝试使用鼠标操作在图像上画一条线。我正在使用 OpenCV 库。我的代码

import matplotlib.pyplot as plt
import cv2

src_window = 'CV2 Window'
SHOW_DEBUG_STEPS = True

drag = 0
select_flag = 0
x1 = 0
x2 = 0
y1 = 0
y2 = 0

point1 = [x1,y1]
point2 = [x2,y2]
SCALAR_YELLOW = (0.0,255.0,255.0)

cap = cv2.VideoCapture('example_01.mp4')

def closeAll():
cap.release()
cv2.destroyAllWindows()

def retn(ret):
if not ret:
print('Error reading the frame')
closeAll()

def frm(fFrame):
if fFrame is None:
print('Error reading the frame')
closeAll()


def drawMyLine(frame):
global point1
global point2
cv2.line(frame,(point1[0],point1[1]),(point2[0],point2[1]),SCALAR_YELLOW,2,8)


def myMouseHandler(event,x,y,flags,param):
global point1
global point2
global drag
global select_flag
global callback

if (event==cv2.EVENT_LBUTTONDOWN and not(drag) and not(select_flag)):
print('case 1')
point1=[x,y]
drag = 1

if (event == cv2.EVENT_MOUSEMOVE and drag and not(select_flag)):
print('case 2')
img1 = fFrame.copy()
point2 = [x,y]
drawMyLine(img1)

if (event == cv2.EVENT_LBUTTONUP and drag and not(select_flag)):
print('case 3')
img2 = fFrame.copy()
point2 = [x,y]
drag = 0
select_flag = 1
cv2.imshow(src_window,img2)
callback = 1


if not(cap.isOpened()):
print('Error reading the video')

ret,fFrame = cap.read()
retn(ret)
frm(fFrame)

fGray = cv2.cvtColor(fFrame,cv2.COLOR_BGR2GRAY)

cv2.imshow(src_window,fGray)
cv2.setMouseCallback(src_window,myMouseHandler)
cv2.waitKey(0)

当我运行代码并尝试通过单击鼠标左键画一条线,将鼠标拖动到第二个点并释放鼠标左键时,我看到我的打印语句 case1、case2、case3 正在终端中打印。但线路还没有出现。我不确定我哪里出错了。

最佳答案

要使用鼠标单击在图像上绘制线条,我们必须捕获鼠标单击的事件 Action ,然后记录起始坐标和结束坐标。 OpenCV 允许我们通过处理鼠标点击事件来做到这一点。每当触发鼠标单击事件时,OpenCV 都会将信息附加到 cv2.setMouseCallback 处理程序,从而将信息中继到我们的 extract_coordinates 回调函数。为了检测事件,OpenCV 需要各种参数:

  • 事件:发生的事件(左/右按下或释放鼠标单击)
  • x:事件的 x 坐标
  • y:事件的 y 坐标
  • flags:OpenCV传递的相关标志
  • 参数:OpenCV 传递的额外参数

按下左键单击 (cv2.EVENT_LBUTTONDOWN) 记录起始坐标,而松开左键单击 (cv2.EVENT_LBUTTONUP) 记录结束坐标。然后我们用 cv2.line 画一条线并将坐标打印到控制台。右键单击 (cv2.EVENT_RBUTTONDOWN) 将重置图像。这是一个在图像上绘制线条的简单小部件:

import cv2

class DrawLineWidget(object):
def __init__(self):
self.original_image = cv2.imread('1.jpg')
self.clone = self.original_image.copy()

cv2.namedWindow('image')
cv2.setMouseCallback('image', self.extract_coordinates)

# List to store start/end points
self.image_coordinates = []

def extract_coordinates(self, event, x, y, flags, parameters):
# Record starting (x,y) coordinates on left mouse button click
if event == cv2.EVENT_LBUTTONDOWN:
self.image_coordinates = [(x,y)]

# Record ending (x,y) coordintes on left mouse bottom release
elif event == cv2.EVENT_LBUTTONUP:
self.image_coordinates.append((x,y))
print('Starting: {}, Ending: {}'.format(self.image_coordinates[0], self.image_coordinates[1]))

# Draw line
cv2.line(self.clone, self.image_coordinates[0], self.image_coordinates[1], (36,255,12), 2)
cv2.imshow("image", self.clone)

# Clear drawing boxes on right mouse button click
elif event == cv2.EVENT_RBUTTONDOWN:
self.clone = self.original_image.copy()

def show_image(self):
return self.clone

if __name__ == '__main__':
draw_line_widget = DrawLineWidget()
while True:
cv2.imshow('image', draw_line_widget.show_image())
key = cv2.waitKey(1)

# Close program with keyboard 'q'
if key == ord('q'):
cv2.destroyAllWindows()
exit(1)

关于python - 使用 Python OpenCV 库使用鼠标单击在图像上绘制一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60587273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com