您的位置:首页 > 编程语言 > MATLAB

图像处理学习笔记之MATLAB中imhist、imadjust、stretchlim函数实现

2017-04-27 14:04 931 查看
vector<int> imhist(Mat &srcImage, unsigned int n = 256)
{
CV_Assert(srcImage.channels() == 1);
vector<int> hist(n, 0);
double a = n / 256.0;
int index = 0;
int rows = srcImage.rows;
int cols = srcImage.cols;
for (int i = 0; i < rows;i++)
{
uchar* pdata = srcImage.ptr<uchar>(i);
for (int j = 0; j < cols;j++)
{
index = a*pdata[j];
++hist[index];
}
}
return hist;
}

void stretchlim(Mat& src, Mat& lowHigh,double tol_low = 0.01, double tol_high = 0.99)
{
CV_Assert(tol_low <= tol_high);

int channelNum = src.channels();
lowHigh.create(channelNum, 2,CV_64F);
int nbins;
if (src.depth() == CV_8U)
{
nbins = 256;
}
else
{
nbins = 65536;
}
//通道分离
vector<Mat> channels;
split(src, channels);

for (int i = 0; i < channelNum; i++)
{
//获取灰度统计信息
double low, high;
auto hist = imhist(channels[i]);
auto toltalSize = std::accumulate(hist.begin(), hist.end(), 0);
//得到 >tol_low的分布概率的灰度等级
for (int j = 0; j < hist.size(); ++j)
{
auto sum = std::accumulate(hist.begin(), hist.begin() + j, 0.0);
if ((sum / toltalSize) > tol_low)  // > tol_low
{
low = j / (double)nbins;
break;
}
}
//得到 >tol_high的分布概率的灰度等级
for (int k = 0; k < hist.size(); ++k)
{
auto sum = std::accumulate(hist.begin(), hist.begin() + k, 0.0);
if ((sum / toltalSize) >= tol_high) // > tol_high
{
high = k / double(nbins);
break;
}
}
if (low==high)
{
lowHigh.ptr<double>(i)[0] = 0;
lowHigh.ptr<double>(i)[1] = 1;
}
else
{
lowHigh.ptr<double>(i)[0] = low;
lowHigh.ptr<double>(i)[1] = high;
}
}
}

void imadjust(Mat& src, Mat& dst,Mat& lowHighIn, Mat&lowHighOut, double gamma=1)
{
CV_Assert(src.data != NULL);

int chl = src.channels();
int rowNum = src.rows;
int colNum = src.cols;

//通道分离
vector<Mat> channels;
split(src, channels);

//设置默认值
if (lowHighIn.data==NULL)
{
lowHighIn=Mat::zeros(chl,2,CV_64F);
for (int i = 0; i < chl; i++)
{
lowHighIn.at<double>(i, 1) = 1;
}
}

if (lowHighOut.data==NULL)
{
lowHighOut = Mat::zeros(chl, 2, CV_64F);
for (int i = 0; i < chl; i++)
{
lowHighOut.at<double>(i, 1) = 1;
}
}
for (int m = 0; m < chl;m++)
{
//gamma校正查表
vector<double> lookuptable(256, 0);
vector<uchar> img(256,0);
for (int i = 0; i < 256; i++)
{
lookuptable[i] = i / 255.0;
if (lookuptable[i]<=lowHighIn.at<double>(m,0))
{
lookuptable[i] = lowHighIn.at<double>(m, 0);
}
if (lookuptable[i] >= lowHighIn.at<double>(m, 1))
{
lookuptable[i] = lowHighIn.at<double>(m, 1);
}
lookuptable[i] = (lookuptable[i] - lowHighIn.at<double>(m, 0)) / (lowHighIn.at<double>(m, 1) - lowHighIn.at<double>(m, 0));
lookuptable[i] = pow(lookuptable[i], gamma);
lookuptable[i] = lookuptable[i] * (lowHighOut.at<double>(m, 1) - lowHighOut.at<double>(m,0))+lowHighOut.at<double>(m,0);
img[i] = lookuptable[i] * 255;
}
for (int j = 0; j < rowNum;j++)
{
for (int k = 0; k < colNum; k++)
{
channels[m].at<uchar>(j, k) = img[channels[m].at<uchar>(j, k)];
}
}
}
merge(channels, dst);
}

int main()
{
Mat srcImage = imread("高圆圆.jpg");
Mat grayImage, dstImage, lowHigh;
cvtColor(srcImage, grayImage, CV_RGB2GRAY);
Mat lh1,lh2;
stretchlim(grayImage, lh1);
imadjust(grayImage,dstImage, lh1, lh2,1);
imshow("处理后的图像", dstImage);
waitKey(0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图像处理 opencv matlab