找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7842|回复: 16
收起左侧

利用ov7670识别红绿蓝物体与定位

  [复制链接]
ID:239323 发表于 2017-10-14 10:25 | 显示全部楼层 |阅读模式
利用ov7670实现物体的简单识别及定位

单片机源程序如下:
  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. /************************************************
  15. ALIENTEK精英STM32开发板实验33
  16. 摄像头OV7670 实验
  17. 作者:正点原子 @ALIENTEK
  18. ************************************************/


  19. const u8*LMODE_TBL[5]={"Auto","Sunny","Cloudy","Office","Home"};                                                        //5种光照模式            
  20. const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"};        //7种特效
  21. extern u8 ov_sta;        //在exit.c里 面定义
  22. extern u8 ov_frame;        //在timer.c里面定义                 
  23. u16 r0,r1;
  24. u16 g0,g1;
  25. u16 b0,b1;
  26. //更新LCD显示
  27. void camera_refresh(void)
  28. {
  29.   
  30.         RESULT Resured;//判定为的目标条件
  31.         RESULT Resugreen;
  32.         RESULT Resublue;
  33.         
  34.   TARGET_CONDI Conditionred={215,240,20,240,30,160,30,30,320,240};        //红色1   API参数  hsl的阈值,识别时用的
  35.   TARGET_CONDI Conditiongreen={70,110,20,240,50,100,30,30,320,240};        //绿色1  API参数  hls的阈值,识别时用的
  36.   TARGET_CONDI Conditionblue={120,160,20,240,30,90,30,30,150,150};        //蓝色1   API参数  hsl的阈值,识别时用的

  37.         u32 j;
  38.          u16 color;         
  39.         if(ov_sta)//有帧中断更新?
  40.         {
  41.                 LCD_Scan_Dir(U2D_L2R);                //从上到下,从左到右  
  42.                 if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,240,320);//将显示区域设置到屏幕中央
  43.                 else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//将显示区域设置到屏幕中央
  44.                 LCD_WriteRAM_Prepare();     //开始写入GRAM        
  45.                 OV7670_RRST=0;                                //开始复位读指针
  46.                 OV7670_RCK_L;
  47.                 OV7670_RCK_H;
  48.                 OV7670_RCK_L;
  49.                 OV7670_RRST=1;                                //复位读指针结束
  50.                 OV7670_RCK_H;
  51.                 for(j=0;j<76800;j++)
  52.                 {
  53.                         OV7670_RCK_L;
  54.                         color=GPIOC->IDR&0XFF;        //读数据
  55.                         OV7670_RCK_H;
  56.                         color<<=8;  
  57.                         OV7670_RCK_L;
  58.                         color|=GPIOC->IDR&0XFF;        //读数据
  59.                         OV7670_RCK_H;
  60.                         LCD->LCD_RAM=color;   
  61.                 }   

  62.      if(Trace(&Conditionred,&Resured) )                      //API
  63.                         {                                
  64.                                 LCD_Fillred(Resured.x-Resured.w/2,Resured.y-Resured.h/2,Resured.x+Resured.w/2,Resured.y-Resured.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
  65.                                 LCD_Fillred(Resured.x-Resured.w/2,Resured.y-Resured.h/2,Resured.x-Resured.w/2+1,Resured.y+Resured.h/2,0xf800);
  66.                                 LCD_Fillred(Resured.x-Resured.w/2,Resured.y+Resured.h/2,Resured.x+Resured.w/2,Resured.y+Resured.h/2+1,0xf800);
  67.                                 LCD_Fillred(Resured.x+Resured.w/2,Resured.y-Resured.h/2,Resured.x+Resured.w/2+1,Resured.y+Resured.h/2,0xf800);
  68.                                 LCD_Fillred(Resured.x-2,Resured.y-2,Resured.x+2,Resured.y+2,0xf800);        
  69.                                   //LED1=!LED1;
  70.                                   r0=Resured.x;
  71.                                   r1=Resured.y;
  72.                         }               
  73.                         if(Trace1(&Conditiongreen,&Resugreen) )                      //API
  74.                         {                                
  75.                                 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
  76.                                 LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x-Resugreen.w/2+1,Resugreen.y+Resugreen.h/2,0xf800);
  77.                                 LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y+Resugreen.h/2,Resugreen.x+Resugreen.w/2,Resugreen.y+Resugreen.h/2+1,0xf800);
  78.                                 LCD_Fillgreen(Resugreen.x+Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x+Resugreen.w/2+1,Resugreen.y+Resugreen.h/2,0xf800);
  79.                                 LCD_Fillgreen(Resugreen.x-2,Resugreen.y-2,Resugreen.x+2,Resugreen.y+2,0xf800);               
  80.                                  // LED1=!LED1;
  81.                                   g0=Resugreen.x;
  82.                                   g1=Resugreen.y;
  83.                         }               
  84.      if(Trace2(&Conditionblue,&Resublue) )                      //API
  85.                         {                                
  86.                                 LCD_Fillblue(Resublue.x-Resublue.w/2,Resublue.y-Resublue.h/2,Resublue.x+Resublue.w/2,Resublue.y-Resublue.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
  87.                                 LCD_Fillblue(Resublue.x-Resublue.w/2,Resublue.y-Resublue.h/2,Resublue.x-Resublue.w/2+1,Resublue.y+Resublue.h/2,0xf800);
  88.                                 LCD_Fillblue(Resublue.x-Resublue.w/2,Resublue.y+Resublue.h/2,Resublue.x+Resublue.w/2,Resublue.y+Resublue.h/2+1,0xf800);
  89.                                 LCD_Fillblue(Resublue.x+Resublue.w/2,Resublue.y-Resublue.h/2,Resublue.x+Resublue.w/2+1,Resublue.y+Resublue.h/2,0xf800);
  90.                                 LCD_Fillblue(Resublue.x-2,Resublue.y-2,Resublue.x+2,Resublue.y+2,0xf800);               
  91.                                  // LED1=!LED1;
  92.                                   b0=Resublue.x;
  93.                                   b1=Resublue.y;
  94.                         }                                       
  95.                  ov_sta=0;                                        //清零帧中断标记
  96.                 ov_frame++;
  97.                 LCD_Scan_Dir(DFT_SCAN_DIR);        //恢复默认扫描方向
  98.         }
  99. }           


  100. int main(void)
  101. {         

  102.         u8 lightmode=0,saturation=2,contrast=2;
  103.         u8 effect=0;         
  104.          u8 i=0;            
  105.         u8 msgbuf[15];                                //消息缓存区
  106.         u8 tm=0;

  107.         delay_init();                     //延时函数初始化         
  108.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  109.         uart_init(115200);                 //串口初始化为 115200
  110.          usmart_dev.init(72);                //初始化USMART               
  111.          LED_Init();                                          //初始化与LED连接的硬件接口
  112.         KEY_Init();                                        //初始化按键
  113.         LCD_Init();                                           //初始化LCD  
  114.         TPAD_Init(6);                                //触摸按键初始化
  115.          POINT_COLOR=RED;                        //设置字体为红色
  116.         LCD_ShowString(30,50,200,16,16,"ELITE STM32F103 ^_^");        
  117.   LCD_ShowString(30,210,200,16,16,"OV7670 Init...");         
  118.         while(OV7670_Init())//初始化OV7670
  119.         {
  120.                 LCD_ShowString(30,210,200,16,16,"OV7670 Error!!");
  121.                 delay_ms(200);
  122.             LCD_Fill(30,210,239,246,WHITE);
  123.                 delay_ms(200);
  124.         }
  125.          LCD_ShowString(30,210,200,16,16,"OV7670 Init OK");
  126.         delay_ms(1500);                    
  127.         OV7670_Light_Mode(lightmode);
  128.         OV7670_Color_Saturation(saturation);
  129.         OV7670_Contrast(contrast);
  130.          OV7670_Special_Effects(effect);         
  131.         
  132.         TIM6_Int_Init(10000,7199);                        //10Khz计数频率,1秒钟中断                                                                          
  133.         EXTI8_Init();                                                //使能定时器捕获
  134.         OV7670_Window_Set(12,176,240,320);        //设置窗口         
  135.           OV7670_CS=0;                        
  136.         LCD_Clear(BLACK);                                                         
  137.          while(1)
  138.         {        
  139.          
  140.                 camera_refresh();//更新显示
  141.                  if(tm)
  142.                 {
  143.                         LCD_ShowString((lcddev.width-240)/2+30,(lcddev.height-320)/2+60,200,16,16,msgbuf);
  144.                         tm--;
  145.                 }
  146.                 i++;
  147.                 if(i==15)//DS0闪烁.
  148.                 {
  149.                         i=0;
  150.                         LED0=!LED0;
  151.                  }
  152.         }           
  153. }

复制代码

所有资料51hei提供下载:
识别红绿蓝并锁定.rar (392.16 KB, 下载次数: 156)
回复

使用道具 举报

ID:221722 发表于 2017-10-15 11:15 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

ID:288639 发表于 2018-3-7 12:42 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

ID:159774 发表于 2018-5-3 10:53 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

ID:322777 发表于 2018-5-5 14:31 | 显示全部楼层
谢谢楼主分享
回复

使用道具 举报

ID:319344 发表于 2018-5-6 11:08 | 显示全部楼层
在正点原子OV7670的基础上做了一些更改,去掉了按键相关部分,main.c的更新显示函数,在照片更新后进行判断,追踪颜色,然后那一块儿就被红或绿或蓝围了一个框,中间还有一个点。屏幕你用的是7寸屏吧?图片是640*480的,真是贫穷限制了我的想象,不过你设置的显示窗口是320*240,我不太理解
内部的那些函数还没看很懂,不过还是学到了些东西,函数的参数&,然后就可以将其值返回,之后可以直接使用,很有用。
可能是我能力还有所欠缺吧,看得很慢,而且因为参数多,注释少,现在还是云里雾里,等我完全看懂后,添加上注释发一份吧,方便大家理解
回复

使用道具 举报

ID:138852 发表于 2018-5-14 17:23 | 显示全部楼层
楼主看懂了吗,有没有注释版,求分享
回复

使用道具 举报

ID:308469 发表于 2018-6-7 23:03 | 显示全部楼层
wenzedong 发表于 2018-5-6 11:08
在正点原子OV7670的基础上做了一些更改,去掉了按键相关部分,main.c的更新显示函数,在照片更新后进行判断 ...

看懂了吗?能解释解释?
回复

使用道具 举报

ID:267330 发表于 2019-2-26 18:38 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

ID:267330 发表于 2019-2-28 17:52 | 显示全部楼层
看懂了吗?能解释解释?
回复

使用道具 举报

ID:267330 发表于 2019-3-5 16:00 | 显示全部楼层
谢谢楼主分享,正在学习
回复

使用道具 举报

ID:138247 发表于 2019-5-2 15:28 | 显示全部楼层

谢谢楼主分享。。
回复

使用道具 举报

ID:199471 发表于 2019-6-14 09:20 | 显示全部楼层
谢谢楼主,好东西啊
回复

使用道具 举报

ID:411625 发表于 2019-8-2 20:09 | 显示全部楼层

感谢楼主分享
回复

使用道具 举报

ID:612668 发表于 2019-10-8 16:23 | 显示全部楼层
感谢楼主分享感谢楼主分享感谢楼主分享
回复

使用道具 举报

ID:620995 发表于 2019-10-9 11:39 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

ID:641287 发表于 2019-12-5 10:08 | 显示全部楼层

你下载这个程序代码可以直接打开工程吗,还是自己建一个
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表