Example usage for org.opencv.core Mat get

List of usage examples for org.opencv.core Mat get

Introduction

In this page you can find the example usage for org.opencv.core Mat get.

Prototype

public double[] get(int row, int col) 

Source Link

Usage

From source file:gov.nasa.jpl.memex.pooledtimeseries.PoT.java

License:Apache License

static void updateOpticalHistogram(double[][][] hist, Mat flow) {
    int d1 = hist.length;
    int d2 = hist[0].length;
    int d3 = hist[0][0].length;

    int step = 4; // 5;

    for (int x = 0; x < frame_width; x += step) {
        int x_type = (int) (x * d1 / frame_width);

        for (int y = 0; y < frame_height; y += step) {
            int y_type = (int) (y * d2 / frame_height);

            Point fxy = new Point(flow.get(y, x));

            double size = (fxy.x + fxy.y) * (fxy.x + fxy.y);

            if (size < 9) {
                continue; // 25
            } else {
                int f_type = opticalFlowType(fxy, d3);

                hist[x_type][y_type][f_type]++;
            }//from w  w  w.j a v a  2 s . com
        }
    }
}

From source file:imageprocess.HistogramProcessor.java

public static Mat getHistogramImage(Mat image) {

    // Compute histogram first
    Mat hist = getGrayHistogram(image);
    // Get min and max bin values

    MinMaxLocResult locPeak = Core.minMaxLoc(hist);
    double maxVal = locPeak.maxVal;
    double minVal = locPeak.minVal;

    // Image on which to display histogram
    Mat histImg = new Mat(image.rows(), image.rows(), CV_8U, new Scalar(255));

    // set highest point at 90% of nbins
    int hpt = (int) (0.9 * 256);

    // Draw vertical line for each bin 
    for (int h = 0; h < 256; h++) {

        double[] f = hist.get(h, 0);
        float binVal = (float) f[0];
        int intensity = (int) (binVal * hpt / maxVal);
        Core.line(histImg, new Point(h, 256.0d), new Point(h, 256.0d - intensity), Scalar.all(0));
    }//w w  w . j  a v a  2s.c  o m
    return histImg;
}

From source file:imageprocess.HistogramProcessor.java

public static Mat stretch(Mat image, int minValue) {
    // Compute histogram first
    Mat hist = getGrayHistogram(image);

    // find left extremity of the histogram
    int imin = 0;
    for (; imin < 256; imin++) {
        System.out.println(String.format("[%d] = %f", imin, hist.get(imin, 0)[0]));
        if (hist.get(imin, 0)[0] > minValue) {
            break;
        }/*from   www  . j  a  va  2s.  c om*/
    }
    // find right extremity of the histogram
    int imax = 255;
    for (; imax >= 0; imax--) {
        if (hist.get(imax, 0)[0] > minValue) {
            break;
        }
    }

    // Create lookup table
    Mat lookup = new Mat(256, 1, CV_8U);

    for (int i = 0; i < 256; i++) {
        if (i < imin) {
            lookup.put(i, 0, 0);
        } else if (i > imax) {
            lookup.put(i, 0, 255);
        } else {
            lookup.put(i, 0, 255.0 * (i - imin) / (imax - imin) + 0.5);
        }
    }
    // Apply lookup table
    Mat result;
    result = applyLookUp(image, lookup);

    return result;
}

From source file:in.fabinpaul.sixthsense.ColorBlobDetectionFragment.java

License:Apache License

private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
    Mat pointMatRgba = new Mat();
    Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
    Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);

    return new Scalar(pointMatRgba.get(0, 0));
}

From source file:javafx1.JavaFX1.java

/**
     * Get the average hue value of the image starting from its Hue channel
     * histogram/*from   w  w w.ja v a  2 s. c  o  m*/
     *
     * @param hsvImg the current frame in HSV
     * @param hueValues the Hue component of the current frame
     * @return the average Hue value
     */
    private double getHistAverage(Mat hsvImg, Mat hueValues) {
        // init
        double average = 0.0;
        Mat hist_hue = new Mat();
        // 0-180: range of Hue values
        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));

        // get the average Hue value of the image
        // (sum(bin(h)*h))/(image-height*image-width)
        // -----------------
        // equivalent to get the hue of each pixel in the image, add them, and
        // divide for the image size (height and width)
        for (int h = 0; h < 180; h++) {
            // for each bin, get its value and multiply it for the corresponding
            // hue
            average += (hist_hue.get(h, 0)[0] * h);
        }

        // return the average hue of the image
        return average = average / hsvImg.size().height / hsvImg.size().width;
    }

From source file:model.JointPDF.java

public JointPDF(Mat imgR, Mat imgO) {
    int x, y;/* w  ww .  ja  v a 2s  . c  om*/
    double count_red, count_green, count_blue, total_red = 0, total_green = 0, total_blue = 0;
    PDF_red = new double[256][256];
    PDF_green = new double[256][256];
    PDF_blue = new double[256][256];

    // Reference Image = x, Other Image = y
    // Make Joint Histogram
    for (int i = 0; i < imgR.rows(); i++) {
        for (int j = 0; j < imgR.cols(); j++) {
            double[] rgbR = imgR.get(i, j);
            double[] rgbO = imgO.get(i, j);

            // Search for Blue PDF
            y = (int) rgbO[0];
            x = (int) rgbR[0];
            PDF_blue[y][x] += 1;

            // Search for Green PDF
            y = (int) rgbO[1];
            x = (int) rgbR[1];
            PDF_green[y][x] += 1;

            // Search for Red PDF
            y = (int) rgbO[2];
            x = (int) rgbR[2];
            PDF_red[y][x] += 1;
        }
    }

    //        System.out.println("ORIGINAL");
    //        for (int i = 0; i < 256; i++) {
    //            for (int j = 0; j < 256; j++) {
    //                if (PDF_blue[i][j] > 0) {
    //                    System.out.println("(" + i + "," + j + "):" + PDF_blue[i][j]);
    //                }
    //            }
    //        }
    // Divide all pixel with Max number of pixel
    for (int i = 0; i < 256; i++) {
        for (int j = 0; j < 256; j++) {
            count_blue = PDF_blue[i][j];
            count_green = PDF_green[i][j];
            count_red = PDF_red[i][j];

            if (count_blue != 0) {
                PDF_blue[i][j] = count_blue / imgR.total();
                total_blue += PDF_blue[i][j];
            }
            if (count_green != 0) {
                PDF_green[i][j] = count_green / imgR.total();
                total_green += PDF_green[i][j];
            }
            if (count_red != 0) {
                PDF_red[i][j] = count_red / imgR.total();
                total_red += PDF_red[i][j];
            }
        }
    }

    // Normalize all pixel so total sum pixel is equal to 1
    for (int i = 0; i < 256; i++) {
        for (int j = 0; j < 256; j++) {
            count_blue = PDF_blue[i][j];
            count_green = PDF_green[i][j];
            count_red = PDF_red[i][j];

            if (count_blue != 0) {
                PDF_blue[i][j] = count_blue / total_blue;
            }
            if (count_green != 0) {
                PDF_green[i][j] = count_green / total_green;
            }
            if (count_red != 0) {
                PDF_red[i][j] = count_red / total_red;
            }
        }
    }
    //        System.out.println("NORMALIZE");
    //        for (int i = 0; i < 256; i++) {
    //            for (int j = 0; j < 256; j++) {
    //                if (PDF_red[i][j] > 0) {
    //                    System.out.println("(" + i + "," + j + "):" + String.format("%.4f",PDF_red[i][j]));
    //                }
    //            }
    //        }
}

From source file:mvision.Bhattacharyya.java

public Mat histogram(String img) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat image = Highgui.imread(img);/*  ww w .ja va 2  s  .  c  om*/

    //Mat image = Highgui.imread("C:\\image1.jpg");

    //Mat src = new Mat(image.height(), image.width(), CvType.CV_8UC2);

    Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2HSV);
    java.util.List<Mat> matList = new LinkedList<Mat>();
    matList.add(image);
    Mat histogram = new Mat();
    MatOfFloat ranges = new MatOfFloat(0, 256);
    MatOfInt histSize = new MatOfInt(255);
    Imgproc.calcHist(matList, new MatOfInt(0), new Mat(), histogram, histSize, ranges);

    // Create space for histogram image
    Mat histImage = Mat.zeros(100, (int) histSize.get(0, 0)[0], CvType.CV_8UC1);

    histogram.convertTo(histogram, CvType.CV_32F);

    // Normalize histogram      
    Core.normalize(histogram, histogram, 1, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
    // Draw lines for histogram points
    for (int i = 0; i < (int) histSize.get(0, 0)[0]; i++) {
        Core.line(histImage, new org.opencv.core.Point(i, histImage.rows()),
                new org.opencv.core.Point(i, histImage.rows() - Math.round(histogram.get(i, 0)[0])),
                new Scalar(255, 255, 255), 1, 8, 0);
    }
    return histogram;

}

From source file:net.bsrc.cbod.opencv.OpenCV.java

/**
 * @param imagePath   name of the orginal image
 * @param mapFilePath name of the orginal image's map file
 * @return/*from w ww . j av a 2  s .co m*/
 */
public static List<Mat> getSegmentedRegions(String imagePath, String mapFilePath, boolean isBlackBg) {

    Mat org = getImageMat(imagePath);
    RegionMap regionMap = RegionMapFactory.getRegionMap(imagePath, mapFilePath);

    List<Mat> result = new ArrayList<Mat>();

    Mat map = regionMap.getMap();

    for (Integer label : regionMap.getLabels()) {

        List<Point> points = new ArrayList<Point>();

        for (int i = 0; i < map.rows(); i++) {
            for (int j = 0; j < map.cols(); j++) {

                double[] temp = map.get(i, j);
                if (temp[0] == label) {
                    // Warning! col=x=j , row=y=i
                    points.add(new Point(j, i));
                }
            }
        }

        Point[] arr = points.toArray(new Point[points.size()]);
        Rect rect = Imgproc.boundingRect(new MatOfPoint(arr));

        Mat region;
        if (isBlackBg) {
            region = getImageWithBlackBg(org, points).submat(rect);
        } else {
            region = org.submat(rect);
        }
        result.add(region);
    }

    return result;
}

From source file:net.bsrc.cbod.opencv.OpenCV.java

/**
 * @param imagePath// w  w w . j av  a2  s .c  o  m
 * @param mapFilePath
 * @param isBlackBg
 * @return
 */
public static List<ImageModel> getSegmentedRegionsAsImageModels(String imagePath, String mapFilePath,
        boolean isBlackBg) {

    Mat org = getImageMat(imagePath);
    RegionMap regionMap = RegionMapFactory.getRegionMap(imagePath, mapFilePath);

    List<ImageModel> result = new ArrayList<ImageModel>();

    Mat map = regionMap.getMap();

    for (Integer label : regionMap.getLabels()) {

        List<Point> points = new ArrayList<Point>();

        for (int i = 0; i < map.rows(); i++) {
            for (int j = 0; j < map.cols(); j++) {

                double[] temp = map.get(i, j);
                if (temp[0] == label) {
                    // Warning! col=x=j , row=y=i
                    points.add(new Point(j, i));
                }
            }
        }

        Point[] arr = points.toArray(new Point[points.size()]);

        Rect rect = null;
        try {
            rect = Imgproc.boundingRect(new MatOfPoint(arr));
        } catch (Exception ex) {
            logger.error("", ex);
            continue;
        }

        Mat region;
        if (isBlackBg) {
            region = getImageWithBlackBg(org, points).submat(rect);
        } else {
            region = org.submat(rect);
        }

        ImageModel imgModel = new ImageModel();
        imgModel.setMat(region);
        imgModel.setRelativeToOrg(rect);

        result.add(imgModel);
    }

    return result;
}

From source file:net.bsrc.cbod.opencv.OpenCV.java

/**
 * Helper method/*from  w  w w  . ja va2s  .c  o m*/
 *
 * @param org
 * @param list
 * @return
 */
private static Mat getImageWithBlackBg(Mat org, List<Point> list) {

    Mat region = Mat.zeros(org.size(), org.type());

    for (Point p : list) {
        int row = (int) p.y;
        int col = (int) p.x;
        region.put(row, col, org.get(row, col));
    }

    return region;
}