找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2699|回復(fù): 1
收起左側(cè)

stm32+ov7670攝像頭二值化處理轉(zhuǎn)變?yōu)楹诎?/span>

[復(fù)制鏈接]
ID:352976 發(fā)表于 2019-5-21 19:34 | 顯示全部樓層 |閱讀模式
通過stm32f103ze +ov7670攝像頭  實(shí)現(xiàn)圖像進(jìn)行二值化處理 將圖像轉(zhuǎn)變?yōu)楹诎讏D

單片機(jī)源程序如下:
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "key.h"
  4. #include "sys.h"
  5. #include "lcd.h"
  6. #include "usart.h"         
  7. #include "string.h"
  8. #include "ov7670.h"
  9. #include "tpad.h"
  10. #include "timer.h"
  11. #include "exti.h"
  12. #include "usmart.h"


  13. const u8*LMODE_TBL[5]={"Auto","Sunny","Cloudy","Office","Home"};                                                        //5種光照模式            
  14. const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"};        //7種特效
  15. extern u8 ov_sta;        //在exit.c里 面定義
  16. extern u8 ov_frame;        //在timer.c里面定義       
  17. u32 Xpos, Ypos;
  18. u32 times;
  19. //更新LCD顯示
  20. void camera_refresh(void)
  21. {
  22.         u32 j,x,y;
  23.         u16 color;         
  24.         if(ov_sta)//有幀中斷更新?
  25.         {
  26.                 LCD_Scan_Dir(U2D_L2R);                //從上到下,從左到右  
  27.                 if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,240,320);//將顯示區(qū)域設(shè)置到屏幕中央
  28.                 else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//將顯示區(qū)域設(shè)置到屏幕中央
  29.                 LCD_WriteRAM_Prepare();     //開始寫入GRAM       
  30.                 OV7670_RRST=0;                                //開始復(fù)位讀指針
  31.                 OV7670_RCK_L;
  32.                 OV7670_RCK_H;
  33.                 OV7670_RCK_L;
  34.                 OV7670_RRST=1;                                //復(fù)位讀指針結(jié)束
  35.                 OV7670_RCK_H;
  36.                 for(x = 0; x < 240; x++)
  37.                 for(y = 0; y < 319; y++)
  38.                 {
  39.                         OV7670_RCK_L;
  40.                         color=GPIOC->IDR&0XFF;        //讀數(shù)據(jù)
  41.                         OV7670_RCK_H;
  42.                         color<<=8;  
  43.                         OV7670_RCK_L;
  44.                         color|=GPIOC->IDR&0XFF;        //讀數(shù)據(jù)
  45.                         OV7670_RCK_H;
  46.                         if(y < 320)
  47.                         {
  48.                                 if(color > 0x5000)
  49.                                 {
  50.                                         color = 0xffff;
  51.                                 }
  52.                                 else
  53.                                 {
  54.                                         color = 0x0000;
  55.                                         times++;
  56.                                         if(times >= 3000 && x > 10 && y < 310)
  57.                                         {
  58.                                                         times = 0;
  59.                                                         Xpos = x;
  60.                                                         Ypos = y;
  61.                                                         printf("ZUOBIAO1:(%d, %d)",Xpos,Ypos);                                               
  62.                                         }
  63.                                 }
  64.                         }
  65.                         else
  66.                         {
  67.                                 color = 0xa06b;
  68.                         }


  69.                         LCD->LCD_RAM=color;   
  70.                 }                                                             
  71.                 ov_sta=0;                                        //清零幀中斷標(biāo)記
  72.                 ov_frame++;
  73.                 LCD_Scan_Dir(DFT_SCAN_DIR);        //恢復(fù)默認(rèn)掃描方向
  74.         }
  75. }
  76. void cameraPosition(u8 dire)
  77. {
  78.         u32 x ,y;
  79.         u16 color;         
  80.         u32 Xpos1, Ypos1, Xpos2, Ypos2;
  81.         if(ov_sta == 2)
  82.         {
  83.                 LCD_Scan_Dir(dire);               
  84.                 LCD_SetCursor(0xf0,0x0000);       
  85.                 LCD_WriteRAM_Prepare();            
  86.                 OV7670_RRST=0;                                //開始復(fù)位讀指針
  87.                 OV7670_RCK_L;
  88.                 OV7670_RCK_H;
  89.                 OV7670_RCK_L;
  90.                 OV7670_RRST=1;                                //復(fù)位讀指針結(jié)束
  91.                 OV7670_RCK_H;
  92.                 for(x = 0; x < 240; x++)
  93.                 for(y = 0; y < 319; y++)
  94.                 {
  95.                         OV7670_RCK_L;
  96.                         color=GPIOC->IDR&0XFF;        //讀數(shù)據(jù)
  97.                         OV7670_RCK_H;
  98.                         color<<=8;  
  99.                         OV7670_RCK_L;
  100.                         color|=GPIOC->IDR&0XFF;        //讀數(shù)據(jù)
  101.                         OV7670_RCK_H;
  102.                         if(color > 0x4500)
  103.                         {
  104.                                 color = 0xffff;
  105.                         }
  106.                         else
  107.                         {
  108.                                 color = 0x0000;
  109.                                
  110.                                 times++;
  111.                                 if((times <= 2500) && (x - y > 1))
  112.                                 {
  113.                                                 if(times == 500)
  114.                                         {
  115.                                                 Xpos1 = x;
  116.                                                 Ypos1 = y;
  117.                                                 printf("ZUOBIAO2:%d,%d  ", Xpos1, Ypos1);
  118.                                         }
  119.                                         if(times == 1500)
  120.                                         {
  121.                                                 Xpos2 = x;
  122.                                                 Ypos2 = y;
  123.                                                 printf("ZUOBIAO3:%d, %d ", Xpos2, Ypos2);
  124.                                         }
  125.                                
  126.                                 }
  127.                         }
  128.                         LCD->LCD_RAM=color;
  129.                           
  130.                 }                                                            
  131.                 EXTI_ClearITPendingBit(EXTI_Line7);  
  132.                 ov_sta = 0;
  133.                 ov_frame++;
  134.                 LCD_Scan_Dir(L2R_U2D);
  135.         }
  136. }


  137. int main(void)
  138. {         
  139.         u8 key;
  140.         u8 lightmode=0,saturation=2,contrast=2;
  141.         u8 effect=0;         
  142.         u8 i=0;            
  143.         u8 msgbuf[15];                                //消息緩存區(qū)
  144.         u8 tm=0;

  145.         delay_init();                     //延時(shí)函數(shù)初始化          
  146.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置中斷優(yōu)先級分組為組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
  147.         uart_init(115200);                 //串口初始化為 115200
  148.         usmart_dev.init(72);                //初始化USMART               
  149.         LED_Init();                                          //初始化與LED連接的硬件接口
  150.         KEY_Init();                                        //初始化按鍵
  151.         LCD_Init();                                           //初始化LCD  
  152.         TPAD_Init(6);                                //觸摸按鍵初始化
  153.         POINT_COLOR=RED;                        //設(shè)置字體為紅色
  154.         LCD_ShowString(30,50,200,16,16,"ELITE STM32F103 ^_^");       
  155.         LCD_ShowString(30,70,200,16,16,"OV7670 TEST");       
  156.         LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
  157.         LCD_ShowString(30,110,200,16,16,"2015/1/18");
  158.         LCD_ShowString(30,130,200,16,16,"KEY0:Light Mode");
  159.         LCD_ShowString(30,150,200,16,16,"KEY1:Saturation");
  160.         LCD_ShowString(30,170,200,16,16,"KEY_UP:Contrast");
  161.         LCD_ShowString(30,190,200,16,16,"TPAD:Effects");         
  162.           LCD_ShowString(30,210,200,16,16,"OV7670 Init...");          
  163.         while(OV7670_Init())//初始化OV7670
  164.         {
  165.                 LCD_ShowString(30,210,200,16,16,"OV7670 Error!!");
  166.                 delay_ms(200);
  167.             LCD_Fill(30,210,239,246,WHITE);
  168.                 delay_ms(200);
  169.         }
  170.         LCD_ShowString(30,210,200,16,16,"OV7670 Init OK");
  171.         delay_ms(1500);                   
  172.         OV7670_Light_Mode(lightmode);
  173.         OV7670_Color_Saturation(saturation);
  174.         OV7670_Contrast(contrast);
  175.         OV7670_Special_Effects(effect);         
  176.        
  177.         TIM6_Int_Init(10000,7199);                        //10Khz計(jì)數(shù)頻率,1秒鐘中斷                                                                          
  178.         EXTI8_Init();                                                //使能定時(shí)器捕獲
  179.         OV7670_Window_Set(12,176,240,320);        //設(shè)置窗口          
  180.           OV7670_CS=0;                       
  181.         LCD_Clear(BLACK);                                                          
  182.         while(1)
  183.         {       
  184.                 key=KEY_Scan(0);//不支持連按
  185.                 if(key)
  186.                 {
  187.                         tm=20;
  188.                         switch(key)
  189.                         {                                    
  190.                                 case KEY0_PRES:        //燈光模式Light Mode
  191.                                         lightmode++;
  192.                                         if(lightmode>4)lightmode=0;
  193.                                         OV7670_Light_Mode(lightmode);
  194.                                         sprintf((char*)msgbuf,"%s",LMODE_TBL[lightmode]);
  195.                                         break;
  196.                                 case KEY1_PRES:        //飽和度Saturation
  197.                                         saturation++;
  198.                                         if(saturation>4)saturation=0;
  199.                                         OV7670_Color_Saturation(saturation);
  200.                                         sprintf((char*)msgbuf,"Saturation:%d",(signed char)saturation-2);
  201.                                         break;
  202.                                 case WKUP_PRES:        //對比度Contrast                            
  203.                                         contrast++;
  204.                                         if(contrast>4)contrast=0;
  205.                                         OV7670_Contrast(contrast);
  206.                                         sprintf((char*)msgbuf,"Contrast:%d",(signed char)contrast-2);
  207.                                         break;
  208.                         }
  209.                 }         
  210.                 if(TPAD_Scan(0))//檢測到觸摸按鍵
  211.                 {
  212.                         effect++;
  213.                         if(effect>6)effect=0;
  214.                         OV7670_Special_Effects(effect);//設(shè)置特效
  215.                          sprintf((char*)msgbuf,"%s",EFFECTS_TBL[effect]);
  216.                         tm=20;
  217.                 }
  218.                 camera_refresh();//更新顯示
  219.                 if(tm)
  220.                 {
  221.                         LCD_ShowString((lcddev.width-240)/2+30,(lcddev.height-320)/2+60,200,16,16,msgbuf);
  222.                         tm--;
  223.                 }
  224.                 i++;
  225.                 if(i==15)//DS0閃爍.
  226.                 {
  227.                         i=0;
  228.                         LED0=!LED0;
  229.                 }
  230.         }          
  231. }
復(fù)制代碼

所有資料51hei提供下載:
攝像頭實(shí)驗(yàn).7z (243.3 KB, 下載次數(shù): 40)


回復(fù)

使用道具 舉報(bào)

ID:601030 發(fā)表于 2019-8-17 14:28 | 顯示全部樓層
樓主cameraPosition函數(shù)是干什么的
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表