显示部分基于oled_spi
#include "REG51.h"
#include "oled.h"
#include "bmp.h"
#define jingzhen 11059200UL /*使用11.0592M晶体*/
#define botelv 9600UL /*波特率定义为9600*/
//unsigned char zifuchuan[]="led1关闭"; //待显示字符。
//unsigned char zifuchuan1[]="led1打开"; //待显示字符。
volatile unsigned char sending;
unsigned int a;
unsigned int falg;
void delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=90;k>0;k--);
}
void init(void) //串口初始化
{
EA=0; //暂时关闭中断
TMOD&=0x0F; //定时器1模式控制在高4位
TMOD|=0x20; //定时器1工作在模式2,自动重装模式
SCON=0x50; //串口工作在模式1
TH1=256-jingzhen/(botelv*12*16); //计算定时器重装值
TL1=256-jingzhen/(botelv*12*16);
PCON|=0x80; //串口波特率加倍
ES=1; //串行中断允许
TR1=1; //启动定时器1
EA=1; //允许中断
}
void send(unsigned char d) //发送一个字节的数据,形参d即为待发送数据。
{
SBUF=d; //将数据写入到串口缓冲
sending=1; //设置发送标志
while(sending); //等待发送完毕
}
void sendc(unsigned char * pd)
{
while((*pd)!='\0')//发送字符串,直到遇到0才结束
{
send(*pd); //发送一个字符
pd++; //移动到下一个字符
}
}
void main()
{
init();
OLED_Init(); //初始化OLED
OLED_Clear();
while(1)
{
OLED_ShowCHinese(0,0,0);//
OLED_ShowCHinese(18,0,1);//
OLED_ShowCHinese(36,0,2);//
OLED_ShowCHinese(54,0,3);//
OLED_ShowCHinese(72,0,4);
OLED_ShowCHinese(90,0,5);
OLED_ShowCHinese(108,0,6);
OLED_ShowString(0,2,"RGB:");
if(falg==1)
{
falg=0;
OLED_Clear();
OLED_ShowString(35,2,"RED");
delay(1000);
}
else if(falg==2)
{
falg=0;
OLED_Clear();
OLED_ShowString(35,2,"BLUE");
delay(1000);
}
else if(falg==3)
{
falg=0;
OLED_ShowString(35,2,"YELLOW");
delay(1000);
}
else if(falg==4)
{
falg=0;
OLED_ShowString(35,2,"GREEN");
delay(1000);
}
}
}
void uart() interrupt 4 //串口发送中断
{
if(RI) //收到数据
{
RI=0; //清中断请求
a=SBUF;
if(a==0x01)
{
falg=1;//RED
}
else if(a==0x02)
{
falg=2;//BLUE
}
else if(a==0x03)
{
falg=3;//yellow
}
else if(a==0x04)
{
falg=4;//green
}
}
else //发送完一字节数据
{
TI=0;
sending=0; //清正在发送标志
}
}
# LOTS OF Blob Detection
import sensor, image, time
# 如果要保证颜色追踪效果的话, 需要对环境的严格控制
# 晚上光源的冷暖色等,都会对颜色追踪造成很大的影响
# 彩色图片颜色的阈值格式组成, 是由LAB颜色空间的各自最小值与最大值组成
# 点击右侧的颜色空间下拉选择按钮, 默认为RGB Color Space
# 参考右侧的LAB Color Space里面的参数
# (minL, maxL, minA, maxA, minB, maxB)
# 灰度图的阈值格式
# (min, max)
# 红色阈值
pink_threshold =(44, 75, 8, 77, -44, 21)
# 黄色阈值
yellow_threshold = (36, 75, -20, 11, 23, 48)
# 蓝色阈值
blue_threshold = (61, 95, -23, -10, -30, -10)
# 绿色阈值
green_threshold =(50, 60, -48, -30, 15, 38)
# 颜色阈值的设定可以在 工具(Tools) -> 机器视觉(Machine Vision) -> 阈值编辑器(Threshold Editor) 中调试
# 颜色代码是find_blobs返回的blob对象中的一个成分, 用于标识,该色块是由在哪个阈值下选择的
# 颜色1: 红色的颜色代码
pink_color_code = 1 # code = 2^0 = 1
# 颜色2: 绿色的颜色代码
yellow_color_code = 2 # code = 2^1 = 2
# 颜色3蓝的代码
blue_color_code = 4# color_code_3 = 2^2 = 4
# 颜色4绿的代码
green_color_code = 8# color_code_4 = 2^3 = 8
sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 选择像素模式 RGB565.
sensor.set_framesize(sensor.QVGA) # use QVGA for speed.
sensor.skip_frames(time = 2000) # Let new settings take affect.
sensor.set_auto_whitebal(False) #关闭白平衡。白平衡是默认开启的,在颜色识别中,需要关闭白平衡。
from pyb import UART
#uart = UART(3,19200)#设置串口1波特率9600
uart = UART(1, 9600)
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
clock = time.clock() # Tracks FPS.
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # 拍照,返回图像
# 在图像中寻找满足颜色阈值约束(color_threshold, 数组格式), 像素阈值pixel_threshold, 色块面积大小阈值(area_threshold)的色块
blobs = img.find_blobs([pink_threshold, yellow_threshold , blue_threshold , green_threshold], area_threshold=100)
if blobs:
#如果找到了目标颜色
for blob in blobs:
#迭代找到的目标颜色区域
x = blob[0]
y = blob[1] #
width = blob[2] # 色块矩形的宽度
height = blob[3] # 色块矩形的高度
center_x = blob[5] # 色块中心点x值
center_y = blob[6] # 色块中心点y值
color_code = blob[8] # 颜色代码
# 添加颜色说明
if color_code == pink_color_code:
img.draw_string(x, y - 10, "red", color = (0xFF, 0x00, 0x00))
print(blob.code())
data=bytearray([0x01])
uart.write(data)
elif color_code == blue_color_code:
img.draw_string(x, y - 10, "blue", color = (0x00, 0x00, 0xFF))
print(blob.code())
data=bytearray([0x02])
uart.write(data)
elif color_code == yellow_color_code:
img.draw_string(x, y - 10, "yellow", color = (0xFF, 0xFF, 0xFF))
print(blob.code())
data=bytearray([0x03])
uart.write(data)
elif color_code == green_color_code:
img.draw_string(x, y - 10, "green", color = (0x00, 0xFF, 0x00))
print(blob.code())
data=bytearray([0x04])
uart.write(data)
#用矩形标记出目标颜色区域
img.draw_rectangle([x, y, width, height])
#在目标颜色区域的中心画十字形标记
img.draw_cross(center_x, center_y)