这是我自己写的ov7725二值化程序并能返回圆的圆心,不喜勿喷,希望有大佬有更好的程序分享过来
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "string.h"
- #include "ov7725.h"
- #include "ov7670.h"
- #include "tpad.h"
- #include "timer.h"
- #include "exti.h"
- #include "usmart.h"
- //由于OV7725传感器安装方式原因,OV7725_WINDOW_WIDTH相当于LCD的高度,OV7725_WINDOW_HEIGHT相当于LCD的宽度
- //注意:此宏定义只对OV7725有效
- #define OV7725_WINDOW_WIDTH 320 // <=320
- #define OV7725_WINDOW_HEIGHT 240 // <=240
- const u8*LMODE_TBL[6]={"Auto","Sunny","Cloudy","Office","Home","Night"};//6种光照模式
- const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"}; //7种特效
- extern u8 ov_sta; //在exit.c里 面定义
- extern u8 ov_frame; //在timer.c里面定义
- //更新LCD显示(OV7725)
- void OV7725_camera_refresh(void)
- {
- u32 i,j;
- u16 color;
- double X_FLAG=0.0,Y_FLAG=0.0;
- int NUM_X=0,NUM_Y=0;
- char FlagStatus;
- if(ov_sta)//有帧中断更新
- {
- // u8 R=0x00,G=0x00,B=0x00;
- // u16 RGB_Color=0x0000;
- // float Max=0,Min=0;
- // float Rhsv=0,Ghsv=0,Bhsv=0;
- // float H=0,S=0,V=0;
- //
- //
- // volatile u16 gray;
- // u8 gm_red,gm_green,gm_blue;
- LCD_Scan_Dir(U2D_L2R);//从上到下,从左到右
- LCD_Set_Window((lcddev.width-OV7725_WINDOW_WIDTH)/2,(lcddev.height-OV7725_WINDOW_HEIGHT)/2,OV7725_WINDOW_WIDTH,OV7725_WINDOW_HEIGHT);//将显示区域设置到屏幕中央
- if(lcddev.id==0X1963)
- LCD_Set_Window((lcddev.width-OV7725_WINDOW_WIDTH)/2,(lcddev.height-OV7725_WINDOW_HEIGHT)/2,OV7725_WINDOW_HEIGHT,OV7725_WINDOW_WIDTH);//将显示区域设置到屏幕中央
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- /*******/
- OV7725_RRST=0; //开始复位读指针
- OV7725_RCK_L;
- OV7725_RCK_H;
- OV7725_RCK_L;
- OV7725_RRST=1; //复位读指针结束
- OV7725_RCK_H;
- for(i=0;i<240;i++,FlagStatus=0)
- {
- for(j=0;j<OV7725_WINDOW_WIDTH;j++)
- {
- // OV7725_RCK_L;
- // color=GPIOC->IDR&0XFF; //读数据
- // OV7725_RCK_H;
- // color<<=8;
- // OV7725_RCK_L;
- // color|=GPIOC->IDR&0XFF; //读数据
- // OV7725_RCK_H;
- // R=color&0x001f;
- // G=(color&0x0770)>>5;
- // B=(color&0xf800)>>11;
- // gm_red = (color & 0xF800) >> 8;
- // gm_green = (color & 0x07E0) >> 3;
- // gm_blue = (color & 0x001F) << 3;
- // gray =(gm_red*77+gm_green*150+gm_blue*29+128)/256;
- // gray=gray/8;
- // color=(0x001f&gray)<<11;
- // color=color|(((0x003f)&(gray*2))<<5);
- // color=color|(0x001f&gray);
- /*********/
- if((i>=20&&i<220)&&(j>=20&&j<220))//100*100
- { OV7725_RCK_L;
- color=GPIOC->IDR&0XFF; //YUYV输出读数据 不读第二个字节
- OV7725_RCK_H;
- //color<<=8;
- OV7725_RCK_L;
- //color|=GPIOC->IDR&0XFF; //读数据
- OV7725_RCK_H;
- if(color>0x5F)
- {
- color=0xffff;//白色
- }
- else
- {
- FlagStatus=1;
- NUM_Y++;
- Y_FLAG+=j;
- color=0x0000;//黑色
- }
- LCD->LCD_RAM=color;
- }
- else
- {
- OV7725_RCK_L;
- OV7725_RCK_H;
- OV7725_RCK_L;
- OV7725_RCK_H;
- LCD->LCD_RAM=0xffff;
- }
-
-
-
-
- // LCD->LCD_RAM=color;
- // /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓RGB转HSV算法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
- // Rhsv = (float) R/25/10; //RGB转换成0,1.
- // Ghsv = (float) G/25/10;
- // Bhsv = (float) B/25/10;
- // Max = (Rhsv>Ghsv)?Rhsv:Ghsv;
- // Max = (Max>Bhsv)?Max:Bhsv; //取RGB最大值
- // Min = (Rhsv<Ghsv)?Rhsv:Ghsv;
- // Min = (Min<Bhsv)?Min:Bhsv; //去RGB最小值
- //
- // if(Rhsv==Max) H = (Ghsv-Bhsv)/(Max-Min);
- // if(Ghsv==Max) H = 2+(Bhsv-Rhsv)/(Max-Min);
- // if(Bhsv==Max) H = 4+(Rhsv-Ghsv)/(Max-Min);
- // H =(int) (H*60); //取H的数值
- // if(H<0) H = H+360;
- // V = (Rhsv>Ghsv)?Rhsv:Ghsv;
- // V = (V>Bhsv)?V:Bhsv; //取V的数值
- // S = (Max-Min)/Max; //取S的数值
- // V = (int) (V*100);
- // S = (int) (S*100);
- // /*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑RGB转HSV算法↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
- }
- if(FlagStatus==1)
- {
- NUM_X++;
- X_FLAG+=i;
- }
- }
- Y_FLAG/=NUM_Y;
- X_FLAG/=NUM_X;
- ov_sta=0; //清零帧中断标记
- ov_frame++;
- /****/
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢复默认扫描方向
- printf("X=%dY=%d\r\n",(int)X_FLAG,(int)Y_FLAG); //打印帧率
- POINT_COLOR=WHITE;
- LCD_Draw_Circle((int)X_FLAG,(int)Y_FLAG,2);
- }
- }
- int main(void)
- {
- u8 lightmode=0,effect=0;
- s8 saturation=0,brightness=0,contrast=0;
-
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- LCD_Init(); //初始化LCD
- uart_init(115200);
- OV7725_Init();
- OV7725_Window_Set(OV7725_WINDOW_WIDTH,OV7725_WINDOW_HEIGHT,0);//VGA模式输出
- OV7725_Light_Mode(lightmode);
- OV7725_Color_Saturation(saturation);
- OV7725_Brightness(brightness);
- OV7725_Contrast(contrast);
- OV7725_Special_Effects(effect);
- OV7725_CS=0;
- SCCB_WR_Reg(0xa6, 0x26);
- SCCB_WR_Reg(0x60, 0x80);
- SCCB_WR_Reg(0x61, 0x80);//黑白
- TIM6_Int_Init(10000,7199);
-
- EXTI8_Init(); //使能定时器捕获
- while(1)
- {
- OV7725_camera_refresh();//更新显示
- }
- }
复制代码
所有资料51hei提供下载:
(库函数版本,适合战舰STM32F1开发板)实验35 摄像头实验.rar
(447.82 KB, 下载次数: 124)
|