from 0 to combat opencv N (6) - histogram equalization

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

from 0 to combat opencv N (6) - histogram equalization

And histogram equalization

1, is a statistical value on the histogram value of each pixel of seeking, such as 0-255 of the image, i.e. the gray histogram statistics that the number of occurrences of each value may be displayed in a histogram.

函数:calcHist( &rgb[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );

parameter:

& Rgb [0]: input array (or set of arrays)

1: The number of the input array (here we use a single-channel image, we can set the input array)

0: the need for statistical channel (dim) index, where we only count the gray (and each array is a single-channel) so long as you write 0 on the line.

Mat (): Mask (0 indicates the pixel is ignored), if not defined, a mask is not used

r_hist: histogram storage matrix

1: rectangular 图维 number

histSize: bin number of each dimension

histRange: the range of each dimension

uniform and accumulate: the same bin size, clear traces of the histogram

 

2, the number of histogram equalization is uniform respective value appears, each image pixel is present are concentrated in some areas, with the histogram equalization can be stretched to a respective region.

函数:equalizeHist( InputArray src, OutputArray dst );

 

3, the histogram statistical tests:

void main()

{

Mat src, dst;

/// 装载图像

src = imread("2.jpg");



/// 分割成3个单通道图像 ( R, G 和 B )

vector<Mat> rgb_planes;

split(src, rgb_planes);

/// 设定bin数目

int histSize = 255;

/// 设定取值范围 ( R,G,B) )

float range[] = { 0, 255 };

const float* histRange = { range };

bool uniform = true; bool accumulate = false;

Mat r_hist, g_hist, b_hist;

/// 计算直方图:

calcHist(&rgb_planes[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);

calcHist(&rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);

calcHist(&rgb_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);



// 创建直方图画布

int hist_w = 400; int hist_h = 400;

int bin_w = cvRound((double)hist_w / histSize);

Mat histImage(hist_w, hist_h, CV_8UC3, Scalar(0, 0, 0));

/// 将直方图归一化到范围 [ 0, histImage.rows ]

normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());



/// 在直方图画布上画出直方图

for (int i = 1; i < histSize; i++)

{

line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))),

Point(bin_w*(i), hist_h - cvRound(r_hist.at<float>(i))),

Scalar(0, 0, 255), 2, 8, 0);

line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(g_hist.at<float>(i - 1))),

Point(bin_w*(i), hist_h - cvRound(g_hist.at<float>(i))),

Scalar(0, 255, 0), 2, 8, 0);

line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))),

Point(bin_w*(i), hist_h - cvRound(b_hist.at<float>(i))),

Scalar(255, 0, 0), 2, 8, 0);

}

// 显示直方图

namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE);

imshow("calcHist Demo", histImage);

waitKey(0);

}

 

 

4, histogram equalization test:

void main()

{

Mat src, dst;



/// 装载图像

src = imread("2.jpg",0);

imshow("src", src);

equalizeHist(src, dst);

imshow("dst", dst);



waitKey(0);

}

 

More attention to micro-channel public number: ML_Study

Guess you like

Origin blog.csdn.net/qq_34106574/article/details/93772108