标签:
杂谈 |
分类: 图像工程 |
幂律变换的基本形式:
http://s9/middle/002OBL7Wzy6MMOK0yW438&690
其中,c和γ为正常数。注:r和s取值范围[0,1]。
使用幂律变换进行对比度增强;灰度级压缩。
C/C++ Demo:
//通用
#include <iostream>
#include <stdio.h>
//图像操作
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace :: std;
using namespace :: cv;
int main()
{
//图像读取
Mat image = imread("Fig0309_a.tif");
//判断图像读取是否有问题
if(!image.data)
{
cout << "image read is error!" << endl;
return 0;
}
//图像基本信息输出
cout << "image Info:Height:" << image.size().height << " Width:" << image.size().width << endl;
//原始图像显示
namedWindow("Original Image");
imshow("Original Image", image);
//处理图像
//读取gamma分别对应的数表
FILE *fp1;
fp1 = fopen("GammaData_0_95.txt","r");
if(fp1 == NULL) { cout << "File Open Error!" << endl; return 0; }
int gamma[256];
for (int i = 0; i < 256; i++)
{
fscanf(fp1, "%d", &gamma[i]);
}
fclose(fp1);
//gamma变换
int nl = image.rows;
int nc = image.cols * image.channels();
if(image.isContinuous())
{
nc = nc * nl;
nl = 1;
}
int i,j;
uchar *data;
for(j = 0; j < nl; j ++)
{
uchar *data = image.ptr<uchar>(j);
for(i = 0; i < nc; i ++)
{
data[i] = gamma[data[i]];
//cout << data[i];
}
}
cout << endl;
//显示图像
namedWindow("Process Image");
imshow("Process Image", image);
//保存图像
waitKey(0);
return 0;
}
Matlab Demo(用来产生数表):
file1 = fopen('D:\Working\OpenCV\OpenCV\GammaData_0_3.txt', 'wt+');
for i = 0 : 255
value1 = ((i / 256)^0.3) * 256;
if(value1 > 255) value1 = 255; end
fprintf(file1, '%d ', round(value1));
end
fclose(file1);
原图:
Gamma = 3.0:
Gamma = 4.0:
Gamma = 5.0: