logo

OpenMV4可以支持鼠标事件吗?

2025-03-18 19:28
8
CC

求助各位大咖,OpenMV4 cam可以支持鼠标事件吗?鼠标框出搜索目标,目前代码一运行就会停止在初始画面处

import sensor, image, time

from pyb import LED, Pin

# 初始化摄像头

sensor.reset()

sensor.set_pixformat(sensor.RGB565)

sensor.set_framesize(sensor.QVGA)

sensor.skip_frames(time=2000)

sensor.set_auto_gain(False)

sensor.set_auto_whitebal(False)

# 初始化 IO 引脚用于与 PLC 通信

forward_pin = Pin('P0', Pin.OUT_PP)

backward_pin = Pin('P1', Pin.OUT_PP)

left_pin = Pin('P2', Pin.OUT_PP)

right_pin = Pin('P3', Pin.OUT_PP)

stop_pin = Pin('P4', Pin.OUT_PP)

# 初始化 OpenMV 自带的三色灯

red_led = LED(1)

green_led = LED(2)

blue_led = LED(3)

# 初次上电,三色灯 LED 循环闪亮 1s,两次结束

for _ in range(2):

red_led.on()

green_led.off()

blue_led.off()

time.sleep(1000)

red_led.off()

green_led.on()

blue_led.off()

time.sleep(1000)

red_led.off()

green_led.off()

blue_led.on()

time.sleep(1000)

red_led.off()

green_led.off()

blue_led.off()

# 初始化 Camshift 追踪器

roi = None

hist = None

tracking = False

# 鼠标事件变量

mouse_down = False

start_x = 0

start_y = 0

# 鼠标事件回调函数

def mouse_callback(e):

global mouse_down, start_x, start_y, roi, tracking

if e[0] == 1: # 鼠标按下事件

mouse_down = True

start_x = e[1]

start_y = e[2]

elif e[0] == 3: # 鼠标释放事件

mouse_down = False

end_x = e[1]

end_y = e[2]

if abs(end_x - start_x) > 10 and abs(end_y - start_y) > 10: # 确保框选区域有效

roi = (min(start_x, end_x), min(start_y, end_y), abs(end_x - start_x), abs(end_y - start_y))

hist = image.Image(roi=roi).get_histogram()

tracking = True

# 绑定鼠标回调函数

sensor.skip_frames(callback=mouse_callback)

# 定义距离阈值(假设通过图像中目标大小估算距离)

target_distance = 500 # 期望保持的距离(单位:cm)

distance_threshold = 50 # 允许的距离误差

# 异常标志

is_abnormal = False

clock = time.clock()

last_blink_time = time.ticks_ms()

while True:

clock.tick()

img = sensor.snapshot()

if not tracking:

# 等待用户鼠标框选目标

img.draw_string(10, 10, "Use mouse to select an object to track", color=(255, 0, 0))

if