Last active
March 19, 2018 19:32
-
-
Save MareArts/6a1b3dbc9d14ca4a9f58b858dd0eeb87 to your computer and use it in GitHub Desktop.
RGB histogram using clacHist function in opencv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://study.marearts.com/2017/12/calchist-for-rgb-image-opencv-histogram.html | |
#include "opencv2/opencv.hpp" | |
#include "opencv2\highgui.hpp" | |
#include <iostream> | |
#ifdef _DEBUG | |
#pragma comment(lib, "opencv_core331d.lib") | |
#pragma comment(lib, "opencv_highgui331d.lib") | |
#pragma comment(lib, "opencv_imgcodecs331d.lib") | |
#pragma comment(lib, "opencv_objdetect331d.lib") | |
#pragma comment(lib, "opencv_imgproc331d.lib") | |
#pragma comment(lib, "opencv_videoio331d.lib") | |
#else | |
#pragma comment(lib, "opencv_core331.lib") | |
#pragma comment(lib, "opencv_highgui331.lib") | |
#pragma comment(lib, "opencv_imgcodecs331.lib") | |
#pragma comment(lib, "opencv_objdetect331.lib") | |
#pragma comment(lib, "opencv_imgproc331.lib") | |
#pragma comment(lib, "opencv_videoio331.lib") | |
#endif | |
using namespace std; | |
using namespace cv; | |
int main() | |
{ | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
Mat imgA, grayImg; | |
imgA = imread(".\\goonies.jpg"); //.\\cow.jpg"); // | |
imshow("img1", imgA); | |
//variables preparing | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
int hbins = 255; //histogram x axis size, that is histSize, | |
//ex) 2 -> 0~128, 129~256, ex)16 -> 0 ~ 15, 16 ~ 31..., | |
int channels[] = { 0 }; //index of channel | |
int histSize[] = { hbins }; | |
float hranges[] = { 0, 255 }; | |
const float* ranges[] = { hranges }; | |
MatND HistB, HistG, HistR; | |
//split rgb | |
vector<Mat> bgr_planes; | |
split(imgA, bgr_planes); | |
//cal histogram & normalization | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
calcHist(&bgr_planes[0], 1, 0, Mat(), HistB, 1, histSize, ranges, true, false); | |
calcHist(&bgr_planes[1], 1, 0, Mat(), HistG, 1, histSize, ranges, true, false); | |
calcHist(&bgr_planes[2], 1, 0, Mat(), HistR, 1, histSize, ranges, true, false); | |
normalize(HistB, HistB, 0, 255, CV_MINMAX); | |
normalize(HistG, HistG, 0, 255, CV_MINMAX); | |
normalize(HistR, HistR, 0, 255, CV_MINMAX); | |
// Draw the histograms for B, G and R | |
int hist_w = 500; int hist_h = 255; | |
int ratio = cvRound((double)hist_w / hbins); | |
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0)); | |
int x1, y1; | |
int x2, y2; | |
for (int i = 1; i < hbins; i++) | |
{ | |
x1 = ratio*(i - 1); | |
y1 = hist_h - cvRound(HistB.at<float>(i - 1)); | |
x2 = ratio*(i); | |
y2 = hist_h - cvRound(HistB.at<float>(i)); | |
//Blue | |
line(histImage, Point(x1,y1), Point(x2,y2), | |
CV_RGB(0, 0, 255), 2, 8, 0); | |
//Green | |
y1 = hist_h - cvRound(HistG.at<float>(i - 1)); | |
y2 = hist_h - cvRound(HistG.at<float>(i)); | |
line(histImage, Point(x1, y1), Point(x2, y2), | |
CV_RGB(0, 255, 0), 2, 8, 0); | |
//Red | |
y1 = hist_h - cvRound(HistR.at<float>(i - 1)); | |
y2 = hist_h - cvRound(HistR.at<float>(i)); | |
line(histImage, Point(x1, y1), Point(x2, y2), | |
CV_RGB(255, 0, 0), 2, 8, 0); | |
} | |
/// Display | |
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE); | |
imshow("calcHist Demo", histImage); | |
waitKey(0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://study.marearts.com/2017/12/calchist-for-rgb-image-opencv-histogram.html