找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

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

自己改的代碼,stm32,ov7670識(shí)別二值化并識(shí)別黑色物體,串口返回坐標(biāo)值

  [復(fù)制鏈接]
ID:320701 發(fā)表于 2018-7-18 15:42 | 顯示全部樓層 |閱讀模式
自己網(wǎng)上找的代碼,根據(jù)要求修改可二值化,識(shí)別黑色物體,用紅色框框框起來,串口返回坐標(biāo)值

單片機(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. #include "EasyTracered.h"


  14. const u8*LMODE_TBL[5]={"Auto","Sunny","Cloudy","Office","Home"};                                                        //5種光照模式            
  15. const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"};        //7種特效
  16. extern u8 ov_sta;        //在exit.c里 面定義
  17. extern u8 ov_frame;        //在timer.c里面定義
  18. u16 r0,r1;
  19. u16 g0,g1;
  20. u16 b0,b1;
  21.         RESULT Resured;//判定為的目標(biāo)條件
  22.         RESULT Resugreen;
  23.         RESULT Resublue;
  24.        

  25. //更新LCD顯示
  26. void camera_refresh(void)//讀取FIFO里面的數(shù)據(jù)
  27. {
  28.         u32 j;
  29.         u16 color;       
  30.   u16  k;       

  31. //  TARGET_CONDI Conditionred={160,160,0,0,240,240,30,30,320,240};        //   API參數(shù)  hsl的閾值,識(shí)別時(shí)用的
  32.     TARGET_CONDI Conditiongreen={0,240,0,240,0,50,30,30,320,240};                //黑色1  API參數(shù)  hls的閾值,識(shí)別時(shí)用的
  33.    
  34.         if(ov_sta)//有幀中斷更新?
  35.         {
  36.                 LCD_Scan_Dir(U2D_L2R);                //從上到下,從左到右  
  37.         if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,960,640);//將顯示區(qū)域設(shè)置到屏幕中央
  38.                  else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window(0,0,320,240);//將顯示區(qū)域設(shè)置到屏幕中央
  39.                 LCD_WriteRAM_Prepare();     //開始寫入GRAM       
  40.                  
  41.                 OV7670_RRST=0;                                //開始復(fù)位讀指針
  42.                 OV7670_RCK_L;
  43.                 OV7670_RCK_H;
  44.                 OV7670_RCK_L;
  45.                 OV7670_RRST=1;                                //復(fù)位讀指針結(jié)束
  46.                 OV7670_RCK_H;
  47.                 for(j=0;j<76800;j++)
  48.                 {
  49.                         OV7670_RCK_L;
  50.                         color=GPIOC->IDR&0XFF;        //讀數(shù)據(jù)
  51.                         OV7670_RCK_H;
  52.                         color<<=8;  
  53.                         OV7670_RCK_L;
  54.                         color|=GPIOC->IDR&0XFF;        //讀數(shù)據(jù)
  55.                         OV7670_RCK_H;
  56.                         k=color>>11;//rrrrrggggggbbbb       
  57.                         if(k>=0x0008)
  58.                         {
  59.                                 color=0xffff;//全白
  60.                         }
  61.                         else
  62.                         {
  63.                                 color=0x0000;//全黑
  64.                         }       
  65.                         LCD->LCD_RAM=color;                          
  66.                 }  
  67.        
  68.                         if(Trace1(&Conditiongreen,&Resugreen) )                      //API
  69.                         {               
  70.                                 LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x+Resugreen.w/2,Resugreen.y-Resugreen.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
  71.                                 LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x-Resugreen.w/2+1,Resugreen.y+Resugreen.h/2,0xf800);
  72.                                 LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y+Resugreen.h/2,Resugreen.x+Resugreen.w/2,Resugreen.y+Resugreen.h/2+1,0xf800);
  73.                                 LCD_Fillgreen(Resugreen.x+Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x+Resugreen.w/2+1,Resugreen.y+Resugreen.h/2,0xf800);
  74.                                 LCD_Fillgreen(Resugreen.x-2,Resugreen.y-2,Resugreen.x+2,Resugreen.y+2,0xf800);               
  75.                                  // LED1=!LED1;
  76.                                   g0=Resugreen.x;
  77.                                   g1=Resugreen.y;
  78.                         }       
  79.                 ov_sta=0;                                        //清零幀中斷標(biāo)記
  80.                 ov_frame++;
  81.                 LCD_Scan_Dir(DFT_SCAN_DIR);        //恢復(fù)默認(rèn)掃描方向
  82.         }
  83. }          


  84. int main(void)
  85. {         
  86.         u8 key;
  87.         u8 lightmode=0,saturation=2,brightness=2,contrast=2;
  88.         u8 effect=6;         
  89.         u8 i=0;            
  90.         u8 msgbuf[15];                                //消息緩存區(qū)
  91.         u8 tm=0;
  92.          
  93.         delay_init();                     //延時(shí)函數(shù)初始化          
  94.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置中斷優(yōu)先級(jí)分組為組2:2位搶占優(yōu)先級(jí),2位響應(yīng)優(yōu)先級(jí)
  95.         uart_init(115200);                 //串口初始化為 115200
  96.         usmart_dev.init(72);                //初始化USMART               
  97.         LED_Init();                                          //初始化與LED連接的硬件接口
  98.         KEY_Init();                                        //初始化按鍵
  99.         LCD_Init();                                           //初始化LCD  
  100.         TPAD_Init(6);                                //觸摸按鍵初始化
  101.         POINT_COLOR=RED;                        //設(shè)置字體為紅色
  102.         LCD_ShowString(30,50,200,16,16,"WarShip STM32");       
  103.         LCD_ShowString(30,70,200,16,16,"OV7670 TEST");       
  104.         LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
  105.         LCD_ShowString(30,110,200,16,16,"2015/1/18");
  106.         LCD_ShowString(30,130,200,16,16,"KEY0:Light Mode");
  107.         LCD_ShowString(30,150,200,16,16,"KEY1:Saturation");
  108.         LCD_ShowString(30,170,200,16,16,"KEY2:Brightness");
  109.         LCD_ShowString(30,190,200,16,16,"KEY_UP:Contrast");
  110.         LCD_ShowString(30,210,200,16,16,"TPAD:Effects");         
  111.   LCD_ShowString(30,230,200,16,16,"OV7670 Init...");          
  112.         while(OV7670_Init())//初始化OV7670
  113.         {
  114.                 LCD_ShowString(30,230,200,16,16,"OV7670 Error!!");
  115.                 delay_ms(200);
  116.             LCD_Fill(30,230,239,246,WHITE);
  117.                 delay_ms(200);
  118.         }
  119.         LCD_ShowString(30,230,200,16,16,"OV7670 Init OK");
  120.         delay_ms(1500);                   
  121.         OV7670_Light_Mode(lightmode);
  122.         OV7670_Color_Saturation(saturation);
  123.         OV7670_Brightness(brightness);
  124.         OV7670_Contrast(contrast);
  125.         OV7670_Special_Effects(effect);         //是特效
  126.         TIM6_Int_Init(10000,7199);                        //10Khz計(jì)數(shù)頻率,1秒鐘中斷                                                                          
  127.         EXTI8_Init();                                                        //使能定時(shí)苃 n韃痘?
  128. //        OV7670_Window_Set(12,176,240,320);        //設(shè)置窗口       
  129.   OV7670_config_window(12,176,240,320);
  130.   OV7670_CS=0;                                       
  131.         LCD_Clear(BLACK);
  132.         while(1)
  133.         {       
  134.                 key=KEY_Scan(0);//不支持連按
  135.                 if(key)
  136.                 {
  137.                         tm=20;
  138.                         switch(key)
  139.                         {                                    
  140.                                 case KEY0_PRES:        //燈光模式Light Mode
  141.                                         lightmode++;
  142.                                         if(lightmode>4)lightmode=0;
  143.                                         OV7670_Light_Mode(lightmode);
  144.                                         sprintf((char*)msgbuf,"%s",LMODE_TBL[lightmode]);
  145.                                         break;
  146.                                 case KEY1_PRES:        //飽和度Saturation
  147.                                         saturation++;
  148.                                         if(saturation>4)saturation=0;
  149.                                         OV7670_Color_Saturation(saturation);
  150.                                         sprintf((char*)msgbuf,"Saturation:%d",(signed char)saturation-2);
  151.                                         break;
  152.                                 case KEY2_PRES:        //亮度Brightness                                 
  153.                                         brightness++;
  154.                                         if(brightness>4)brightness=0;
  155.                                         OV7670_Brightness(brightness);
  156.                                         sprintf((char*)msgbuf,"Brightness:%d",(signed char)brightness-2);
  157.                                         break;
  158.                                 case WKUP_PRES:        //對(duì)比度Contrast                            
  159.                                         contrast++;
  160.                                         if(contrast>4)contrast=0;
  161.                                         OV7670_Contrast(contrast);
  162.                                         sprintf((char*)msgbuf,"Contrast:%d",(signed char)contrast-2);
  163.                                         break;
  164.                         }
  165.                 }         
  166.                 if(TPAD_Scan(0))//檢測(cè)到觸摸按鍵
  167.                 {
  168.                         effect++;
  169.                         if(effect>6)effect=0;
  170.                         OV7670_Special_Effects(effect);//設(shè)置特效
  171.                          sprintf((char*)msgbuf,"%s",EFFECTS_TBL[effect]);
  172.                         tm=20;
  173.                 }
  174.                 camera_refresh();//更新顯示
  175.                
  176.                 if(tm)
  177.                 {
  178.                         LCD_ShowString((lcddev.width-240)/2+30,(lcddev.height-320)/2+60,200,16,16,msgbuf);
  179.                         tm--;
  180.                 }
  181.                 i++;
  182.                 if(i==15)//DS0閃爍.
  183.                 {
  184.                         i=0;
  185.                         LED0=!LED0;
  186.                         printf("x坐標(biāo)=%d\r\n",g0);
  187.                         printf("y坐標(biāo)=%d\r\n",g1);
  188.                 }
  189.         }          
  190. }
復(fù)制代碼

所有資料51hei提供下載:
實(shí)驗(yàn)35 攝像頭實(shí)驗(yàn).rar (394.47 KB, 下載次數(shù): 177)


評(píng)分

參與人數(shù) 2黑幣 +55 收起 理由
Mr丶朋 + 5 共享資料的黑幣獎(jiǎng)勵(lì)!
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:341045 發(fā)表于 2018-7-19 12:01 | 顯示全部樓層
收藏備用,謝謝分享
回復(fù)

使用道具 舉報(bào)

ID:374851 發(fā)表于 2018-7-19 14:47 | 顯示全部樓層
學(xué)習(xí)了
回復(fù)

使用道具 舉報(bào)

ID:383374 發(fā)表于 2018-11-29 13:58 | 顯示全部樓層
不錯(cuò)的例程
回復(fù)

使用道具 舉報(bào)

ID:511417 發(fā)表于 2019-4-13 18:58 | 顯示全部樓層
下載試試
回復(fù)

使用道具 舉報(bào)

ID:524469 發(fā)表于 2019-4-29 18:58 | 顯示全部樓層
試試看
回復(fù)

使用道具 舉報(bào)

ID:466908 發(fā)表于 2019-5-1 10:18 | 顯示全部樓層
請(qǐng)問樓主的ov7670有沒有帶FIFO
回復(fù)

使用道具 舉報(bào)

ID:138247 發(fā)表于 2019-5-2 15:23 | 顯示全部樓層

謝謝樓主分享。。。
回復(fù)

使用道具 舉報(bào)

ID:537523 發(fā)表于 2019-5-13 23:10 | 顯示全部樓層
這個(gè)用ov7725 都需要改哪些程序
回復(fù)

使用道具 舉報(bào)

ID:28208 發(fā)表于 2019-5-19 13:37 | 顯示全部樓層
挺厲害的!
回復(fù)

使用道具 舉報(bào)

ID:542507 發(fā)表于 2019-5-20 02:17 | 顯示全部樓層
前輩你這個(gè)很厲害,比如我電腦屏幕有5種顏色,分別是1 2 3 4 5,假如攝像頭識(shí)別到2號(hào)顏色 就反饋回一個(gè)2號(hào)的信息,這個(gè)信息到我的舵機(jī)控制器里  我控制器再實(shí)現(xiàn)我代碼寫好的動(dòng)作 可以實(shí)現(xiàn)嗎?
回復(fù)

使用道具 舉報(bào)

ID:352976 發(fā)表于 2019-5-23 19:36 | 顯示全部樓層
為什么燒錄到 單片機(jī)上只有白框   沒有圖像啊  
回復(fù)

使用道具 舉報(bào)

ID:572726 發(fā)表于 2019-7-31 16:47 | 顯示全部樓層
其他顏色怎么改呢
回復(fù)

使用道具 舉報(bào)

ID:587606 發(fā)表于 2019-8-5 15:27 | 顯示全部樓層
if(Trace1(&Conditiongreen,&Resugreen) )                      //API
                        {               
                                LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x+Resugreen.w/2,Resugreen.y-Resugreen.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
                                LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x-Resugreen.w/2+1,Resugreen.y+Resugreen.h/2,0xf800);
                                LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y+Resugreen.h/2,Resugreen.x+Resugreen.w/2,Resugreen.y+Resugreen.h/2+1,0xf800);
                                LCD_Fillgreen(Resugreen.x+Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x+Resugreen.w/2+1,Resugreen.y+Resugreen.h/2,0xf800);
                                LCD_Fillgreen(Resugreen.x-2,Resugreen.y-2,Resugreen.x+2,Resugreen.y+2,0xf800);               
                                 // LED1=!LED1;
                                  g0=Resugreen.x;
                                  g1=Resugreen.y;
                        }        

能不能請(qǐng)問下這段代碼的含義?
回復(fù)

使用道具 舉報(bào)

ID:542954 發(fā)表于 2019-8-11 17:03 | 顯示全部樓層
菜雞一只314159 發(fā)表于 2019-5-1 10:18
請(qǐng)問樓主的ov7670有沒有帶FIFO

你看程序里有寫讀取FIFO的函數(shù) 應(yīng)該是帶的
回復(fù)

使用道具 舉報(bào)

ID:585455 發(fā)表于 2019-8-18 23:29 | 顯示全部樓層
只做過arducam ov2640
感謝分享ov7670
回復(fù)

使用道具 舉報(bào)

ID:592325 發(fā)表于 2019-9-10 14:57 | 顯示全部樓層
能問下是用那個(gè)芯片嗎
回復(fù)

使用道具 舉報(bào)

ID:612668 發(fā)表于 2019-10-8 16:19 | 顯示全部樓層
收藏備用,謝謝分享
回復(fù)

使用道具 舉報(bào)

ID:320701 發(fā)表于 2020-5-9 15:46 | 顯示全部樓層
zhang@# 發(fā)表于 2019-9-10 14:57
能問下是用那個(gè)芯片嗎

STM32F103
回復(fù)

使用道具 舉報(bào)

ID:625538 發(fā)表于 2020-7-20 17:01 | 顯示全部樓層
anzhii 發(fā)表于 2019-5-13 23:10
這個(gè)用ov7725 都需要改哪些程序

我也是OV7725 請(qǐng)問能直接用嗎
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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