标签:
</pre><pre name="code" class="cpp"><span style="white-space:pre"> </span>Mat src,dest; <span style="white-space:pre"> </span>Src.copyTo(src); if (src.channels() > 1) { cvtColor(src, src, CV_BGR2GRAY); } MatND hist; const int histSize = 256; float range[] = { 0, 255 }; const float *ranges[] = { range }; const int channels = 0; cv::calcHist(&src, 1, &channels, Mat(), hist, 1, &histSize, ranges); float total = src.size().width* src.size().height; float bins[histSize] = { 0 }; float binsAcc[histSize] = { 0 }; Mat lut(1, 256, CV_8U); vector<float> vectorBins; vector<float> maxBins; float sumBins = 0.0; int countMax = 0; float TValue = 0; // Find the mapping table for (int i = 0; i<histSize; i++) { float bin_val = hist.at<float>(i); // 第i灰度级上的数 bins[i] = bin_val / total; if (bins[i] > 0) { vectorBins.push_back(bins[i]); } if (i>0) { binsAcc[i] =binsAcc[i-1] + bins[i]; } else { binsAcc[0] = bins[0]; } lut.at<uchar>(i) = static_cast<uchar>(cvRound(binsAcc[i] * 255)); }
Mat dest2; equalizeHist(src, dest2); imshow("equlization2", dest2);
Mat src,dest; Src.copyTo(src); if (src.channels() > 1) { cvtColor(src, src, CV_BGR2GRAY); } MatND hist; const int histSize = 256; float range[] = { 0, 255 }; const float *ranges[] = { range }; const int channels = 0; cv::calcHist(&src, 1, &channels, Mat(), hist, 1, &histSize, ranges); float total = src.size().width* src.size().height; float bins[histSize] = { 0 }; float binsAcc[histSize] = { 0 }; Mat lut(1, 256, CV_8U); vector<float> vectorBins; vector<float> maxBins; float sumBins = 0.0; int countMax = 0; float TValue = 0; // Find the mapping table for (int i = 0; i<histSize; i++) { float bin_val = hist.at<float>(i); // 第i灰度级上的数 bins[i] = bin_val / total; if (bins[i] > 0) { vectorBins.push_back(bins[i]); } } // Calculate the Meadin value by 3 sapce for (int i = 1; i < vectorBins.size() - 1; i++) { if (vectorBins[i] < vectorBins[i - 1] && vectorBins[i - 1] < vectorBins[i + 1] || vectorBins[i] > vectorBins[i - 1] && vectorBins[i - 1] > vectorBins[i + 1]) { vectorBins[i] = vectorBins[i - 1]; } else if (vectorBins[i] < vectorBins[i + 1] && vectorBins[i + 1] < vectorBins[i - 1] || vectorBins[i] > vectorBins[i + 1] && vectorBins[i + 1] > vectorBins[i - 1]) { vectorBins[i] = vectorBins[i + 1]; } } // Calculate the max peak value for (int i = 1; i < vectorBins.size() - 1; i++) { if (vectorBins[i] - vectorBins[i - 1] >= 0 && vectorBins[i+1] - vectorBins[i] <= 0) { maxBins.push_back(vectorBins[i]); sumBins += vectorBins[i]; countMax++; } } TValue = sumBins / countMax; // Find the mapping table for (int i = 0; i<histSize; i++) { if (bins[i] > TValue) { bins[i] = TValue; } if (i>0) { binsAcc[i] = binsAcc[i - 1] + bins[i]; } else { binsAcc[0] = bins[0]; } } for (int i = 0; i < histSize; i++) { lut.at<uchar>(i) = static_cast<uchar>(cvRound(binsAcc[i] * 255 / binsAcc[255])); } LUT(src, lut, dest); imshow("src", src); imshow("equlization", dest);
标签:
原文地址:http://blog.csdn.net/fioletfly/article/details/51011399