Example usage for org.opencv.imgproc Imgproc calcBackProject

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

Introduction

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

Prototype

public static void calcBackProject(List<Mat> images, MatOfInt channels, Mat hist, Mat dst, MatOfFloat ranges,
            double scale) 

Source Link

Usage

From source file:classes.ObjectFinder.java

private void backprojectObjectHistogram() {

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

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

    // Getting the pixels that are in te specified ranges    
    int hmin = this.thresholdsVector.get(0);
    int hmax = this.thresholdsVector.get(1);
    int smin = this.thresholdsVector.get(2);
    int smax = this.thresholdsVector.get(3);
    int vmin = this.thresholdsVector.get(4);
    int vmax = this.thresholdsVector.get(5);

    Mat maskImage = new Mat(this.objectImage.size(), CvType.CV_8UC1);
    Core.inRange(hsvImage, new Scalar(hmin, smin, vmin), new Scalar(hmax, smax, vmax), maskImage);

    // Taking the hue channel of the image
    Mat hueImage = new Mat(hsvImage.size(), hsvImage.depth());

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

    // Backprojecting the histogram over that hue channel image
    MatOfFloat ranges = new MatOfFloat(0, 180);
    MatOfInt channels = new MatOfInt(0);

    Imgproc.calcBackProject(Arrays.asList(hueImage), channels, this.objectHistogram, this.backprojectionImage,
            ranges, 1);// w w  w . ja va2 s.  c  om

    Core.bitwise_and(backprojectionImage, maskImage, backprojectionImage);

}

From source file:imageprocess.ObjectFinder.java

public Mat find(final Mat image, MatOfInt channels, MatOfFloat ranges) {

    Mat result = new Mat();

    if (isIsSparse()) { // call the right function based on histogram type

        Imgproc.calcBackProject(Arrays.asList(image), channels, // vector specifying what histogram dimensions belong to what image channels
                ROIHistogram, // the histogram we are using
                result, // the resulting back projection image
                ranges, // the range of values, for each dimension
                255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
        );//from  w w  w  .ja v a  2s.  c o  m

    } else {
        Imgproc.calcBackProject(Arrays.asList(image), channels, // vector specifying what histogram dimensions belong to what image channels
                ROIHistogram, // the histogram we are using
                result, // the resulting back projection image
                ranges, // the range of values, for each dimension
                255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
        );
    }

    // Threshold back projection to obtain a binary image
    Mat thresholded = new Mat(result.rows(), result.cols(), result.type());
    if (getThreshold() > 0.0) {
        Imgproc.threshold(result, thresholded, 255 * getThreshold(), 255, THRESH_BINARY);
    }

    return thresholded;
}

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

From source file:nz.ac.auckland.lablet.vision.CamShiftTracker.java

License:Open Source License

/**
 * Gets the location of an object in a frame. Assumes you have called setRegionOfInterest,
 * which informs CamShiftTracker which object to track.
 *
 * @param frame The frame to search for the object in.
 * @return The location and bounds of the object, represented by a Rect.
 *///from ww w  .java  2s  .c o  m
public Rect getObjectLocation(Bitmap frame) {
    Mat image = new Mat();
    Utils.bitmapToMat(frame, image);

    //        Mat out = new Mat(image.rows(), image.cols(), image.type());
    //        image.convertTo(out, -1, 2.0, 2.0);
    //        image = out;

    toHsv(image, hsvMin, hsvMax);

    ArrayList<Mat> hsvs = new ArrayList<>();
    hsvs.add(hsv);

    Imgproc.calcBackProject(hsvs, new MatOfInt(0), hist, backproj, ranges, 1);
    Core.bitwise_and(backproj, mask, backproj);

    try {
        Rect tempTrackWindow = trackWindow.clone();
        RotatedRect result = Video.CamShift(backproj, trackWindow, termCriteria);

        if (result.size.equals(new Size(0, 0)) && result.angle == 0 && result.center.equals(new Point(0, 0))) {
            trackWindow = tempTrackWindow;
            return null;
        }
    } catch (Exception e) {
        Log.e(TAG, "Shit went down: ", e);
        return null;
    }

    return trackWindow.clone();
}