找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5636|回复: 19
收起左侧

自己改的代码,stm32,ov7670识别二值化并识别黑色物体,串口返回坐标值

  [复制链接]
ID:320701 发表于 2018-7-18 15:42 | 显示全部楼层 |阅读模式
自己网上找的代码,根据要求修改可二值化,识别黑色物体,用红色框框框起来,串口返回坐标值

单片机源程序如下:
  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;//判定为的目标条件
  22.         RESULT Resugreen;
  23.         RESULT Resublue;
  24.        

  25. //更新LCD显示
  26. void camera_refresh(void)//读取FIFO里面的数据
  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参数  hsl的阈值,识别时用的
  32.     TARGET_CONDI Conditiongreen={0,240,0,240,0,50,30,30,320,240};                //黑色1  API参数  hls的阈值,识别时用的
  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);//将显示区域设置到屏幕中央
  38.                  else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window(0,0,320,240);//将显示区域设置到屏幕中央
  39.                 LCD_WriteRAM_Prepare();     //开始写入GRAM       
  40.                  
  41.                 OV7670_RRST=0;                                //开始复位读指针
  42.                 OV7670_RCK_L;
  43.                 OV7670_RCK_H;
  44.                 OV7670_RCK_L;
  45.                 OV7670_RRST=1;                                //复位读指针结束
  46.                 OV7670_RCK_H;
  47.                 for(j=0;j<76800;j++)
  48.                 {
  49.                         OV7670_RCK_L;
  50.                         color=GPIOC->IDR&0XFF;        //读数据
  51.                         OV7670_RCK_H;
  52.                         color<<=8;  
  53.                         OV7670_RCK_L;
  54.                         color|=GPIOC->IDR&0XFF;        //读数据
  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;                                        //清零帧中断标记
  80.                 ov_frame++;
  81.                 LCD_Scan_Dir(DFT_SCAN_DIR);        //恢复默认扫描方向
  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];                                //消息缓存区
  91.         u8 tm=0;
  92.          
  93.         delay_init();                     //延时函数初始化          
  94.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  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;                        //设置字体为红色
  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计数频率,1秒钟中断                                                                          
  127.         EXTI8_Init();                                                        //使能定时苃 n鞑痘?
  128. //        OV7670_Window_Set(12,176,240,320);        //设置窗口       
  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:        //对比度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))//检测到触摸按键
  167.                 {
  168.                         effect++;
  169.                         if(effect>6)effect=0;
  170.                         OV7670_Special_Effects(effect);//设置特效
  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坐标=%d\r\n",g0);
  187.                         printf("y坐标=%d\r\n",g1);
  188.                 }
  189.         }          
  190. }
复制代码

所有资料51hei提供下载:
实验35 摄像头实验.rar (394.47 KB, 下载次数: 175)

评分

参与人数 2黑币 +55 收起 理由
Mr丶朋 + 5 共享资料的黑币奖励!
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:341045 发表于 2018-7-19 12:01 | 显示全部楼层
收藏备用,谢谢分享
回复

使用道具 举报

ID:374851 发表于 2018-7-19 14:47 | 显示全部楼层
学习了
回复

使用道具 举报

ID:383374 发表于 2018-11-29 13:58 | 显示全部楼层
不错的例程
回复

使用道具 举报

ID:511417 发表于 2019-4-13 18:58 | 显示全部楼层
下载试试
回复

使用道具 举报

ID:524469 发表于 2019-4-29 18:58 | 显示全部楼层
试试看
回复

使用道具 举报

ID:466908 发表于 2019-5-1 10:18 | 显示全部楼层
请问楼主的ov7670有没有带FIFO
回复

使用道具 举报

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

谢谢楼主分享。。。
回复

使用道具 举报

ID:537523 发表于 2019-5-13 23:10 | 显示全部楼层
这个用ov7725 都需要改哪些程序
回复

使用道具 举报

ID:28208 发表于 2019-5-19 13:37 | 显示全部楼层
挺厉害的!
回复

使用道具 举报

ID:542507 发表于 2019-5-20 02:17 | 显示全部楼层
前辈你这个很厉害,比如我电脑屏幕有5种颜色,分别是1 2 3 4 5,假如摄像头识别到2号颜色 就反馈回一个2号的信息,这个信息到我的舵机控制器里  我控制器再实现我代码写好的动作 可以实现吗?
回复

使用道具 举报

ID:352976 发表于 2019-5-23 19:36 | 显示全部楼层
为什么烧录到 单片机上只有白框   没有图像啊  
回复

使用道具 举报

ID:572726 发表于 2019-7-31 16:47 | 显示全部楼层
其他颜色怎么改呢
回复

使用道具 举报

ID:587606 发表于 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;
                        }        

能不能请问下这段代码的含义?
回复

使用道具 举报

ID:542954 发表于 2019-8-11 17:03 | 显示全部楼层
菜鸡一只314159 发表于 2019-5-1 10:18
请问楼主的ov7670有没有带FIFO

你看程序里有写读取FIFO的函数 应该是带的
回复

使用道具 举报

ID:585455 发表于 2019-8-18 23:29 | 显示全部楼层
只做過arducam ov2640
感謝分享ov7670
回复

使用道具 举报

ID:592325 发表于 2019-9-10 14:57 | 显示全部楼层
能问下是用那个芯片吗
回复

使用道具 举报

ID:612668 发表于 2019-10-8 16:19 | 显示全部楼层
收藏备用,谢谢分享
回复

使用道具 举报

ID:320701 发表于 2020-5-9 15:46 | 显示全部楼层
zhang@# 发表于 2019-9-10 14:57
能问下是用那个芯片吗

STM32F103
回复

使用道具 举报

ID:625538 发表于 2020-7-20 17:01 | 显示全部楼层
anzhii 发表于 2019-5-13 23:10
这个用ov7725 都需要改哪些程序

我也是OV7725 请问能直接用吗
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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