1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> int main() { cv::Mat srcImage = cv::imread("circles.jpg"); if (!srcImage.data) return 1; cv::Mat srcGray; cvtColor(srcImage, srcGray, cv::COLOR_BGR2GRAY); const int channels[ 1 ] = {0}; const int histSize[ 1 ] = {256}; float hranges[ 2 ] = {0, 255}; const float *ranges[ 1 ] = {hranges}; cv::MatND hist; calcHist(&srcGray, 1, channels, cv::Mat(), hist, 1, histSize, ranges); float table[ 256 ]; int nPix = srcGray.cols * srcGray.rows; for (int i = 0; i < 256; i++) { float temp[ 256 ]; temp[ i ] = hist.at<float>(i) / nPix * 255; if (i != 0) { table[ i ] = table[ i - 1 ] + temp[ i ]; } else { table[ i ] = temp[ i ]; } } cv::Mat lookUpTable(cv::Size(1, 256), CV_8U); for (int i = 0; i < 256; i++) { lookUpTable.at<uchar>(i) = static_cast<uchar>(table[ i ]); } cv::Mat histTransResult; cv::LUT(srcGray, lookUpTable, histTransResult); cv::imshow("srcGray", srcGray); cv::imshow("histTransResult", histTransResult); cv::waitKey(0); return 0; }
|