图像处理学习笔记之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; }
相关文章推荐
- 【Matlab图像处理】学习笔记:cat函数合成RGB 图像
- matlab图像处理函数学习——图像或者矩阵填充函数:padarray
- opencv2函数学习之blur,GaussianBlur,medianBlur和bilateralFilter:实现图像平滑处理
- Matlab图像处理学习笔记(二):基于颜色的图像分割
- Matlab图像处理学习笔记(一):二值化、开操作、连通区域提取、重心、ROI
- Matlab图像处理学习笔记(六):基于sift特征点的人民币识别
- matlab图像处理学习笔记(1):matlab中double、im2double、mat2gray区别
- 【转】matlab图像处理学习笔记-数学形态与二值图像操
- Matlab图像处理学习笔记(五):Harris角点检测
- Matlab图像处理学习笔记(四):多边形检测
- Matlab图像处理学习笔记(三):基于匹配的目标识别
- 【Matlab图像处理】学习笔记:提取图片的R,G,B分量
- 关于MATLAB图像处理学习笔记20070929
- Matlab图像处理学习笔记(四):多边形检测
- Matlab图像处理学习笔记(三):基于匹配的目标识别
- 2014年2月27日 Matlab 相关图像处理函数学习
- MATLAB图像处理学习笔记之一
- matlab图像处理学习笔记-数学形态与二值图像操作
- matlab 数字图像处理 intrans函数 学习笔记
- Matlab图像处理学习笔记(三):基于匹配的目标识别