代码拉取完成,页面将自动刷新
import cv2 as cv
import image
import graph
import numpy as np
import threading
winName = image.winName # 窗口名
seed = [] # 种子结点列表
threadStop = [False] # 线程列表
# 用于停止鼠标事件的响应
def setVoid(event, x, y, flags, param):
pass
# 用于绘制路径
def draw(end):
end = graph.arr[end[0]][end[1]]
if end.e and not end.inl:
while end.backNode is not None:
cv.circle(image.img, (end.y, end.x), 2, (0, 127, 255), -1)
end = end.backNode
# 鼠标回调函数
def onMouse(event, x, y, flags, param):
# 鼠标左键按下
if event == cv.EVENT_LBUTTONDOWN:
length = len(threadStop)
threadStop[length - 1] = True # 终止上一线程
threadStop.append(False)
t = threading.Thread(target=graph.path, args=(y, x, length)) # 创建新线程
t.start()
print('设置种子结点为:( %d, %d )' % (y, x))
len1 = len(seed)
if len1 > 0:
cv.circle(image.img, (seed[len1 - 1][1], seed[len1 - 1][0]), 3, (0, 0, 0), -1)
seed.append((y, x))
cv.circle(image.img, (x, y), 3, (0, 0, 0), -1)
image.temp = image.img.copy()
# 鼠标移动
elif event == cv.EVENT_MOUSEMOVE:
len1 = len(seed)
if len1 > 0:
image.img = image.temp.copy()
draw((y, x))
# 鼠标右键按下
elif event == cv.EVENT_RBUTTONDOWN:
len1 = len(seed)
if len1 > 0:
cv.line(image.img, (x, y), (seed[0][1], seed[0][0]), (0, 127, 255), thickness=3)
cv.circle(image.img, (seed[0][1], seed[0][0]), 3, (0, 0, 0), -1)
cv.circle(image.img, (seed[len1 - 1][1], seed[len1 - 1][0]), 3, (0, 0, 0), -1)
print('绘制结束!!!')
cv.setMouseCallback(winName, setVoid)
# 每次鼠标事件结束刷新窗口
cv.imshow(image.winName, image.img)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。