通過stm32f103ze +ov7670攝像頭 實現圖像進行二值化處理 將圖像轉變為黑白圖
單片機源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "string.h"
- #include "ov7670.h"
- #include "tpad.h"
- #include "timer.h"
- #include "exti.h"
- #include "usmart.h"
- const u8*LMODE_TBL[5]={"Auto","Sunny","Cloudy","Office","Home"}; //5種光照模式
- const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"}; //7種特效
- extern u8 ov_sta; //在exit.c里 面定義
- extern u8 ov_frame; //在timer.c里面定義
- u32 Xpos, Ypos;
- u32 times;
- //更新LCD顯示
- void camera_refresh(void)
- {
- u32 j,x,y;
- u16 color;
- if(ov_sta)//有幀中斷更新?
- {
- LCD_Scan_Dir(U2D_L2R); //從上到下,從左到右
- if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,240,320);//將顯示區域設置到屏幕中央
- else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//將顯示區域設置到屏幕中央
- LCD_WriteRAM_Prepare(); //開始寫入GRAM
- OV7670_RRST=0; //開始復位讀指針
- OV7670_RCK_L;
- OV7670_RCK_H;
- OV7670_RCK_L;
- OV7670_RRST=1; //復位讀指針結束
- OV7670_RCK_H;
- for(x = 0; x < 240; x++)
- for(y = 0; y < 319; y++)
- {
- OV7670_RCK_L;
- color=GPIOC->IDR&0XFF; //讀數據
- OV7670_RCK_H;
- color<<=8;
- OV7670_RCK_L;
- color|=GPIOC->IDR&0XFF; //讀數據
- OV7670_RCK_H;
- if(y < 320)
- {
- if(color > 0x5000)
- {
- color = 0xffff;
- }
- else
- {
- color = 0x0000;
- times++;
- if(times >= 3000 && x > 10 && y < 310)
- {
- times = 0;
- Xpos = x;
- Ypos = y;
- printf("ZUOBIAO1:(%d, %d)",Xpos,Ypos);
- }
- }
- }
- else
- {
- color = 0xa06b;
- }
- LCD->LCD_RAM=color;
- }
- ov_sta=0; //清零幀中斷標記
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢復默認掃描方向
- }
- }
- void cameraPosition(u8 dire)
- {
- u32 x ,y;
- u16 color;
- u32 Xpos1, Ypos1, Xpos2, Ypos2;
- if(ov_sta == 2)
- {
- LCD_Scan_Dir(dire);
- LCD_SetCursor(0xf0,0x0000);
- LCD_WriteRAM_Prepare();
- OV7670_RRST=0; //開始復位讀指針
- OV7670_RCK_L;
- OV7670_RCK_H;
- OV7670_RCK_L;
- OV7670_RRST=1; //復位讀指針結束
- OV7670_RCK_H;
- for(x = 0; x < 240; x++)
- for(y = 0; y < 319; y++)
- {
- OV7670_RCK_L;
- color=GPIOC->IDR&0XFF; //讀數據
- OV7670_RCK_H;
- color<<=8;
- OV7670_RCK_L;
- color|=GPIOC->IDR&0XFF; //讀數據
- OV7670_RCK_H;
- if(color > 0x4500)
- {
- color = 0xffff;
- }
- else
- {
- color = 0x0000;
-
- times++;
- if((times <= 2500) && (x - y > 1))
- {
- if(times == 500)
- {
- Xpos1 = x;
- Ypos1 = y;
- printf("ZUOBIAO2:%d,%d ", Xpos1, Ypos1);
- }
- if(times == 1500)
- {
- Xpos2 = x;
- Ypos2 = y;
- printf("ZUOBIAO3:%d, %d ", Xpos2, Ypos2);
- }
-
- }
- }
- LCD->LCD_RAM=color;
-
- }
- EXTI_ClearITPendingBit(EXTI_Line7);
- ov_sta = 0;
- ov_frame++;
- LCD_Scan_Dir(L2R_U2D);
- }
- }
- int main(void)
- {
- u8 key;
- u8 lightmode=0,saturation=2,contrast=2;
- u8 effect=0;
- u8 i=0;
- u8 msgbuf[15]; //消息緩存區
- u8 tm=0;
- delay_init(); //延時函數初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設置中斷優先級分組為組2:2位搶占優先級,2位響應優先級
- uart_init(115200); //串口初始化為 115200
- usmart_dev.init(72); //初始化USMART
- LED_Init(); //初始化與LED連接的硬件接口
- KEY_Init(); //初始化按鍵
- LCD_Init(); //初始化LCD
- TPAD_Init(6); //觸摸按鍵初始化
- POINT_COLOR=RED; //設置字體為紅色
- LCD_ShowString(30,50,200,16,16,"ELITE STM32F103 ^_^");
- LCD_ShowString(30,70,200,16,16,"OV7670 TEST");
- LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
- LCD_ShowString(30,110,200,16,16,"2015/1/18");
- LCD_ShowString(30,130,200,16,16,"KEY0:Light Mode");
- LCD_ShowString(30,150,200,16,16,"KEY1:Saturation");
- LCD_ShowString(30,170,200,16,16,"KEY_UP:Contrast");
- LCD_ShowString(30,190,200,16,16,"TPAD:Effects");
- LCD_ShowString(30,210,200,16,16,"OV7670 Init...");
- while(OV7670_Init())//初始化OV7670
- {
- LCD_ShowString(30,210,200,16,16,"OV7670 Error!!");
- delay_ms(200);
- LCD_Fill(30,210,239,246,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(30,210,200,16,16,"OV7670 Init OK");
- delay_ms(1500);
- OV7670_Light_Mode(lightmode);
- OV7670_Color_Saturation(saturation);
- OV7670_Contrast(contrast);
- OV7670_Special_Effects(effect);
-
- TIM6_Int_Init(10000,7199); //10Khz計數頻率,1秒鐘中斷
- EXTI8_Init(); //使能定時器捕獲
- OV7670_Window_Set(12,176,240,320); //設置窗口
- OV7670_CS=0;
- LCD_Clear(BLACK);
- while(1)
- {
- key=KEY_Scan(0);//不支持連按
- if(key)
- {
- tm=20;
- switch(key)
- {
- case KEY0_PRES: //燈光模式Light Mode
- lightmode++;
- if(lightmode>4)lightmode=0;
- OV7670_Light_Mode(lightmode);
- sprintf((char*)msgbuf,"%s",LMODE_TBL[lightmode]);
- break;
- case KEY1_PRES: //飽和度Saturation
- saturation++;
- if(saturation>4)saturation=0;
- OV7670_Color_Saturation(saturation);
- sprintf((char*)msgbuf,"Saturation:%d",(signed char)saturation-2);
- break;
- case WKUP_PRES: //對比度Contrast
- contrast++;
- if(contrast>4)contrast=0;
- OV7670_Contrast(contrast);
- sprintf((char*)msgbuf,"Contrast:%d",(signed char)contrast-2);
- break;
- }
- }
- if(TPAD_Scan(0))//檢測到觸摸按鍵
- {
- effect++;
- if(effect>6)effect=0;
- OV7670_Special_Effects(effect);//設置特效
- sprintf((char*)msgbuf,"%s",EFFECTS_TBL[effect]);
- tm=20;
- }
- camera_refresh();//更新顯示
- if(tm)
- {
- LCD_ShowString((lcddev.width-240)/2+30,(lcddev.height-320)/2+60,200,16,16,msgbuf);
- tm--;
- }
- i++;
- if(i==15)//DS0閃爍.
- {
- i=0;
- LED0=!LED0;
- }
- }
- }
復制代碼
所有資料51hei提供下載:
攝像頭實驗.7z
(243.3 KB, 下載次數: 40)
2019-5-21 23:40 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|