import sensor, image, time, mathfrom pyb import UART#threshold_index = 0 # 0 for red, 1 for green, 2 for blue # 颜色跟踪阈值 (L Min, L Max, A Min, A Max, B Min, B Max)# 下面的阈值通常跟踪红色/绿色/蓝色的东西。您可能希望调整它们……thresholds = [(50, 65, 40, 50, 35, 60), # generic_red_thresholds (75, 90, -80, -70, 40, 80), # generic_green_thresholds (55, 70, -21, 0, -50, -35)] # generic_blue_thresholds sensor.reset()#重置感光元件,重置摄像机sensor.set_pixformat(sensor.RGB565) #设置颜色格式为RGB565,彩色,每个像素16bit。sensor.set_framesize(sensor.QVGA) #图像大小为QVGAsensor.skip_frames(time = 2000) #跳过n张照片,在更改设置后,跳过一些帧,等待感光元件变稳定。sensor.set_auto_gain(False) #颜色识别必须关闭自动增益,会影响颜色识别效果sensor.set_auto_whitebal(False) #颜色识别必须关闭白平衡,会影响颜色识别效果,导致颜色的阈值发生改变clock = time.clock() uart = UART(3, 9600) #初始化串口3,波特率为9600(注意:上位机记得也配置成9600) # 只有像素大于“pixels_threshold”和面积大于“area_threshold”的区域才是# 由下面的"find_blobs"返回。更改“pixels_threshold”和“area_threshold”# 相机的分辨率。"merge=True"合并图像中所有重叠的斑点。 while(True): clock.tick()# 追踪两个snapshots()之间经过的毫秒数. img = sensor.snapshot()#截取感光元件中的一张图片 img.lens_corr(1.8) # 1.8的强度参数对于2.8mm镜头来说是不错的。 #在img.find_blobs这个函数中,我们进行颜色识别 #roi是“感兴趣区”,是在画面的中央还是右上方或者哪里进行颜色识别。此处我们没有进行配置,默认整个图像进行识别 for blob in img.find_blobs([thresholds[0]], pixels_threshold=200, area_threshold=200, merge=True): # 这些值始终是稳定的。 uart.write("0") print('0') img.draw_rectangle(blob.rect()) #用矩形标记出目标颜色区域 img.draw_cross(blob.cx(), blob.cy()) #在目标颜色区域的中心画十字形标记 for blob in img.find_blobs([thresholds[1]], pixels_threshold=200, area_threshold=200, merge=True): # 这些值始终是稳定的。 uart.write("1") print('1') img.draw_rectangle(blob.rect()) #用矩形标记出目标颜色区域 img.draw_cross(blob.cx(), blob.cy()) #在目标颜色区域的中心画十字形标记 for blob in img.find_blobs([thresholds[2]], pixels_threshold=200, area_threshold=200, merge=True): # 这些值始终是稳定的。 uart.write("2") print('2') img.draw_rectangle(blob.rect()) #用矩形标记出目标颜色区域 img.draw_cross(blob.cx(), blob.cy()) #在目标颜色区域的中心画十字形标记 for code in img.find_qrcodes(): # 进行二维码检测 img.draw_rectangle(code.rect(), color = (255, 0, 0)) message = code.payload() #返回二维码有效载荷的字符串 if message == 'red': uart.write("0") print('0') if message == 'green': uart.write("1") print('1') if message == 'blue': uart.write("2") print('2')
时间: 2025-03-22 18:07:38 浏览: 69
### 颜色识别与二维码扫描程序性能优化
为了提升基于 OpenMV 的颜色识别与二维码扫描程序的性能,可以从以下几个方面入手:
#### 1. 减少图像处理的数据量
通过缩小输入图像分辨率来减少 RAM 中存储的像素数量。OpenMV 支持调整摄像头捕捉的图像大小,这可以通过 `sensor.set_framesize()` 方法实现[^1]。降低分辨率会显著减少每帧所需的内存空间和计算时间。
```python
import sensor, image
# 设置较低的分辨率为 QVGA (320x240)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA) # 或者更小如 QQVGA (160x120)
```
#### 2. 利用 ROI 提高效率
如果只需要检测特定区域中的二维码,则可以定义一个感兴趣区域(ROI),从而避免对整张图片进行不必要的处理。`img.find_qrcodes([roi])` 是用于此目的的有效工具[^3]。合理设置 roi 参数能够大幅削减算法复杂度。
```python
# 定义感兴趣区域
roi = (80, 60, 160, 120) # 假设目标位于中心部分
qrcodes = img.find_qrcodes(roi=roi)
for code in qrcodes:
print(code.payload())
```
#### 3. 调整阈值加速颜色过滤
对于颜色识别模块来说,精确设定二值化阈值得到黑白掩模至关重要。过宽泛或者不合适的范围都会增加误判率同时也拖慢速度。因此建议多次实验找到最贴近实际应用场景的最佳数值组合。
```python
red_threshold = [(30, 100), (-60, -10), (0, 60)] # 示例红色阈值
green_threshold = [(30, 100), (-10, 60), (-60, -10)] # 示例绿色阈值
blobs_red = img.find_blobs([red_threshold], pixels_threshold=100, area_threshold=100)
if blobs_red:
print("Detected Red QR Code!")
blobs_green = img.find_blobs([green_threshold], pixels_threshold=100, area_threshold=100)
if blobs_green:
print("Detected Green QR Code!")
```
#### 4. 数据传输精简设计
既然提到有用的信息仅有三个参数而非四个,就应该考虑剔除冗余项以减轻通信负担。具体做法取决于串口协议的设计,在不影响功能的前提下尽可能压缩消息长度[^2]。
```c
// 单片机接收端伪代码示例
void process_data(uint8_t *data){
int x = data[0]; // X坐标
int y = data[1]; // Y坐标
float angle = *(float*)&data[2]; // 解码角度
// 不再解析多余字段...
}
```
以上方法综合运用可有效改善整个系统的响应时间和资源利用率。
阅读全文
相关推荐




