ISP算法:gamma矫正

Gamma矫正:在视频系统,线性光Intensity通过Gamma校正转换为非线性的视频信号,通常在摄像过程内完成。

矫正原因:

起初人们发现,CRT显示器使用电子显像管,控制电流大小显示屏幕上的亮度,电流和亮度之间存在非线性关系,其中gamma值是CRT显示器的伽马值:

成像显示设备,输入能量和图片颜色是成线性的。这就导致显示器显示的图像和实际图像不一致,因此引入了gamma矫正:

通过这一矫正便可以保证显示器和输入能量呈线性关系:

整体流程如下图所示:

ISP算法:gamma矫正_第1张图片 

特别说明:

这是一个美丽的错误,因为摄像机的gamma矫正符合人眼观察物体的特性:人眼感知中会灰块,其物理亮度值大约在20%左右,也就说人眼对暗光的色彩分布更为敏感。如下图所示:

ISP算法:gamma矫正_第2张图片

而我们在摄像机中的伽玛矫正恰好将CCD感知到的线性关系图,矫正成符合人眼感知的非线性关系图,如下图所示:

 ISP算法:gamma矫正_第3张图片

注:Gamma矫正的数值为1/2.2,大约为0.45

代码实现:

#include "stdafx.h"
#include "stdio.h"
#include "opencv2\\opencv.hpp"

using namespace std;
using namespace cv;
IplImage* isp_GammaCorrect(IplImage* pImg)
{
	int nWidth = pImg->width;
	int nHight = pImg->height;

	IplImage* pImg_out = cvCreateImage(CvSize(nWidth, nHight), IPL_DEPTH_8U, 3);

	// 建立查询表
	float gamma_val = 0.45;
	unsigned char LUT[256];
	for (int i = 0; i < 256; i++)
	{
		LUT[i] = (unsigned char)(pow((float)(i / 255.0), gamma_val)*(float)255.0);
		//printf("i:%d LUT[%d]:%f\n", i, i, (float)LUT[i]);
	}

	// 遍历图像修改每一个通道上对应像素灰度值	
	for (int r = 0; r < nHight; r++)
	{
		for (int c = 0; c < nWidth; c++)
		{
			pImg_out->imageData[r*nWidth * 3 + c * 3 + 0] = LUT[(uchar)pImg->imageData[r*nWidth * 3 + c * 3 + 0]];
			pImg_out->imageData[r*nWidth * 3 + c * 3 + 1] = LUT[(uchar)pImg->imageData[r*nWidth * 3 + c * 3 + 1]];
			pImg_out->imageData[r*nWidth * 3 + c * 3 + 2] = LUT[(uchar)pImg->imageData[r*nWidth * 3 + c * 3 + 2]];
		}
	}
	return pImg_out;
}
int main()
{
	char* img_path = "..\\lena.jpg";
	IplImage* pImg = cvLoadImage(img_path);
	if (NULL == pImg)
	{
		printf("read image error!\n");
		getchar();
	}
	IplImage* pImgOut = isp_GammaCorrect(pImg);
	cvNamedWindow("pImg", 0);
	cvShowImage("pImg", pImg);
	cvNamedWindow("pImgOut", 0);
	cvShowImage("pImgOut", pImgOut);
	cvWaitKey(0);

	return 0;
}

说明:在代码里用到了查表,这一应用在计算机视觉中通常还会做归一化预处理时使用,目的是消除阴影。

参考资料:

http://www.360doc.com/content/16/1020/14/496343_599870460.shtml

http://blog.csdn.net/ycc541/article/details/42263577

http://blog.csdn.net/candycat1992/article/details/46228771

https://www.cambridgeincolour.com/tutorials/gamma-correction.htm

https://www.cnblogs.com/whw19818/p/5766038.html



你可能感兴趣的:(图像处理)