|
這個(gè)工程的應(yīng)用很簡(jiǎn)單,通過(guò)該H,S,L的閾值來(lái)提取識(shí)別物。
如果要換rgb識(shí)別,則省去RGBtoHSL這個(gè)函數(shù),然后測(cè)出閾值,就一樣了。
是基于stm32f103rct6+ov7670的,通過(guò)該原子的固件庫(kù)歷程實(shí)現(xiàn)的,適合于新手學(xué)習(xí)。
//RGB轉(zhuǎn)HSL
單片機(jī)源程序如下:
- #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;//捕獲顏色設(shè)置
- // 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); //設(shè)置光標(biāo)位置
- LCD_WriteRAM_Prepare(); //開(kāi)始寫(xiě)入GRAM
- OV7670_CS=0;
- OV7670_RRST=0; //開(kāi)始復(fù)位讀指針
- OV7670_RCK=0;
- OV7670_RCK=1;
- OV7670_RCK=0;
- OV7670_RRST=1; //復(fù)位讀指針結(jié)束
- OV7670_RCK=1;
- for(j=0;j<76800;j++)
- {
- GPIOB->CRL=0X88888888;
- OV7670_RCK=0;
- color=OV7670_DATA; //讀數(shù)據(jù)
- OV7670_RCK=1;
- color<<=8;
- OV7670_RCK=0;
- color|=OV7670_DATA; //讀數(shù)據(jù)
- OV7670_RCK=1;
- GPIOB->CRL=0X33333333;
- LCD_WR_DATA(color);
- }
- OV7670_CS=1;
- OV7670_RCK=0;
- OV7670_RCK=1;
- EXTI->PR=1<<15; //清除LINE8上的中斷標(biāo)志位
- ov_sta=0; //開(kāi)始下一次采集
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢復(fù)默認(rèn)掃描方向
- }
- }
- 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轉(zhuǎn)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;
-
- //計(jì)算亮度
- l = (maxVal+minVal)*240/255/2;
-
- if(maxVal == minVal)//若r=g=b
- {
- h = 0;
- s2 = 0;
- }
- else
- {
- //計(jì)算色調(diào)
- 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;
- //計(jì)算飽和度
- 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; //靈敏度,,就是有多少紅色(像素塊為單位)可以觸發(fā)條件。
- ///pointcnt_exp 需要實(shí)際測(cè)試,來(lái)確定最終值
- delay_init(); //延時(shí)函數(shù)初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 設(shè)置中斷優(yōu)先級(jí)分組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) //強(qiáng)制設(shè)置屏幕分辨率為320*240.以支持3.5寸大屏
- {
- lcddev.width=240;
- lcddev.height=320;
- }
- usmart_dev.init(72); //初始化USMART
-
- POINT_COLOR=BLACK;//設(shè)置字體為紅色
-
- 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計(jì)數(shù)頻率,1秒鐘中斷
- EXTI15_Init(); //使能定時(shí)器捕獲
- OV7670_Window_Set(10,174,240,320); //設(shè)置窗口
- OV7670_CS=0;
- uart_init(9600); //串口初始化為9600
-
- while(1)
- {
- for(find_y=0;find_y<320;find_y=find_y +4)//尋找測(cè)試點(diǎn)
- {
- 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轉(zhuǎn)化為色調(diào),亮度,對(duì)比度
-
- 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))) //藍(lán)色條件
- //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;
- // 發(fā)送 命令
- //if(k==5) // 人為降低發(fā)送頻率 減少主控緩存壓力
- // {
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
轉(zhuǎn)hsl識(shí)別.rar
(381.77 KB, 下載次數(shù): 329)
2018-1-24 01:24 上傳
點(diǎn)擊文件名下載附件
|
|