Skip to content

Commit 154de76

Browse files
authoredAug 27, 2019
Add files via upload
1 parent e2267f9 commit 154de76

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
 

‎histogram.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "opencv2/highgui.hpp"
2+
#include "opencv2/imgcodecs.hpp"
3+
#include "opencv2/imgproc.hpp"
4+
#include <iostream>
5+
6+
using namespace std;
7+
using namespace cv;
8+
int main(int argc, char** argv)
9+
{
10+
Mat src = imread(argv[1]);
11+
if( src.empty() )
12+
{
13+
printf("No image found");
14+
return -1;
15+
}
16+
17+
vector<Mat> bgr_planes;
18+
split( src, bgr_planes );
19+
int histSize = 256;
20+
float range[] = { 0, 256 }; //the upper boundary is exclusive
21+
const float* histRange = { range };
22+
bool uniform = true, accumulate = false;
23+
Mat b_hist, g_hist, r_hist;
24+
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
25+
calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
26+
calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
27+
int hist_w = 512, hist_h = 400;
28+
int bin_w = cvRound( (double) hist_w/histSize );
29+
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
30+
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
31+
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
32+
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
33+
for( int i = 1; i < histSize; i++ )
34+
{
35+
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ),
36+
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
37+
Scalar( 255, 0, 0), 2, 8, 0 );
38+
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ),
39+
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
40+
Scalar( 0, 255, 0), 2, 8, 0 );
41+
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ),
42+
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
43+
Scalar( 0, 0, 255), 2, 8, 0 );
44+
}
45+
imshow("Source image", src );
46+
imshow("calcHist Demo", histImage );
47+
waitKey();
48+
return 0;
49+
}

0 commit comments

Comments
 (0)
Please sign in to comment.