**
颜色识别
**
import sensor, image, time,math
from pyb import UART
from pyb import Pin, Timer, LED
import re
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # 关闭自动增益,must be turned off for color tracking
sensor.set_auto_whitebal(False) #颜色识别时,关闭自动白平衡,must be turned off for color tracking
thresholds = [(14, 68, 8, 111, -5, 114),#红色阈值generic_red_thresholds
(33, 85, -76, -12, 16, 81)#绿色阈值generic_green_thresholds
] # generic_blue_thresholds
uart=UART(1,115200) #PA9 txd PA10--rxd 黑色OPENMV接口PA9和PA10。
#uart = UART(3, 9600) #正式红色的openmv的接口P4和P5
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters timeout_char=1000
clock = time.clock()
g_uart_cmdA_flag=0 # 将变量声明为全局变量,如此才可改变其数值
g_uart_cmdB_flag=0 # 将变量声明为全局变量,如此才可改变其数值
g_uart_color_flag=0 # 将变量声明为全局变量,如此才可改变其数值
#*********************************************************************************************
#串口接收函数的定义
def Uart_recv(): # 串口接收数据
global g_uart_cmdA_flag #注意,加一个global全局,一定要加,不然是局部变量。
global g_uart_cmdB_flag #注意,加一个global全局,一定要加,不然是局部变量。
global g_uart_color_flag #注意,加一个global全局,一定要加,不然是局部变量。
if (uart.any()): # 更新串口接收数据
recv_data = eval(str(uart.read())) #接收读取的字符串
print(recv_data) #调试时候,在串行终端打印接收信息,看下接收什么数据
#uart.write(recv_data) #调试时候,发给串口
if ("&" in recv_data) : #如果接收的字符串中有&号。注意,这个不是第一个字符串,只要有&就行,可以修改下,检查帧头
print("Openmv has recved CMD data.") #打印字符串,说明到了运行到了这里
#***************************接收到命令&L,扫描颜色***********************************
if("L" in recv_data): #如果有L字符在接收字符串里面 ,openmv接收到&L命令,把颜色标记位变为1
g_uart_color_flag =1
print("cflag\r\n") #打印信息,告诉调试人,运行到了这里,判断成功
#************************************接收到命令A****************************************
if ("lightA" in recv_data): #如果接收字符串有lightA,比如&lightA
print(len(recv_data)) #打印出来接收字符串的长度
g_uart_cmdA_flag = 1 #命令A标记等于1
print("Ready for light 1!") #打印信息,告诉调试人,运行到了这里,判断成功
## &lightA0 或者&lightA99 提取A后面的数据,必须是正数,后面不能加换行符号,否则长度改变
## 字符串的下标计数从0开始,它是一个标记特殊的list(数据结构会讲到)
##提取字符串的数据,比如&lightA6,这一串字符串总的字符个数是8个,如果&lightA78,字符串的字符个数是大于8;
##将数据78提出来,如果字符串长度大于8个,那么分解成十位和个位,再合并成一个数据给ddd
##将数据6提出来,如果字符串长度等于8个,那么分解个位,给ddd,为什么减48?
if len(recv_data)>8:
ddd=(int(recv_data[7])-48)*10
ddd=ddd+(int(recv_data[8])-48)
print(ddd)
elif len(recv_data)==8:
ddd=(int(recv_data[7])-48)
print(ddd)
#****************************如果收到另外一个命令,那么将对应标记等于1***********************
if ("lightB1" in recv_data):#如果接收字符串有B,比如&B
g_uart_cmdB_flag = 1
print("Ready for B!")
#*********************************************************************************************
#定义两个数据,测试发送
adata =10.2
bdata =123
#定义两个数据,测试发送
cdata=88.65
ddata=896
#********************************************主函数********************************
while(True):
clock.tick()
img = sensor.snapshot()
Uart_recv() #主函数中不断检测,可以在串口中断中进行接收,或者是开辟一个定时器,定时5ms-10ms检查一次这个函数(这方法好像有问题,定时器的),
#***********************************************************命令 A****************************
if g_uart_cmdA_flag ==1 : #命令A的回应
# uart.write("loc"+(str)adata+"num"+(str)bdata+"#") # 串口发送
#发送字符串,字符串中包括了数据,数据可以根据检测得到,浮点数或者是整数
#**************补充图像处理函数
#
uart.write("adata:%0.2f,bdata:%d\r\n"%(adata,bdata))
g_uart_cmdA_flag = 0 #用完将标记清0,给下次使用;
#***********************************************************命令 B ****************************
if g_uart_cmdB_flag ==1 : #命令B的回应
#补充图像处理函数
uart.write("cdata:%0.2f,ddata:%d\r\n"%(cdata,ddata))#发送字符串,字符串中包括了数据,数据可以根据检测得到,浮点数或者是整数
g_uart_cmdB_flag = 0
#****************************命令L**************************************
if g_uart_color_flag ==1: #扫描颜色
print("here\r\n") #调试使用,说明运行到了这里,接收到对应命令。
for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
# These values depend on the blob not being circular - otherwise they will be shaky.
if blob.elongation() > 0.5:
img.draw_edges(blob.min_corners(), color=(255,0,0))
img.draw_line(blob.major_axis_line(), color=(0,255,0))
img.draw_line(blob.minor_axis_line(), color=(0,0,255))
# These values are stable all the time.
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
# Note - the blob rotation is unique to 0-180 only.
img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
if blob.code()==1: #如果是红色
#uart.write("&R\r\n")
print(blob.cx())
print(blob.cy())
uart.write("&Rcx=%d,cy=%d\r\n"%(blob.cx(),blob.cy()))
print("R\r\n")
if blob.code()==2: #如果是绿色
#uart.write("&G\r\n")
uart.write("&Gcx=%d,cy=%d\r\n"%(blob.cx(),blob.cy()))
print("G\r\n")
# uart.write("cdata:%0.2f,ddata:%d\r\n"%(cdata,ddata))#发送字符串,字符串中包括了数据,数据可以根据检测得到,浮点数或者是整数
g_uart_color_flag = 0 #颜色标记位清0
time.sleep(10)
循迹,分ROI做循迹,送东西,识别红色,黑色的线,看电赛送药小车
打包
# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
import pyb, sensor, image, math, time
from pyb import UART
import json
uart = UART(3,115200)
red_threshold = [(71, 25, 98, 19, 101, -14)]
black_threshold = [(0, 42, -84, 19, -104, 32)]
#QVGA 320*240
roi_1 = [(40, 0, 240, 40), # 北
(40, 200, 160, 40), # 南
(0, 0, 40, 240), # 西
(280, 0, 40, 240), # 东
(30,80,300,80)] # 中
sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565.
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(20) # 跳过10帧,使新设置生效
sensor.set_auto_whitebal(False) # turn this off.
sensor.set_auto_gain(False) # must be turned off for color tracking
clock = time.clock()
buf =[0 for i in range(5)]
m = -1
zhong_x = 0
zhong_y = 0
bei_x = 0
bei_y = 0
nan_x = 0
nan_y = 0
dong_x = 0
dong_y = 0
xi_x = 0
xi_y = 0
black_num = 0
threshold_pixel = 280
uart.init(115200, bits=8, parity=None, stop=1)
def pack_dot_data():
pack_data=bytearray([0xAA,0xFF,zhong_x>>8,zhong_x,dong_y>>8,dong_y,xi_y>>8,xi_y,
black_num,0x00,0x00])
lens = len(pack_data) #数据包大小
#pack_data[3] = lens-6; #有效数据个数
sc = 0
ac = 0
i = 0
while i<(lens-1):
sc = sc + pack_data[i]
ac = ac + sc
i=i+1
pack_data[lens-2] = sc
pack_data[lens-1] = ac;
return pack_data
while(True):
clock.tick()
m = -1
isten = 0
black_num = 0
img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0)
for r in roi_1:
m += 1
blobs = img.find_blobs(red_threshold, roi=r[0:4],pixels_threshold=100, area_threshold=100, merge=True)
#img.draw_rectangle(r[0:4], color=(255,0,0))
if blobs:
most_pixels = 0
largest_blob = 0
for i in range(len(blobs)):
#目标区域找到的颜色块(线段块)可能不止一个,找到最大的一个,作为本区域内的目标直线
if blobs[i].pixels() > most_pixels:
most_pixels = blobs[i].pixels()
#merged_blobs[i][4]是这个颜色块的像素总数,如果此颜色块像素总数大于
largest_blob = i
#print(blobs[largest_blob].cx(),blobs[largest_blob].cy())
if m == 0:
bei_x = blobs[largest_blob].cx()
bei_y = blobs[largest_blob].cy()
elif m == 1:
nan_x = blobs[largest_blob].cx()
nan_y = blobs[largest_blob].cy()
elif m == 2:
dong_x = blobs[largest_blob].cx()
dong_y = blobs[largest_blob].cy()
elif m == 3:
xi_x = blobs[largest_blob].cx()
xi_y = blobs[largest_blob].cy()
elif m == 4:
zhong_x = blobs[largest_blob].cx()
zhong_y = blobs[largest_blob].cy()
# Draw a rect around the blob.
img.draw_rectangle(blobs[largest_blob].rect())
#将此区域的像素数最大的颜色块画矩形和十字形标记出来
img.draw_cross(blobs[largest_blob].cx(),
blobs[largest_blob].cy())
buf[m] = 1
else:
buf[m] = 0
if m == 0:
bei_x = 0
bei_y = 0
elif m == 1:
nan_x = 0
nan_y = 0
elif m == 2:
dong_x = 0
dong_y = 0
elif m == 3:
xi_x = 0
xi_y = 0
elif m == 4:
zhong_x = 0
zhong_y = 0
blobs = img.find_blobs(black_threshold, roi=r[0:4],pixels_threshold=100, area_threshold=100, merge=False)
for b in blobs:
#记录大于threshold_pixel值的黑色色块的数量
if b.pixels() > threshold_pixel:
img.draw_rectangle(b.rect())
print(b.pixels())
black_num = black_num + 1;
print(zhong_x,zhong_y)
uart.write(pack_dot_data())
print(black_num)
print(clock.fps())