cvg*_*cvg 2 python opencv image image-processing line
我正在尝试使用鼠标操作在图像上画一条线。我正在使用 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)
Run Code Online (Sandbox Code Playgroud)
当我运行代码并尝试通过单击鼠标左键绘制一条线,将鼠标拖动到第二个点并释放鼠标左键时,我看到我的打印语句 case1、case2、case3 正在终端中打印。但线路还没有出现。我不确定我哪里出错了。
要使用鼠标单击在图像上绘制线条,我们必须捕获鼠标单击的事件动作,然后记录起始坐标和结束坐标。OpenCV 允许我们通过处理鼠标点击事件来做到这一点。每当触发鼠标单击事件时,OpenCV 都会extract_coordinates通过将信息附加到处理程序来将信息中继到我们的回调函数cv2.setMouseCallback。为了检测事件,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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6590 次 |
| 最近记录: |