(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;
}

Comments

Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

(C, C++) TinyXML , xml read & write

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )