openmv识别二维码,颜色识别与串口通信

openmv识别二维码,颜色识别与串口通信

  • 目的
    • 初始目的
    • 进阶想法
  • 二维码识别
  • 颜色识别
  • 串口通信
  • 完整代码

目的

初始目的

1.识别二维码
2.识别物体颜色
3.与stm32串口通信

进阶想法

1.给二维码指定内容:“xxx+xxx”,识别并发送到串口;
2.识别三种颜色,按顺序发送到串口;
3.待更新(识别条形码并发送至串口)

二维码识别

while(True):
    img = sensor.snapshot()
    img.lens_corr(1.8)
    for code in img.find_qrcodes():
        output_str="%s" % code.payload()
		#output_str为二维码内容

附(草料二维码生成器)

颜色识别

#颜色阈值
green_threshold   = (73, 96, -79, -22, -128, 127)
red_threshold   = (41, 61, 42, 127, -128, 127)
blue_threshold   = (22, 67, 9, 127, -128, -54)

blobs = img.find_blobs([green_threshold,red_threshold,blue_threshold],x_stride=25,y_stride=50,area_threshold=1000)
for b in blobs:
	img.draw_rectangle(b[0:4]) # rect
    #用矩形标记出目标颜色区域
    img.draw_cross(b[5], b[6]) # cx, cy
    #在目标颜色区域的中心画十字形标记
    print(b[8])#b[8]为颜色代号,red=2,green=1,blue=4

串口通信

openmv上的P4连接stm32的RX,P5连接TX
1.简单方法

from pyb import UART
uart = UART(3, 19200)
while(True):
	if uart.any():#判断是否接收到数据
		getrx = uart.readline()#读取数据
		uart.write('abc')#发送数据

虽然此法较为简单,但是数据容易在传输的过程中出错,以下是更为便捷的一种方法。

2.json发送
openmv可以通过串口直接发送字符串,具体的教程可戳这里->戳我
在此不多做赘述。
但我们想要更稳定,高效的通信方法,以下。

3.按帧传输
参照这位博主的文章->戳我
那么假如我想把读取到格式为"xxx+xxx"的二维码内容发送给串口,可以用以下方式。

#1.定义帧头
FH = bytearray([0xb3,0xb3])
uart.write(FH)#串口发送帧头
#假如已经识别到二维码
a1,a2,a3 = int(output_str[0]),int(output_str[1]),int(output_str[2])
b1,b2,b3 = int(output_str[0]),int(output_str[1]),int(output_str[2])
data=bytearray([a1,a2,a3,b1,b2,b3])#数据转码
uart.write(data)#串口发送

stm32接收:

void USART2_IRQHandler(void){
	
	static uint8_t rebuf[8]={0},i=0;
	
	if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET)
	{
		rebuf[i++]=USART_ReceiveData(USART2);	
		if(rebuf[0]!=0xb3)//帧头
			i=0;
	  if((i==2)&&(rebuf[1]!=0xb3))//判断帧头
			i=0;
		if(i>=7)//代表一帧数据完毕
		{
				memcpy(OpenMV_Rx_BUF,rebuf,i);
			i = 0;
 
		}
		USART_ClearFlag(USART2,USART_FLAG_RXNE);
	}	
}

另外,我们可以通过写小灯闪烁来判断是否成功执行某一步骤

import pyb

led.off()#小灯闪烁并变蓝
time.sleep(100)     #延时100ms
led = pyb.LED(2)
led.on()

完整代码

(这里是早期写的代码,能初步实现一些想法,完善后的过一阵子我就会贴出来哈)

import sensor, image, time, pyb
import ujson
from pyb import UART

green_threshold   = (73, 96, -79, -22, -128, 127)
#(0, 100, -128, -25, -128, 127)
red_threshold   = (41, 61, 42, 127, -128, 127)
#(41, 61, 42, 127, -128, 127)
blue_threshold   = (22, 67, 9, 127, -128, -54)
#(15, 100, -128, 127, -128, -41)
#red=2,green=1,blue=4
getrx = 0
getcmd = 0
renum = 0
colornum = 0
ptrposition = 0
sensor.reset()# 初始化摄像头
sensor.set_pixformat(sensor.RGB565)# 格式为 RGB565.
sensor.set_framesize(sensor.QQVGA) # 使用 QQVGA 速度快一些
sensor.skip_frames(time = 2000) # 跳过2000s,使新设置生效,并自动调节白平衡
sensor.set_auto_gain(False) # 关闭自动自动增益。默认开启的,在颜色识别中,一定要关闭白平衡。
sensor.set_auto_whitebal(False)
#关闭白平衡。白平衡是默认开启的,在颜色识别中,一定要关闭白平衡。
clock = time.clock() # 追踪帧率
led = pyb.LED(1) # Red LED = 1, Green LED = 1, Blue LED = 2, IR LEDs = 4.
uart = UART(3, 115200, timeout_char = 1000)
led.on()

def Rec_NUM1(lista):
    if (lista[0]=='1' and lista[1]=='2' and lista[2]=='3'):
        return 1
    elif (lista[0]=='1' and lista[1]=='3' and lista[2]=='2'):
        return 2
    elif (lista[0]=='2' and lista[1]=='1' and lista[2]=='3'):
        return 3
    elif (lista[0]=='2' and lista[1]=='3' and lista[2]=='1'):
        return 4
    elif (lista[0]=='3' and lista[1]=='1' and lista[2]=='2'):
        return 5
    elif (lista[0]=='3' and lista[1]=='2' and lista[2]=='1'):
        return 6

def Rec_NUM2(lista):
    if (lista[4]=='1' and lista[5]=='2' and lista[6]=='3'):
        return 1
    elif (lista[4]=='1' and lista[5]=='3' and lista[6]=='2'):
        return 2
    elif (lista[4]=='2' and lista[5]=='1' and lista[6]=='3'):
        return 3
    elif (lista[4]=='2' and lista[5]=='3' and lista[6]=='1'):
        return 4
    elif (lista[4]=='3' and lista[5]=='1' and lista[6]=='2'):
        return 5
    elif (lista[4]=='3' and lista[5]=='2' and lista[6]=='1'):
        return 6

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    if uart.any():
        led.off()
        getrx = uart.readline()
        time.sleep(150)     #延时150ms
        led = pyb.LED(2)
        led.on()
        getcmd = int(getrx)
        print(getcmd)
      #  print(img.find_qrcodes())
    img = sensor.snapshot()# 从感光芯片获得一张图像
    img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
    blobs = img.find_blobs([green_threshold,red_threshold,blue_threshold],x_stride=25,y_stride=50,area_threshold=1000)
    if(getcmd==2):
        for code in img.find_qrcodes():
            output_str="%s" % code.payload() #方式1
            renum = int(Rec_NUM1(output_str)*10 + Rec_NUM2(output_str))
            uart.write(ujson.dumps(renum))
            getcmd = 0
            led.off()
    if blobs and getcmd==3:
        for b in blobs:
            # Draw a rect around the blob.
            img.draw_rectangle(b[0:4]) # rect
            #用矩形标记出目标颜色区域
            img.draw_cross(b[5], b[6]) # cx, cy
            #在目标颜色区域的中心画十字形标记
            #print(b[5], b[6], b[8])
            #uart.write(ujson.dumps(b[8]))
            #将颜色序号转为比赛序号
            if b[8]==1:
                colornum=2
            elif b[8]==2:
                colornum=1
            elif b[8]==4:
                colornum=3
            print('colornum=',colornum,'output_str[ptrposition]=',output_str[ptrposition])
            #若任务码对应
            if (int(output_str[ptrposition]) == colornum):
                uart.write('t')
                ptrposition+=1
                if ptrposition==4:
                    ptrposition+=1
                getcmd=0

你可能感兴趣的:(图像处理)