5/12/2014

(OpenCV) Hue Histogram drawing and get value of RGB and bin count (example source code using calcHist in openCV)

This histogram drawing source code is referenced Sample code of CAMshift in opencv.

We usually use hue color in hsv to get color distribution, because hue is a little bit free from lighting than RGB model.

The source code is example of drawing rgb graph from hue histogram.

I try to drawing color graph from 2 channel histogram, for example hue, saturation, but I failed, I don't know that is impossible or not.

But 1 channel is possible, refer to this source code and fiagure.

If you want to know how to get histogram, refer to this page.
http://feelmare.blogspot.kr/2014/05/open-cv-get-histogram-and-compare-color.html

Thank you.

-----
#include <  stdio.h>
#include <  iostream>
#include <  opencv2\opencv.hpp>


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing
#pragma comment(lib, "opencv_highgui247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#endif 

using namespace cv;
using namespace std;


//get representative 3 color

int main()
{
 
 //read 2 images for histogram comparing
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 Mat imgA;
 Mat imgA_mask;
 imgA = imread(".\\image1.jpg");
 imgA_mask = imread(".\\image1_mask.jpg", 0);

 imshow("img1", imgA);
 imshow("img1_mask", imgA_mask);


 //variables preparing
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 int hbins = 30; 
 int channels[] = {0};
 int histSize[] = {hbins};
 float hranges[] = { 0, 180 };
 const float* ranges[] = { hranges}; 

 Mat patch_HSV;
 MatND HistA, HistB;

 //cal histogram & normalization
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 cvtColor(imgA, patch_HSV, CV_BGR2HSV);
 calcHist( &patch_HSV, 1, channels,  imgA_mask, //MaskForHisto, // // do use mask
     HistA, 1, histSize, ranges,
     true, // the histogram is uniform
     false );
 normalize(HistA, HistA,  0, 255, CV_MINMAX);

 
 
 //Mat for drawing 
 Mat histimg = Mat::zeros(200, 320, CV_8UC3); 
 histimg = Scalar::all(0);
 int binW = histimg.cols / hbins;
 Mat buf(1, hbins, CV_8UC3);
 //Set RGB color
 for( int i = 0; i <  hbins; i++ )
  buf.at< Vec3b>(i) = Vec3b(saturate_cast< uchar>(i*180./hbins), 255, 255);
 cvtColor(buf, buf, CV_HSV2BGR);
 //drawing routine
 for( int i = 0; i <  hbins; i++ )
 {
  int val = saturate_cast< int>(HistA.at< float>(i)*histimg.rows/255);
  
  rectangle( histimg, Point(i*binW,histimg.rows),
   Point((i+1)*binW,histimg.rows - val),
   Scalar(buf.at< Vec3b>(i)), -1, 8 );
  int r,g,b;
  b =  buf.at< Vec3b>(i)[0];
  g =  buf.at< Vec3b>(i)[1];
  r =  buf.at< Vec3b>(i)[2];

  //show bin and RGB value
  printf("[%d] r=%d, g=%d, b=%d , bins = %d \n",i , r, g, b, val);
 }
 imshow( "Histogram", histimg );



 waitKey(0);

 return 0;
}


----

(opencv) get histogram and compare color similarity of 2 images(calcHist, compareHist, example source code)

This is example source code of get Histogram and compare color similarity of 2 images.

To get histogram, we use calcHist function in opencv and use compareHist to comparing.

Generally, when comparing based color, HSV color medel is more accurate then RGB model.

And using 2 channel of Hue, Saturation is better than using only 1 channel. ex) hue

In calcHist parameters, there is mask option. The mask image is black and white image.

When set mask option, calcHist get histogram only white region pixels. So if you knwo foreground of image, comparing value will be better.

First source code is no mask option.
Second source code is mask option.





#include <  stdio.h>
#include <  iostream>
#include <  opencv2\opencv.hpp>

#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing
#pragma comment(lib, "opencv_highgui247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#endif 

using namespace cv;
using namespace std;



int main()
{
 
 //read 2 images for histogram comparing
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 Mat imgA, imgB;
 imgA = imread(".\\image1.jpg");
 imgB = imread(".\\image2.jpg");

 
 imshow("img1", imgA);
 imshow("img2", imgB);


 //variables preparing
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 int hbins = 30, sbins = 32; 
 int channels[] = {0,  1};
 int histSize[] = {hbins, sbins};
 float hranges[] = { 0, 180 };
 float sranges[] = { 0, 255 };
 const float* ranges[] = { hranges, sranges}; 

 Mat patch_HSV;
 MatND HistA, HistB;

 //cal histogram & normalization
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 cvtColor(imgA, patch_HSV, CV_BGR2HSV);
 calcHist( &patch_HSV, 1, channels,  Mat(), // do not use mask
     HistA, 2, histSize, ranges,
     true, // the histogram is uniform
     false );
 normalize(HistA, HistA,  0, 255, CV_MINMAX);


 cvtColor(imgB, patch_HSV, CV_BGR2HSV);
 calcHist( &patch_HSV, 1, channels,  Mat(),// do not use mask
     HistB, 2, histSize, ranges,
     true, // the histogram is uniform
     false );
 normalize(HistB, HistB, 0, 255, CV_MINMAX);
 
 
 
 
 //compare histogram
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //비교하기
 float bc = compareHist(HistA, HistB, CV_COMP_BHATTACHARYYA); 
 printf("(The range of matcing value is 0~1, 1 is best matching, 0 means miss matching\n");
 printf("V = %lf \n", bc);


 waitKey(0);

 return 0;
}



 
 

#include <  stdio.h>
#include <  iostream>
#include <  opencv2\opencv.hpp>

#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing
#pragma comment(lib, "opencv_highgui247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#endif 

using namespace cv;
using namespace std;



int main()
{
 
 //read 2 images for histogram comparing
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 Mat imgA, imgB;
 Mat imgA_mask, imgB_mask;
 imgA = imread(".\\image1.jpg");
 imgB = imread(".\\image2.jpg");
 imgA_mask = imread(".\\image1_mask.jpg", 0);
 imgB_mask = imread(".\\image2_mask.jpg", 0);
 
 imshow("img1", imgA);
 imshow("img2", imgB);
 imshow("img1_mask", imgA_mask);
 imshow("img2_mask", imgB_mask);


 //variables preparing
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 int hbins = 30, sbins = 32; 
 int channels[] = {0,  1};
 int histSize[] = {hbins, sbins};
 float hranges[] = { 0, 180 };
 float sranges[] = { 0, 255 };
 const float* ranges[] = { hranges, sranges}; 

 Mat patch_HSV;
 MatND HistA, HistB;

 //cal histogram & normalization
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 cvtColor(imgA, patch_HSV, CV_BGR2HSV);
 calcHist( &patch_HSV, 1, channels,  imgA_mask, //MaskForHisto, // // do use mask
     HistA, 2, histSize, ranges,
     true, // the histogram is uniform
     false );
 normalize(HistA, HistA,  0, 255, CV_MINMAX);


 cvtColor(imgB, patch_HSV, CV_BGR2HSV);
 calcHist( &patch_HSV, 1, channels,  imgB_mask, //MaskForHisto, // // do use mask
     HistB, 2, histSize, ranges,
     true, // the histogram is uniform
     false );
 normalize(HistB, HistB, 0, 255, CV_MINMAX);
 
 
 
 
 //compare histogram
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //비교하기
 float bc = compareHist(HistA, HistB, CV_COMP_BHATTACHARYYA); 
 printf("(The range of matcing value is 0~1, 1 is best matching, 0 means miss matching\n");
 printf("V = %lf \n", bc);


 waitKey(0);

 return 0;
}