logo

为什么这段代码里面 颜色分割部分会报错

2023-06-07 10:03
I
chep
import sensor, image, time
from pyb import Pin, LED

# 初始化相机、LED灯和引脚
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
led = LED(3)
pin1 = Pin('P1', Pin.OUT_PP)

# 预设颜色阈值
green_threshold = (72, 100, -1, 82, -11, 101)
red_threshold = (0, 50, 20, 80, -10, 40)

while True:
    
    # 获取图像并处理
    img = sensor.snapshot()
    
    # 进行颜色分割
    green_mask = img.color_threshold(green_threshold)
    red_mask = img.color_threshold(red_threshold)
    
    # 对颜色分割进行形态学操作
    green_mask.erode(1)
    green_mask.dilate(2)
    red_mask.erode(1)
    red_mask.dilate(2)
    
    # 对颜色分割进行轮廓检测
    green_blobs = green_mask.find_blobs(min_area=500)
    red_blobs = red_mask.find_blobs(min_area=500)
    
    # 数量统计到count变量中
    count = len(green_blobs) + len(red_blobs)
    
    # 判断数量是否大于等于2,输出电平和点亮LED蓝灯
    if count >= 2:
        pin1.low()
        led.on()
    else:
        pin1.high()
        led.off()

为什么这段代码里面green_mask = img.color_threshold(green_threshold)段会报错找不到原因

2023-06-07 16:46
I
网红小智智 星瞳科技

https://book.openmv.cc/example/04-Image-Filters/color-binary-filter.html

参考这个代码。color_threshold不知道哪来的,OpenMV没有这个函数

2023-06-07 22:17
I
chep

能帮修复整一个完整代码吗