Example usage for org.opencv.imgproc Imgproc compareHist

List of usage examples for org.opencv.imgproc Imgproc compareHist

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc compareHist.

Prototype

public static double compareHist(Mat H1, Mat H2, int method) 

Source Link

Usage

From source file:mvision.Bhattacharyya.java

public double calcCorrelation(String input, String img) {
    return Imgproc.compareHist(histogram(input), histogram(img), 2);
}

From source file:mvision.ChiSquare.java

public double calcCorrelation(String input, String img) {
    return Imgproc.compareHist(histogram(input), histogram(img), 1);
}

From source file:mvision.Correlation.java

public double calcCorrelation(String input, String img) {
    return Imgproc.compareHist(histogram(input), histogram(img), 0);
}

From source file:Recognizer.Recognizer.java

public Image HistMatch(Image imQuery, Image imDB) {
    Image imOut = new Image(352, 288);

    Mat srcQ, srcDB;/*w w  w  . j  a  va  2  s . c o m*/
    Mat hsvQ = new Mat(), hsvDB = new Mat();

    srcQ = imQuery.Image3CtoMat_CV();
    srcDB = imDB.Image3CtoMat_CV();

    //Convert To HSV
    Imgproc.cvtColor(srcQ, hsvQ, Imgproc.COLOR_RGB2HSV);
    Imgproc.cvtColor(srcDB, hsvDB, Imgproc.COLOR_RGB2HSV);

    java.util.List<Mat> matlistQ = Arrays.asList(hsvQ);
    java.util.List<Mat> matlistDB = Arrays.asList(hsvDB);

    //Use 100 bins for hue, 100 for Saturation
    int h_bins = 360, s_bins = 4;
    int[] histsize = { h_bins, s_bins };
    MatOfInt histSize = new MatOfInt(histsize);

    MatOfFloat Ranges = new MatOfFloat(0, 180, 0, 256);

    int[] channels = { 0, 1 };
    MatOfInt CH = new MatOfInt(channels);

    Mat hist_Q = new Mat();
    Mat hist_DB = new Mat();

    Imgproc.calcHist(matlistQ, CH, new Mat(), hist_Q, histSize, Ranges);
    Core.normalize(hist_Q, hist_Q, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    float res;

    Mat[] hsvaLev1 = new Mat[4];
    Mat[] hsvaLev2 = new Mat[16];
    Mat[] hsvaLev3 = new Mat[64];
    // Mat[] hsvaLev4 = new Mat[256];

    float[] iaLev1 = new float[4];
    float[] iaLev2 = new float[16];
    float[] iaLev3 = new float[64];
    //float[] iaLev4 = new float[256];

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            hsvaLev1[i * 2 + j] = hsvDB.submat(0 + i * 288 / 2, 143 + i * 288 / 2, 0 + j * 352 / 2,
                    175 + j * 352 / 2);
        }
    }

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            hsvaLev2[i * 4 + j] = hsvDB.submat(0 + i * 288 / 4, 71 + i * 288 / 4, 0 + j * 352 / 4,
                    87 + j * 352 / 4);
        }
    }

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            hsvaLev3[i * 8 + j] = hsvDB.submat(0 + i * 288 / 8, 35 + i * 288 / 8, 0 + j * 352 / 8,
                    43 + j * 352 / 8);
        }
    }

    System.out.println("Lev_1");
    for (int m = 0; m < 4; m++) {
        matlistDB = Arrays.asList(hsvaLev1[m]);
        Imgproc.calcHist(matlistDB, CH, new Mat(), hist_DB, histSize, Ranges);
        Core.normalize(hist_DB, hist_DB, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        res = (float) Imgproc.compareHist(hist_Q, hist_DB, Imgproc.CV_COMP_BHATTACHARYYA);

        System.out.println("Res: " + res);
        iaLev1[m] = res;
    }

    System.out.println("Lev_2");
    for (int m = 0; m < 16; m++) {
        matlistDB = Arrays.asList(hsvaLev2[m]);
        Imgproc.calcHist(matlistDB, CH, new Mat(), hist_DB, histSize, Ranges);
        Core.normalize(hist_DB, hist_DB, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        res = (float) Imgproc.compareHist(hist_Q, hist_DB, Imgproc.CV_COMP_BHATTACHARYYA);

        System.out.println("Res: " + res);
        iaLev2[m] = res;
    }

    System.out.println("Lev_3");
    for (int m = 0; m < 64; m++) {
        matlistDB = Arrays.asList(hsvaLev3[m]);
        Imgproc.calcHist(matlistDB, CH, new Mat(), hist_DB, histSize, Ranges);
        Core.normalize(hist_DB, hist_DB, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        res = (float) Imgproc.compareHist(hist_Q, hist_DB, Imgproc.CV_COMP_BHATTACHARYYA);

        System.out.println("Res: " + res);
        iaLev3[m] = res;
    }

    int x = MinIndex(iaLev1);
    int i = x % 2;
    int j = x / 2;
    Core.rectangle(srcDB, new Point(0 + j * 352 / 2, 0 + i * 288 / 2),
            new Point(175 + j * 352 / 2, 143 + i * 288 / 2), new Scalar(0, 255, 0));

    x = MinIndex(iaLev2);
    i = x % 4;
    j = x / 4;
    Core.rectangle(srcDB, new Point(0 + j * 352 / 4, 0 + i * 288 / 4),
            new Point(87 + j * 352 / 4, 71 + i * 288 / 4), new Scalar(0, 0, 255));

    x = MinIndex(iaLev3);
    i = x % 8;
    j = x / 8;
    Core.rectangle(srcDB, new Point(0 + j * 352 / 8, 0 + i * 288 / 8),
            new Point(43 + j * 352 / 8, 35 + i * 288 / 8), new Scalar(255, 0, 0));

    imOut.Mat_CVtoImage3C(srcDB);

    return imOut;
}

From source file:Recognizer.Recognizer.java

public Image HistBlockCompare(Image imQuery, Image imDB, int m, int n) // SingleBlock Size mxn -> Eg: 88x72 -> m =88; n = 72
{
    // Initialzations
    Image imOut = new Image(352, 288);

    Mat srcQ, srcDB;/*w w w . ja va 2 s.  co  m*/
    Mat hsvQ = new Mat(), hsvDB = new Mat();

    srcQ = imQuery.Image3CtoMat_CV();
    srcDB = imDB.Image3CtoMat_CV();

    //Convert To HSV
    Imgproc.cvtColor(srcQ, hsvQ, Imgproc.COLOR_RGB2HSV);
    Imgproc.cvtColor(srcDB, hsvDB, Imgproc.COLOR_RGB2HSV);

    java.util.List<Mat> matlistQ = Arrays.asList(hsvQ);
    java.util.List<Mat> matlistDB = Arrays.asList(hsvDB);

    //Use 100 bins for hue, 100 for Saturation
    int h_bins = 180, s_bins = 2;
    int[] histsize = { h_bins, s_bins };
    MatOfInt histSize = new MatOfInt(histsize);

    MatOfFloat Ranges = new MatOfFloat(0, 180, 0, 256);

    int[] channels = { 0, 1 };
    MatOfInt CH = new MatOfInt(channels);

    Mat hist_Q = new Mat();
    Mat hist_DB = new Mat();

    Imgproc.calcHist(matlistQ, CH, new Mat(), hist_Q, histSize, Ranges);
    Core.normalize(hist_Q, hist_Q, 0, 1, Core.NORM_MINMAX, -1, new Mat());

    float[][] CompareHistResult = new float[352 - m][288 - n];

    for (int i = 0; i < (352 - m); i++) // width
    {
        for (int j = 0; j < (288 - n); j++) // height
        {
            // Get Indiaviadua Submatrix for Matching putrposes
            hist_DB = hsvDB.submat(j, (j + n), i, (i + m));
            // Now Compare Histogram using OpenCV functions
            matlistDB = Arrays.asList(hist_DB);
            Imgproc.calcHist(matlistDB, CH, new Mat(), hist_DB, histSize, Ranges);
            Core.normalize(hist_DB, hist_DB, 0, 1, Core.NORM_MINMAX, -1, new Mat());
            CompareHistResult[i][j] = (float) Imgproc.compareHist(hist_Q, hist_DB, Imgproc.CV_COMP_CHISQR);
        }
    }

    // Search min from result
    float min = CompareHistResult[0][0];
    int minIndex_i = 0;
    int minIndex_j = 0;
    for (int i = 0; i < (352 - m); i++) // width
    {
        for (int j = 0; j < (288 - n); j++) // height
        {
            if (CompareHistResult[i][j] < min) {
                min = CompareHistResult[i][j];
                minIndex_i = i;
                minIndex_j = j;
            }
        }
    }
    //
    Core.rectangle(srcDB, new Point(minIndex_i, minIndex_j), new Point(minIndex_i + m, minIndex_j + n),
            new Scalar(0, 255, 0));

    System.out.println("Result: " + CompareHistResult[minIndex_i][minIndex_j]);
    imOut.Mat_CVtoImage3C(srcDB);

    return imOut;
}