Example usage for org.opencv.imgproc Imgproc calcHist

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

Introduction

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

Prototype

public static void calcHist(List<Mat> images, MatOfInt channels, Mat mask, Mat hist, MatOfInt histSize,
            MatOfFloat ranges, boolean accumulate) 

Source Link

Usage

From source file:Retrive.java

public Mat histo(Mat imgs) {
    Vector<Mat> bgr_planes = new Vector<>();
    Core.split(imgs, bgr_planes);//from w ww. j a  v a  2  s . c o  m

    MatOfInt histSize = new MatOfInt(256);
    final MatOfFloat histRange = new MatOfFloat(0f, 256f);
    boolean accumulate = false;
    Mat b_hist = new Mat();
    int hist_w = 512;
    int hist_h = 600;
    //long bin_w;
    //bin_w = Math.round((double) (hist_w / 256));

    Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC3);

    Imgproc.calcHist(bgr_planes, new MatOfInt(0), new Mat(), b_hist, histSize, histRange, accumulate);
    Core.normalize(b_hist, b_hist, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
    return b_hist;
}

From source file:classes.ObjectFinder.java

private void computeObjectHistogram() {
    // Converting the current fram to HSV color space
    Mat hsvImage = new Mat(this.objectImage.size(), CvType.CV_8UC3);

    System.out.println(this.objectImage);

    Imgproc.cvtColor(this.objectImage, hsvImage, Imgproc.COLOR_BGR2HSV);

    // Getting the pixels that are in te specified ranges
    Mat maskImage = new Mat(this.objectImage.size(), CvType.CV_8UC1);
    int hmin = thresholdsVector.get(0);
    int hmax = thresholdsVector.get(1);
    int smin = thresholdsVector.get(2);
    int smax = thresholdsVector.get(3);
    int vmin = thresholdsVector.get(4);
    int vmax = thresholdsVector.get(5);

    Core.inRange(hsvImage, new Scalar(hmin, smin, vmin), new Scalar(hmax, smax, vmax), maskImage);

    Mat hueImage = new Mat(hsvImage.size(), CvType.CV_8UC1);

    MatOfInt fromto = new MatOfInt(0, 0);
    Core.mixChannels(Arrays.asList(hsvImage), Arrays.asList(hueImage), fromto);

    MatOfInt sizes = new MatOfInt(16);
    MatOfFloat ranges = new MatOfFloat(0, 180);
    MatOfInt channels = new MatOfInt(0);

    Mat histogram = new Mat();
    boolean accumulate = false;
    Imgproc.calcHist(Arrays.asList(hueImage), channels, maskImage, histogram, sizes, ranges, accumulate);

    Highgui.imwrite("histogram.png", histogram);

    // The resulting histogram is normalized and placed in the class variable
    Core.normalize(histogram, objectHistogram, 0, 255, Core.NORM_MINMAX);

}

From source file:contador_de_moedas.Reconhecimento.java

public Mat CalculaHistograma(Mat mask, Mat img_Original) {
    // Converte para um tipo de imagem com canais separados
    List<Mat> imagesList = new ArrayList<>();
    imagesList.add(img_Original);/*w w  w  .j a  v a  2 s .  c o  m*/
    // Parametros do calcHist
    Mat histograma = new Mat();
    int h_bins = 30;
    int s_bins = 32;
    MatOfInt mHistSize = new MatOfInt(h_bins, s_bins);
    MatOfFloat mRanges = new MatOfFloat(0, 179, 0, 255);
    MatOfInt mChannels = new MatOfInt(0, 1);
    // Calcula o histograma
    Imgproc.calcHist(imagesList, mChannels, mask, histograma, mHistSize, mRanges, false);
    // Normalizao necessria para ajustar valores
    Core.normalize(histograma, histograma, 0, 255, Core.NORM_MINMAX, -1, new Mat());

    return (histograma);
}

From source file:edu.soict.hust.k57.mmdb.components.HistogramCaculator.java

@Override
public void accept(ImgEnt t) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
    Mat m = Imgcodecs.imread(t.getF().getPath());
    List<Mat> images = new ArrayList<Mat>();
    Core.split(m, images);//from  w  w w.  j a v a  2s .  c  o m

    MatOfInt histSize = new MatOfInt(t.getBin()); // kch thc ca histogram
    MatOfInt channels = new MatOfInt(0); // Knh mu mun tnh
    MatOfFloat histRange = new MatOfFloat(0, 256);

    Mat bHist = new Mat();
    Mat gHist = new Mat();
    Mat rHist = new Mat();

    Imgproc.calcHist(images.subList(0, 1), channels, new Mat(), bHist, histSize, histRange, false);
    Core.normalize(bHist, bHist, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Imgproc.calcHist(images.subList(1, 2), channels, new Mat(), gHist, histSize, histRange, false);
    Core.normalize(gHist, gHist, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Imgproc.calcHist(images.subList(2, 3), channels, new Mat(), rHist, histSize, histRange, false);
    Core.normalize(rHist, rHist, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    t.setbHistogram(bHist);
    t.setgHistogram(gHist);
    t.setrHistogram(rHist);
}

From source file:edu.sust.cse.util.Histogram.java

public static Mat getHistogram(Mat image) {

    try {//from   w ww . j av  a2 s.  com
        Mat src = new Mat(image.height(), image.width(), CvType.CV_8UC2);
        Imgproc.cvtColor(image, src, Imgproc.COLOR_RGB2GRAY);
        ArrayList<Mat> bgr_planes = new ArrayList<>();
        Core.split(src, bgr_planes);

        MatOfInt histSize = new MatOfInt(256);

        final MatOfFloat histRange = new MatOfFloat(0f, 256f);

        boolean accumulate = false;

        Mat b_hist = new Mat();

        Imgproc.calcHist(bgr_planes, new MatOfInt(0), new Mat(), b_hist, histSize, histRange, accumulate);

        int hist_w = 512;
        int hist_h = 600;
        long bin_w;
        bin_w = Math.round((double) (hist_w / 256));

        Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC1);

        Core.normalize(b_hist, b_hist, 3, histImage.rows(), Core.NORM_MINMAX);

        for (int i = 1; i < 256; i++) {

            Core.line(histImage, new Point(bin_w * (i - 1), hist_h - Math.round(b_hist.get(i - 1, 0)[0])),
                    new Point(bin_w * (i), hist_h - Math.round(Math.round(b_hist.get(i, 0)[0]))),
                    new Scalar(255, 0, 0), 2, 8, 0);

        }

        return histImage;
    } catch (Exception ex) {
        System.out.println("[HISTOGRAM][ERROR][" + ex.getMessage() + "]");
        return null;
    }
}

From source file:fuzzycv.MainFrame.java

/**
 * Get the average value of the histogram representing the image Hue
 * component// ww  w. ja  v  a2s .com
 *
 * @param hsvImg
 *            the current frame in HSV
 * @param hueValues
 *            the Hue component of the current frame
 * @return the average value
 */
private double getHistoAvg(Mat hsvImg, Mat hueValues) {
    double average = 0.0;
    Mat hist_hue = new Mat();
    MatOfInt histSize = new MatOfInt(180);
    List<Mat> hue = new ArrayList<>();
    hue.add(hueValues);

    //compute the histogram
    Imgproc.calcHist(hue, new MatOfInt(0), new Mat(), hist_hue, histSize, new MatOfFloat(0, 179), true);
    // get the average for each bin
    for (int h = 0; h < 180; h++) {
        average += (hist_hue.get(h, 0)[0] * h);
    }
    return average = average / hsvImg.size().height / hsvImg.size().width;
}

From source file:info.jmfavreau.bifrostcore.imageprocessing.ImageToColor.java

License:Open Source License

private Mat extract_main_region(Mat img, Mat roi) {
    Mat hist = new Mat();
    int h_bins = 30;
    int s_bins = 32;
    MatOfInt mHistSize = new MatOfInt(h_bins, s_bins);

    MatOfFloat mRanges = new MatOfFloat(0, 179, 0, 255);
    MatOfInt mChannels = new MatOfInt(0, 1);

    Imgproc.calcHist(Arrays.asList(img), mChannels, roi, hist, mHistSize, mRanges, false);

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

    Mat backproj = new Mat();
    Imgproc.calcBackProject(Arrays.asList(img), mChannels, hist, backproj, mRanges, 1);

    Log.w("bifrostcore",
            "Number of pixels in the biggest region: " + String.valueOf(Core.countNonZero(backproj)));
    return backproj.mul(roi);
}