ISP算法:gamma矯正

Gamma矯正:在視頻系統,線性光Intensity通過Gamma校正轉換爲非線性的視頻信號,通常在攝像過程內完成。

矯正原因:

起初人們發現,CRT顯示器使用電子顯像管,控制電流大小顯示屏幕上的亮度,電流和亮度之間存在非線性關係,其中gamma值是CRT顯示器的伽馬值:

成像顯示設備,輸入能量和圖片顏色是成線性的。這就導致顯示器顯示的圖像和實際圖像不一致,因此引入了gamma矯正:

通過這一矯正便可以保證顯示器和輸入能量呈線性關係:

整體流程如下圖所示:

 

特別說明:

這是一個美麗的錯誤,因爲攝像機的gamma矯正符合人眼觀察物體的特性:人眼感知中會灰塊,其物理亮度值大約在20%左右,也就說人眼對暗光的色彩分佈更爲敏感。如下圖所示:

而我們在攝像機中的伽瑪矯正恰好將CCD感知到的線性關係圖,矯正成符合人眼感知的非線性關係圖,如下圖所示:

 

注: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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章