这个工程的应用很简单,通过该H,S,L的阈值来提取识别物。
如果要换rgb识别,则省去RGBtoHSL这个函数,然后测出阈值,就一样了。
是基于stm32f103rct6+ov7670的,通过该原子的固件库历程实现的,适合于新手学习。
//RGB转HSL
单片机源程序如下:
- #include "sys.h"
- #include "usart.h"
- #include "delay.h"
- #include "led.h"
- #include "usmart.h"
- #include "lcd.h"
- #include "hsl.h"
- #include "ov7670.h"
- #include "exti.h"
- #include "timer.h"
- extern u8 ov_sta; //在exit.c里面定义
- extern u8 ov_frame; //在timer.c里面定义
- //
- int h,s2,l;
- u16 get_date;
- COLOR_HSL Hsl1;
- #define min3v(v1, v2, v3) ((v1)>(v2)? ((v2)>(v3)?(v3):(v2)):((v1)>(v3)?(v3):(v1)))//比较最小值
- #define max3v(v1, v2, v3) ((v1)<(v2)? ((v2)<(v3)?(v3):(v2)):((v1)<(v3)?(v3):(v1)))//比较最大值
- RESULT Resu;//捕获颜色设置
- // TARGET_CONDI Condition={60,100,20,120,10,160,40,40,320,240};
- //TARGET_CONDI Condition={0,200,0,34,20,30,40,40,320,240};
- //unsigned char i;
- //static volatile ErrorStatus HSEStartUpStatus = SUCCESS;
- int find_x,find_y;
- //unsigned int position=0;
- //typedef struct
- // {
- // int x; // [0,320]
- // int y; // [0,240]
- // }
- //ZUOBIAO;//HSL格式颜色
- //ZUOBIAO zuobiao_for_top;
- //ZUOBIAO zuobiao_for_down;
- //ZUOBIAO zuobiao_for_left;
- //ZUOBIAO zuobiao_for_right;
- //ZUOBIAO zuobiao_for_center;
- //
- //更新LCD显示
- void camera_refresh(void)
- {
- u32 j;
- u16 color;
- if(ov_sta==2)
- {
- LCD_Scan_Dir(U2D_L2R); //从上到下,从左到右
- LCD_SetCursor(0x00,0x0000); //设置光标位置
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- OV7670_CS=0;
- OV7670_RRST=0; //开始复位读指针
- OV7670_RCK=0;
- OV7670_RCK=1;
- OV7670_RCK=0;
- OV7670_RRST=1; //复位读指针结束
- OV7670_RCK=1;
- for(j=0;j<76800;j++)
- {
- GPIOB->CRL=0X88888888;
- OV7670_RCK=0;
- color=OV7670_DATA; //读数据
- OV7670_RCK=1;
- color<<=8;
- OV7670_RCK=0;
- color|=OV7670_DATA; //读数据
- OV7670_RCK=1;
- GPIOB->CRL=0X33333333;
- LCD_WR_DATA(color);
- }
- OV7670_CS=1;
- OV7670_RCK=0;
- OV7670_RCK=1;
- EXTI->PR=1<<15; //清除LINE8上的中断标志位
- ov_sta=0; //开始下一次采集
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢复默认扫描方向
- }
- }
- static void readcolor(unsigned int x,unsigned int y,COLOR_RGB *Rgb)
- {
- unsigned short C16;
- C16 = LCD_ReadPoint(x,y);
- Rgb->red = (unsigned char)((C16&0xf800)>>8);
- Rgb->green = (unsigned char)((C16&0x07e0)>>3);
- Rgb->blue = (unsigned char)((C16&0x001f)<<3);
- }
- //RGB转HSL
- static void rGBtohSL(const COLOR_RGB *Rgb,COLOR_HSL *Hsl)
- {
- int maxVal,minVal,difVal;
- int r = Rgb->red;
- int g = Rgb->green;
- int b = Rgb->blue;
- int h,s2,l;
- maxVal = max3v(r, g, b);
- minVal = min3v(r, g, b);
-
- difVal = maxVal-minVal;
-
- //计算亮度
- l = (maxVal+minVal)*240/255/2;
-
- if(maxVal == minVal)//若r=g=b
- {
- h = 0;
- s2 = 0;
- }
- else
- {
- //计算色调
- if(maxVal==r)
- {
- if(g>=b)
- h = 40*(g-b)/(difVal);
- else
- h = 40*(g-b)/(difVal) + 240;
- }
- else if(maxVal==g)
- h = 40*(b-r)/(difVal) + 80;
- else if(maxVal==b)
- h = 40*(r-g)/(difVal) + 160;
- //计算饱和度
- if(l == 0)
- s2 = 0;
- else if(l<=120)
- s2 = (difVal)*240/(maxVal+minVal);
- else
- s2 = (difVal)*240/(511 - (maxVal+minVal));
- }
- Hsl->hue = (unsigned char)(((h>240)? 240 : ((h<0)?0:h)));
- Hsl->saturation = (unsigned char)(((s2>240)? 240 : ((s2<0)?0:s2)));
- Hsl->luminance = (unsigned char)(((l>240)? 240 : ((l<0)?0:l)));
- }
- int main()
- {
- COLOR_RGB Rgb;
- int sum_x=0,sum_y=0,i_xy = 0,n_i;
- int X,Y;
- // int led_flg = 0;
- // int pointCnt=0;
- // int val_exp = 500;
- // int position_front_Cnt = 0,position_left_Cnt = 0,position_right_Cnt = 0,position_centre_Cnt = 0;
- // int position_front_exp = val_exp,position_left_exp = val_exp,position_right_exp = val_exp,position_centre_exp = val_exp ,pointCnt_exp = 900; //灵敏度,,就是有多少红色(像素块为单位)可以触发条件。
- ///pointcnt_exp 需要实际测试,来确定最终值
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
- uart_init(9600);
- OV7670_Init();
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init(); //初始化LCD
- if(lcddev.id==0X6804||lcddev.id==0X5310||lcddev.id==0X5510||lcddev.id==0X1963) //强制设置屏幕分辨率为320*240.以支持3.5寸大屏
- {
- lcddev.width=240;
- lcddev.height=320;
- }
- usmart_dev.init(72); //初始化USMART
-
- POINT_COLOR=BLACK;//设置字体为红色
-
- while(OV7670_Init())//初始化OV7670
- {
- LCD_ShowString(60,150,200,200,16,"OV7670 Error!!");
- delay_ms(200);
- LCD_Fill(60,150,239,166,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(60,150,200,200,16,"camera, okey!");
- TIM3_Int_Init(10000,7199); //TIM3,10Khz计数频率,1秒钟中断
- EXTI15_Init(); //使能定时器捕获
- OV7670_Window_Set(10,174,240,320); //设置窗口
- OV7670_CS=0;
- uart_init(9600); //串口初始化为9600
-
- while(1)
- {
- for(find_y=0;find_y<320;find_y=find_y +4)//寻找测试点
- {
- for(find_x=0;find_x<240;find_x=find_x +4)
- {
- get_date=LCD_ReadPoint(find_x,find_y);
- readcolor(find_x,find_y,&Rgb);
- rGBtohSL(&Rgb,&Hsl1);// 完成了度rgb转化为色调,亮度,对比度
-
- if(((Hsl1.hue>=0)&&(Hsl1.hue<=27))&&((Hsl1.saturation>=70)&&\
- (Hsl1.saturation<=100))&&((Hsl1.luminance>=70)&&(Hsl1.luminance<=150))) //红色条件
- //if(((Hsl1.hue>=220)&&(Hsl1.hue<=240))&&((Hsl1.saturation>=80)&&\
- // (Hsl1.saturation<=100))&&((Hsl1.luminance>=70)&&(Hsl1.luminance<=150))) //蓝色条件
- //if(((Hsl1.hue>=50)&&(Hsl1.hue<=70))&&((Hsl1.saturation>=80)&&\
- // (Hsl1.saturation<=100))&&((Hsl1.luminance>=70)&&(Hsl1.luminance<=150))) //黄色
- {
- sum_x = sum_x+find_x ;
- sum_y = sum_y+find_y ;
- i_xy++;
- }
- }
- }
- // k++;
- n_i=i_xy;
- X=sum_x /n_i;
- Y=sum_y /n_i;
- // 发送 命令
- //if(k==5) // 人为降低发送频率 减少主控缓存压力
- // {
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
转hsl识别.rar
(381.77 KB, 下载次数: 329)
|