STM32 OV7725 传感器

Posted 为了维护世界和平_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32 OV7725 传感器相关的知识,希望对你有一定的参考价值。

OV7725 传感器

OV7725摄像机™ 图像传感器是一种低压CMOS器件,在一个小封装中提供单芯片VGA相机和图像处理器的全部功能。OV7725通过串行摄像机控制总线(SCCB)接口控制,提供多种格式的全帧、亚采样或加窗8位/10位图像。
设备有一个图像阵列,可在VGA中以每秒60帧(fps)的速度运行,用户可完全控制图像质量、格式和输出数据传输。所有必需的图像处理功能,包括曝光控制、伽马、白平衡、色彩饱和度、色调控制等,

特点

  • 低光操作的高灵敏度
  • 标准SCCB接口
  • 对原始RGB、RGB的输出支持(GRB 4:2:2,RGB565/555/444)和YCbCr(4:2:2)格式
  • 支持图像大小:VGA、QVGA和任意大小从CIF降到40x30
  • 自动图像控制功能,包括:自动曝光控制(AEC),自动增益控制(AGC)、自动白平衡(AWB),自动带通滤波器(ABF)和自动黑电平校准(ABLC)
  • 图像质量控制,包括颜色饱和度,色调、gamma、锐度(边缘增强)和抗信号干扰
  • ISP包括降噪和缺陷校正
  • 镜头阴影校正
  • 饱和度水平自动调整(UV调整)
  • 边缘增强级别自动调整
  • 降噪水平自动调整
  • 帧同步能力

    模块功能框图
项目Value
Image Sensor Arraytotal array of 656 x 488 pixels, with active pixels 640 x 480 in YUV mode
Analog Signal Processor模拟信号处理,包括自动增益控制(AGC)、 自动白平衡(AWB)
A/D Converters数字黑电平矫正,可选择U/V通道延时;附加模数范围控制
Test Pattern Generator8-bar color bar 模数
Digital Signal Processor (DSP)该块控制从原始数据到RGB的插值以及一些图像质量控制。•边缘增强(二维高通滤波器)•颜色空间转换器(可将原始数据更改为RGB或YUV/YCbCr)•RGB矩阵消除颜色串扰•色调和饱和度控制•可编程伽马控制•将10位数据传输到8位
Image Scaler(图像裁剪控制发送图像之前所需的所有输出和数据格式。此块将YUV/RGB输出从VGA缩放到CIF,并且几乎可以调整CIF下的任何大小
Timing Generator通常,正时发生器控制以下功能:•阵列控制和帧生成•内部定时信号生成和分配•帧速率定时•自动曝光控制(AEC)•外部定时输出(VSYNC、HREF/HSYNC和PCLK)
Digital Video Port
SCCB Interface类似于I2C

1、选择输出格式

OV7725有四种输出格式:YCbCr, RGB565/RGB555, Bayer raw RGB and Processed raw RGB

LCD驱动程序的数据格式始终为RGB。例如,RGB444、RGB565、RGB555、RGB888等。数据格式和内存接口始终是压缩的。压缩数据是从YCbCr数据压缩而来的。因此,RGB和YCbCr数据都需要在后端芯片内部。

后端芯片内部分为三种
1)全ISP
可以应用传感器功能,如缺陷像素校正、镜头校正、伽马、颜色矩阵、去噪、锐度、BLC等。
2)YCbCr ISP
仅仅支持YCbCr。ISP可以将YCbCr转换为RGB格式用于LCD显示,并压缩YCbCr用于存储
3)没有ISP
格式转换用软件来做

YCbCr to RGB24
Y = 0.299R + 0.587G + 0.114B
Cb = 0.568(B-Y) + 128 = –0.172R – 0.339G + 0.511B + 128
Cr = 0.713(R-Y) + 128 = 0.511R – 0.428G – 0.083B + 128
Y = ((77 * R + 150 * G + 29 * B) >> 8);
Cb = ((-43 * R - 85 * G + 128 * B) >> 8) + 128;
Cr = ((128 * R - 107 * G - 21 * B) >> 8) + 128;
RGB24 to YCbCr
R = Y + 1.371(Cr – 128)
G = Y – 0.698(Cr – 128) – 0.336(Cb – 128)
B = Y + 1.732(Cb – 128)
R = Y + (351*(Cr – 128)) >> 8
G = Y – (179*(Cr – 128) + 86*(Cb – 128))>>8
B = Y + (443*(Cb – 128)) >> 8

2、选择输出分辨率

1)如果有ISP
可以做裁剪,OV7725输出仅仅VGA格式,ISP将VGA图像缩放到移动设备所需的其他分辨率。
2)没有ISP
必须使用OV7725的LCD定标器来精确缩放输出分辨率,使其与LCD大小一致

3、帧率调整

对于60Hz光环境,建议帧速率为30fps和15fps;对于50Hz光环境,建议帧速率为25fps和14.3fps。夜间模式的帧速率较低 Frame Rate Adjustment for 24Mhz、26Mhz 13Mhz input clock

30 fps, PCLK = 24Mhz

SCCB_salve_Address = 0x42;
write_SCCB(0x11, 0x01);
write_SCCB(0x0d, 0x41);
write_SCCB(0x2a, 0x00);
write_SCCB(0x2b, 0x00);
write_SCCB(0x33, 0x00);
write_SCCB(0x34, 0x00);
write_SCCB(0x2d, 0x00);
write_SCCB(0x2e, 0x00);
write_SCCB(0x0e, 0x65);

4、夜间模式

有两种设置,一种是固定帧率,另外一种是自动帧率
For 24Mhz/26Mhz Clock Input

3.75fps night mode for 60Hz light environment
SCCB_salve_Address = 0x42;
write_SCCB(0x11, 0x0f);
write_SCCB(0x0e, 0x65);

3.125fps night mode for 50Hz light environment
SCCB_salve_Address = 0x42;
write_SCCB(0x11, 0x0f);
write_SCCB(0x0e, 0x65);

For 24Mhz/26Mhz Clock Input

15fps ~ 3.75fps night mode for 60Hz light environment
SCCB_salve_Address = 0x42;
write_SCCB(0x11, 0x03);
write_SCCB(0x0e, 0xe5);

25fps ~ 3.125fps night mode for 50Hz light environment
SCCB_salve_Address = 0x42;
write_SCCB(0x11, 0x01);
write_SCCB(0x0e, 0xf5);

5、消除光带

Not n/100, has band = n/100, no band

办公室灯光的强度并不均匀。它随交流频率而变化。例如,如果交流频率为50Hz,则灯光强度在100hz时发生变化。通过将曝光设置为n/100(n/120持续60Hz)秒,光带被移除。

Banding Filter Setting for 24Mhz Input Clock

30fps for 60Hz light frequency
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xff); //banding filter enable
write_SCCB(0x22, 0x7f); //60Hz banding filter
write_SCCB(0x23, 0x03); //4 step for 60hz

15fps for 60Hz light frequency
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xff); //banding filter enable
write_SCCB(0x22, 0x3f); //60Hz banding filter
write_SCCB(0x23, 0x07); //8 step for 60hz

25fps for 50Hz light frequency
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xff); //banding filter enable
write_SCCB(0x22, 0x98); //50Hz banding filter
write_SCCB(0x23, 0x03); //4 step for 50hz

光带不能去除的情况
正常情况下,光带是可以通过带状滤波片去除。
但也有一些特殊情况,如太阳光和办公室光混合,荧光灯拍照,光带无法去除。原因是50hz光环境的曝光时间小于1/100秒,60hz光环境的曝光时间小于1/120秒,因此无法移除光带。

6、白平衡

1)简单白平衡
简单白平衡假设为“灰色世界”。这意味着世界的平均颜色是灰色的。
优点:简单的白平衡不依赖于镜头。简单白平衡的一般设置可适用于具有不同镜头的所有模块。
缺点:在“灰色世界”不真实的情况下,颜色不准确。例如,背景有巨大的红色、蓝色或绿色等。前景的颜色会不准确。如果相机的目标颜色单一(如红、蓝、绿)简单的白平衡会使单色变成灰色。

Settings
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xff); //AWB on
write_SCCB(0x69, 0x5d); // Simple AWB

2)高级白平衡
高级白平衡使用色温信息检测白色区域并进行白平衡。
优点:颜色比简单白平衡更加准确,甚至在单一背景情况下,相机也不会使单色变成灰色。
缺点:高级白平衡设置取决于镜头。必须为每个带有新镜头的模块调整设置。调整必须由光学实验室的光学设备进行。

白平衡模式如何选择?
通常,对于低分辨率相机模块,如CIF、VGA和1.3M,选择简单AWB。对于高分辨率相机模块,如2M、3M,选择高级AWB。

7、缺陷像素矫正

缺陷像素包括死像素和伤像素。

死像素包括白死像素和黑死像素。白色死像素始终是白色的,无论实际图片是亮还是暗。黑死像素始终为黑色,无论实际图片是亮还是暗。

受伤的像素可能会随着光线而变化,但不会像正常像素那样变化。白色受伤像素比普通像素亮得多,但不是完全白色。黑色受伤像素比正常像素暗得多,但不是完全黑色。

OV7725具有内置的缺陷像素校正功能。如果OV7725输出YCbCr、RGB565、处理后的原始RGB,则可以启用缺陷像素校正功能来修复缺陷像素。但如果使用Bayer原始RGB,则无法使用传感器的缺陷像素校正功能。应使用后端芯片的缺陷像素校正。

8、黑电平矫正

黑电平校准(BLC)的功能是在图像的黑暗区域产生准确的颜色。OV7725内置自动BLC功能。它应该总是打开的。

9、视频模式

视频模式需要高帧速率,通常固定为15fps。视频模式没有夜间模式。

10、数字缩减

如果OV7725输出的图像小于QVGA,则可能支持数字变焦。例如
VGA不支持数码变焦
QVGA 1x,2x
QVGA 1x、2x、4x
QCIF 1x,1.8x
QQCIF 1x、2x、3.6x

OV7725 函数

1、光模式

Auto
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xff); //AWB on
write_SCCB(0x0e, 0x65); 
write_SCCB(0x2d, 0x00);
write_SCCB(0x2e, 0x00);

Sunny
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xfd); //AWB off
write_SCCB(0x01, 0x5a);
write_SCCB(0x02, 0x5c);
write_SCCB(0x0e, 0x65); 
write_SCCB(0x2d, 0x00);
write_SCCB(0x2e, 0x00);

Cloudy
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xfd); //AWB off
write_SCCB(0x01, 0x58);
write_SCCB(0x02, 0x60);
write_SCCB(0x0e, 0x65); 
write_SCCB(0x2d, 0x00);
write_SCCB(0x2e, 0x00);

Office(办公室模式)
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xfd); //AWB off
write_SCCB(0x01, 0x84);
write_SCCB(0x02, 0x4c);
write_SCCB(0x0e, 0x65); 
write_SCCB(0x2d, 0x00);
write_SCCB(0x2e, 0x00);

Home 家模式
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xfd); //AWB off
write_SCCB(0x01, 0x96);
write_SCCB(0x02, 0x40);
write_SCCB(0x0e, 0x65); 
write_SCCB(0x2d, 0x00);
write_SCCB(0x2e, 0x00);

Night(夜晚模式)
SCCB_salve_Address = 0x42;
write_SCCB(0x13, 0xff); //AWB on
write_SCCB(0x0e, 0xe5);

2、颜色饱和度

OV7725的色彩饱和度可以调整。高色彩饱和度会使图片看起来更生动,但副作用是噪音更大,肤色不准确。

Saturation + 4
SCCB_salve_Address = 0x42;
write_SCCB(0xa7, 0x80);
write_SCCB(0xa8, 0x80);

Saturation + 3
SCCB_salve_Address = 0x42;
write_SCCB(0xa7, 0x70);
write_SCCB(0xa8, 0x70);

Saturation + 2
SCCB_salve_Address = 0x42;
write_SCCB(0xa7, 0x60);
write_SCCB(0xa8, 0x60);

Saturation + 1
SCCB_salve_Address = 0x42;
write_SCCB(0xa7, 0x50);
write_SCCB(0xa8, 0x50);

Saturation 0
SCCB_salve_Address = 0x42;
write_SCCB(0xa7, 0x40);
write_SCCB(0xa8, 0x40);

Saturation -1
SCCB_salve_Address = 0x42;
write_SCCB(0xa7, 0x30);
write_SCCB(0xa8, 0x30);

Saturation - 2
SCCB_salve_Address = 0x42;
write_SCCB(0xa7, 0x20);
write_SCCB(0xa8, 0x20);

Saturation - 3
SCCB_salve_Address = 0x42;
write_SCCB(0xa7, 0x10);
write_SCCB(0xa8, 0x10);

Saturation - 4
SCCB_salve_Address = 0x42;
write_SCCB(0xa7, 0x00);
write_SCCB(0xa8, 0x00);

3、亮度

OV7725的亮度可以调整。亮度越高,图片越亮。高亮度的副作用是图片看起来有泡沫

Brightness +4
SCCB_salve_Address = 0x42;
write_SCCB(0x9b, 0x48);
write_SCCB(0xab, 0x06);

Brightness +3
SCCB_salve_Address = 0x42;
write_SCCB(0x9b, 0x38);
write_SCCB(0xab, 0x06);

Brightness +2
SCCB_salve_Address = 0x42;
write_SCCB(0x9b, 0x28);
write_SCCB(0xab, 0x06);

Brightness +1
SCCB_salve_Address = 0x42;
write_SCCB(0x9b, 0x18);
write_SCCB(0xab, 0x06);

Brightness 0
SCCB_salve_Address = 0x42;
write_SCCB(0x9b, 0x08);
write_SCCB(0xab, 0x06);

Brightness -1
SCCB_salve_Address = 0x42;
write_SCCB(0x9b, 0x08);
write_SCCB(0xab, 0x0e);

Brightness -2
SCCB_salve_Address = 0x42;
write_SCCB(0x9b, 0x18);
write_SCCB(0xab, 0x0e);

Brightness -3
SCCB_salve_Address = 0x42;
write_SCCB(0x9b, 0x28);
write_SCCB(0xab, 0x0e);

Brightness -4
SCCB_salve_Address = 0x42;
write_SCCB(0x9b, 0x38);
write_SCCB(0xab, 0x0e);

4、对比度

OV7725的对比度可以调整。对比度越高,图像越清晰。但副作用是失去了动态范围

Contrast +4
SCCB_salve_Address = 0x42;
write_SCCB(0x9c, 0x30);

Contrast +3
SCCB_salve_Address = 0x42;
write_SCCB(0x9c, 0x2c);

Contrast +2
SCCB_salve_Address = 0x42;
write_SCCB(0x9c, 0x28);

Contrast +1
SCCB_salve_Address = 0x42;
write_SCCB(0x9c, 0x24);

Contrast 0
SCCB_salve_Address = 0x42
write_SCCB(0x9c, 0x20);

Contrast -1
SCCB_salve_Address = 0x42;
write_SCCB(0x9c, 0x1c);

Contrast -2
SCCB_salve_Address = 0x42;
write_SCCB(0x9c, 0x18);

Contrast -3
SCCB_salve_Address = 0x42;
write_SCCB(0x9c, 0x14);

Contrast -4
SCCB_salve_Address = 0x42;
write_SCCB(0x9c, 0x10);

5、特效

OV7725支持一些特殊效果,如黑白、负片、深褐色、蓝色、redish、绿色等。如果用户需要其他特殊效果,应该由后端芯片支持

Normal
SCCB_salve_Address = 0x42;
write_SCCB(0xa6, 0x06);
write_SCCB(0x60, 0x80);
write_SCCB(0x61, 0x80);

B&W
SCCB_salve_Address = 0x42;
write_SCCB(0xa6, 0x26);
write_SCCB(0x60, 0x80);
write_SCCB(0x61, 0x80);

Bluish
SCCB_salve_Address = 0x42;
write_SCCB(0xa6, 0x1e);
write_SCCB(0x60, 0xa0);
write_SCCB(0x61, 0x40);

Sepia
SCCB_salve_Address = 0x42;
write_SCCB(0xa6, 0x1e);
write_SCCB(0x60, 0x40);
write_SCCB(0x61, 0xa0);

Redish
SCCB_salve_Address = 0x42;
write_SCCB(0xa6, 0x1e);
write_SCCB(0x60, 0x80);
write_SCCB(0x61, 0xc0);

Greenish
SCCB_salve_Address = 0x42;
write_SCCB(0xa6, 0x1e);
write_SCCB(0x60, 0x60);
write_SCCB(0x61, 0x60);

Negative
SCCB_salve_Address = 0x42;
write_SCCB(0xa6, 0x46);

OV7725 设置参考

13.1 YCbCr
//OV7720, YCbCr, VGA
//15fps at 24MHz input clock, 4x maximum gain
//2/27/2007
write_SCCB(0x12, 0x80); // Reset
Delay(2ms);
write_SCCB(0x3d, 0x03);
write_SCCB(0x17, 0x22);
write_SCCB(0x18, 0xa4);
write_SCCB(0x19, 0x07);
write_SCCB(0x1a, 0xf0);
write_SCCB(0x32, 0x00);
write_SCCB(0x29, 0xa0);
write_SCCB(0x2c, 0xf0);
write_SCCB(0x2a, 0x00);
write_SCCB(0x11, 0x01);
write_SCCB(0x42, 0x7f);
write_SCCB(0x4d, 0x09);
write_SCCB(0x63, 0xe0);
write_SCCB(0x64, 0xff);
write_SCCB(0x65, 0x20);
write_SCCB(0x66, 0x00);
write_SCCB(0x67, 0x48);
write_SCCB(0x13, 0xf0);
write_SCCB(0x0d, 0x41); // 0x51/0x61/0x71 for different AEC/AGC window
write_SCCB(0x0f, 0xc5);
write_SCCB(0x14, 0x11);
write_SCCB(0x22, 0x7f); 
write_SCCB(0x23, 0x03);
write_SCCB(0x24, 0x40)
write_SCCB(0x25, 0x30);
write_SCCB(0x26, 0xa1);
write_SCCB(0x2b, 0x00);
write_SCCB(0x6b, 0xaa);
write_SCCB(0x13, 0xff);
write_SCCB(0x90, 0x05);
write_SCCB(0x91, 0x01);
write_SCCB(0x92, 0x03);
write_SCCB(0x93, 0x00);
write_SCCB(0x94, 0xb0);
write_SCCB(0x95, 0x9d);
write_SCCB(0x96, 0x13);
write_SCCB(0x97, 0x16);
write_SCCB(0x98, 0x7b);
write_SCCB(0x99, 0x91);
write_SCCB(0x9a, 0x1e);
write_SCCB(0x9b, 0x08);
write_SCCB(0x9c, 0x20);
write_SCCB(0x9e, 0x81);
write_SCCB(0xa6, 0x06);
//Gamma
write_SCCB(0x7e, 0x0c);
write_SCCB(0x7f, 0x16);
write_SCCB(0x80, 0x2a);
write_SCCB(0x81, 0x4e);
write_SCCB(0x82, 0x61);
write_SCCB(0x83, 0x6f);
write_SCCB(0x84, 0x7b);
write_SCCB(0x85, 0x86);
write_SCCB(0x86, 0x8e);
write_SCCB(0x87, 0x97);
write_SCCB(0x88, 0xa4);
write_SCCB(0x89, 0xaf);
write_SCCB(0x8a, 0xc5);
write_SCCB(0x8b, 0xd7);
write_SCCB(0x8c, 0xe8);
write_SCCB(0x8d, 0x20);
// for 30 fps, 60Hz
write_SCCB(0x33, 0x00); 
write_SCCB(0x22, 0x7f);
write_SCCB(0x23, 0x03);
// for 25 fps, 50Hz
//write_SCCB(0x33, 0x66);
//write_SCCB(0x22, 0x99);
//write_SCCB(0x23, 0x03);
// Lens Correction, should be tuned with real camera module
write_SCCB(0x4a, 0x10);
write_SCCB(0x49, 0x10);
write_SCCB(0x4b, 0x14);
write_SCCB(0x4c, 0x17);
write_SCCB(0x46, 0x05);
write_SCCB(0x0e, 0x65);

以上是关于STM32 OV7725 传感器的主要内容,如果未能解决你的问题,请参考以下文章

VerilogFPGA驱动Ov7670/Ov7725搭建视频通路(RGB565灰度图)

VerilogFPGA驱动Ov7670/Ov7725搭建视频通路(RGB565灰度图)

VerilogFPGA驱动Ov7670/Ov7725搭建视频通路(RGB565灰度图)

关于stm32摄像头二值化的问题

OV7725学习

OV7725初始化配置