找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2356|回复: 1
打印 上一主题 下一主题
收起左侧

stm32+ov7670摄像头二值化处理转变为黑白

[复制链接]
跳转到指定楼层
楼主
ID:352976 发表于 2019-5-21 19:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
通过stm32f103ze +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. 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);//将显示区域设置到屏幕中央
  28.                 else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//将显示区域设置到屏幕中央
  29.                 LCD_WriteRAM_Prepare();     //开始写入GRAM       
  30.                 OV7670_RRST=0;                                //开始复位读指针
  31.                 OV7670_RCK_L;
  32.                 OV7670_RCK_H;
  33.                 OV7670_RCK_L;
  34.                 OV7670_RRST=1;                                //复位读指针结束
  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;        //读数据
  41.                         OV7670_RCK_H;
  42.                         color<<=8;  
  43.                         OV7670_RCK_L;
  44.                         color|=GPIOC->IDR&0XFF;        //读数据
  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;                                        //清零帧中断标记
  72.                 ov_frame++;
  73.                 LCD_Scan_Dir(DFT_SCAN_DIR);        //恢复默认扫描方向
  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;                                //开始复位读指针
  87.                 OV7670_RCK_L;
  88.                 OV7670_RCK_H;
  89.                 OV7670_RCK_L;
  90.                 OV7670_RRST=1;                                //复位读指针结束
  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;        //读数据
  97.                         OV7670_RCK_H;
  98.                         color<<=8;  
  99.                         OV7670_RCK_L;
  100.                         color|=GPIOC->IDR&0XFF;        //读数据
  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];                                //消息缓存区
  144.         u8 tm=0;

  145.         delay_init();                     //延时函数初始化          
  146.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  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;                        //设置字体为红色
  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计数频率,1秒钟中断                                                                          
  178.         EXTI8_Init();                                                //使能定时器捕获
  179.         OV7670_Window_Set(12,176,240,320);        //设置窗口          
  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);//设置特效
  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. }
复制代码

所有资料51hei提供下载:
摄像头实验.7z (243.3 KB, 下载次数: 40)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:601030 发表于 2019-8-17 14:28 | 只看该作者
楼主cameraPosition函数是干什么的
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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