2
votes

I want to compare two pictures similarity Code:

Mat mat1=Highgui.imread("/mnt/sdcard/91.png");
Mat mat2=Highgui.imread("/mnt/sdcard/92.png");
double distance = Imgproc.compareHist(mat1, mat2, Imgproc.CV_COMP_CORREL); //(this line throws an exception)

Exception information:

01-30 10:48:20.203: E/AndroidRuntime(3540): Caused by: CvException [org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/opencv/modules/imgproc/src/histogram.cpp:1387: error: (-215) H1.type() == H2.type() && H1.type() == CV_32F in function double cv::compareHist(const cv::_InputArray&, const cv::_InputArray&, int)

Can anybody help me? How should I solve this?

4

4 Answers

4
votes

At first make sure that both images have 1 channel (if not, than convert them to grayscale with cvtColor or choose one channel witn cvSplit) and have one type, for instance, CV_8UC1.

Then calculate histograms of this images. Example of code:

int histSize = 180;
float range[] = {0, 180};
const float* histRange = {range};
bool uniform = true;
bool accumulate = false;
cv::Mat hist1, hist2;
cv::calcHist(&mat1, 1, 0, cv::Mat(), hist1, 1, &histSize, &histRange, uniform, accumulate );
cv::calcHist(&mat2, 1, 0, cv::Mat(), hist2, 1, &histSize, &histRange, uniform, accumulate );

double result = cv::compareHist( hist1, hist2, CV_COMP_CORREL); 
2
votes

On Android the code would be similar to:

Mat image0 = ...; // 
Mat image1 = ...;

Mat hist0 = new Mat();
Mat hist1 = new Mat();

int hist_bins = 30;           //number of histogram bins
int hist_range[]= {0,180};//histogram range
MatOfFloat ranges = new MatOfFloat(0f, 256f);
MatOfInt histSize = new MatOfInt(25);

Imgproc.calcHist(Arrays.asList(image0), new MatOfInt(0), new Mat(), hist0, histSize, ranges);
Imgproc.calcHist(Arrays.asList(image1), new MatOfInt(0), new Mat(), hist1, histSize, ranges);

double res = Imgproc.compareHist(image0, image01, Imgproc.CV_COMP_CORREL);
1
votes

@skornos your code is wrong. It should be

Mat image0 = ...; // 
Mat image1 = ...;

Mat hist0 = new Mat();
Mat hist1 = new Mat();

int hist_bins = 30;           //number of histogram bins
int hist_range[]= {0,180};//histogram range
MatOfFloat ranges = new MatOfFloat(0f, 256f);
MatOfInt histSize = new MatOfInt(25);

Imgproc.calcHist(Arrays.asList(image0), new MatOfInt(0), new Mat(), hist0, histSize, ranges);
Imgproc.calcHist(Arrays.asList(image1), new MatOfInt(0), new Mat(), hist1, histSize, ranges);

double res = Imgproc.compareHist(hist0, hist1, Imgproc.CV_COMP_CORREL);

Note the last line, it should be hist0 compare to hist1 not comparing images

0
votes
      Mat hsvRef = new Mat();
      Mat hsvCard = new Mat();

      Mat srcRef = new Mat(refImage.getHeight(), refImage.getWidth(), CvType.CV_8UC2);
      Utils.bitmapToMat(refImage, srcRef);


      Mat srcCard = new Mat(cardImage.getHeight(), cardImage.getWidth(), CvType.CV_8UC2);
      Utils.bitmapToMat(cardImage, srcCard);

      /// Convert to HSV
      Imgproc.cvtColor(srcRef, hsvRef, Imgproc.COLOR_BGR2HSV);
      Imgproc.cvtColor(srcCard, hsvCard, Imgproc.COLOR_BGR2HSV);

      /// Using 50 bins for hue and 60 for saturation
      int hBins = 50;
      int sBins = 60;
      MatOfInt histSize = new MatOfInt( hBins,  sBins);

      // hue varies from 0 to 179, saturation from 0 to 255
      MatOfFloat ranges =  new MatOfFloat( 0f,180f,0f,256f );

      // we compute the histogram from the 0-th and 1-st channels
      MatOfInt channels = new MatOfInt(0, 1);



      Mat histRef = new Mat();
      Mat histCard = new Mat();

      ArrayList<Mat> histImages=new ArrayList<Mat>();
      histImages.add(hsvRef);
      Imgproc.calcHist(histImages,
              channels,
              new Mat(),
              histRef,
              histSize,
              ranges,
              false);
      Core.normalize(histRef,
              histRef,
              0,
              1,
              Core.NORM_MINMAX,
              -1,
              new Mat());

      histImages=new ArrayList<Mat>();
      histImages.add(hsvCard);
      Imgproc.calcHist(histImages,
              channels,
              new Mat(),
              histCard,
              histSize,
              ranges,
              false);

      Core.normalize(histCard,
              histCard,
              0,
              1,
              Core.NORM_MINMAX,
              -1,
              new Mat());

      double resp1 = Imgproc.compareHist(histRef, histCard, 0);
      Log.d(TAG, "HIST COMPARE 0" + resp1);
      double resp2 = Imgproc.compareHist(histRef, histCard, 1);
      Log.d(TAG, "HIST COMPARE 1" + resp2);
      double resp3 = Imgproc.compareHist(histRef, histCard, 2);
      Log.d(TAG, "HIST COMPARE 2" + resp3);
      double resp4 = Imgproc.compareHist(histRef, histCard, 3);
      Log.d(TAG, "HIST COMPARE 3" + resp4);