Example usage for org.opencv.core MatOfInt get

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

Introduction

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

Prototype

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

Source Link

Usage

From source file:OCV_ConvexHull.java

License:Open Source License

private void showData(MatOfPoint pts, MatOfInt hull) {
    // set the ResultsTable
    ResultsTable rt = OCV__LoadLibrary.GetResultsTable(true);

    int num_hull = (int) hull.size().height;
    float[] xPoints = new float[num_hull];
    float[] yPoints = new float[num_hull];

    for (int i = 0; i < num_hull; i++) {
        int index = (int) hull.get(i, 0)[0];
        xPoints[i] = (float) pts.get(index, 0)[0];
        yPoints[i] = (float) pts.get(index, 0)[1];

        rt.incrementCounter();// w  w  w.j av  a 2 s  .c  o  m
        rt.addValue("X", xPoints[i]);
        rt.addValue("Y", yPoints[i]);
    }

    rt.show("Results");

    // set the ROI
    RoiManager roiMan = OCV__LoadLibrary.GetRoiManager(true, true);
    PolygonRoi proi = new PolygonRoi(xPoints, yPoints, Roi.POLYGON);
    roiMan.addRoi(proi);
}

From source file:edu.fiu.cate.breader.BaseSegmentation.java

/**
 * Finds the bounding box for the book on the stand using 
 * the high resolution image.//from w w w  .  j a  v a  2  s  .c om
 * @param src- High Resolution image of the book
 * @return Rectangle delineating the book
 */
public Rect highRes(Mat src) {
    Mat dst = src.clone();
    Imgproc.blur(src, dst, new Size(100.0, 100.0), new Point(-1, -1), Core.BORDER_REPLICATE);
    Imgproc.threshold(dst, dst, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
    Imgproc.Canny(dst, dst, 50, 200, 3, false);

    List<MatOfPoint> contours = new LinkedList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE,
            new Point(0, 0));

    Mat color = new Mat();
    Imgproc.cvtColor(src, color, Imgproc.COLOR_GRAY2BGR);
    for (int k = 0; k < contours.size(); k++) {
        byte[] vals = ITools.getHeatMapColor((float) k / (float) contours.size());
        Imgproc.drawContours(color, contours, k, new Scalar(vals[0], vals[1], vals[2]), 8);
    }
    new IViewer("HighRes Contours ", BReaderTools.bufferedImageFromMat(color));

    Point center = new Point(src.cols() / 2, src.rows() / 2);
    //Check hierarchy tree
    int[] res = polySearch(center, hierarchy, contours, 0);
    while (res[0] != 1 && res[2] != -1) {
        res = polySearch(center, hierarchy, contours, res[2]);
        if (res[0] == 1)
            break;
    }

    MatOfInt tHull = new MatOfInt();
    int index = 0;
    if (res[1] != -1) {
        index = res[1];
    }
    Imgproc.convexHull(contours.get(index), tHull);

    //get bounding box
    MatOfPoint cont = contours.get(index);
    Point[] points = new Point[tHull.rows()];
    for (int i = 0; i < tHull.rows(); i++) {
        int pIndex = (int) tHull.get(i, 0)[0];
        points[i] = new Point(cont.get(pIndex, 0));
    }
    Rect out = Imgproc.boundingRect(new MatOfPoint(points));
    return out;
}

From source file:edu.fiu.cate.breader.BaseSegmentation.java

/**
 * Finds the bounding box for the book on the stand using 
 * the depth average image./*w w w . j a  va  2 s.  co  m*/
 * @param src- The Depth average image
 * @return Rectangle delineating the book
 */
public Rect lowResDist(Mat src) {
    Mat dst = src.clone();

    Imgproc.blur(src, dst, new Size(5, 5), new Point(-1, -1), Core.BORDER_REPLICATE);
    //      Imgproc.threshold(dst, dst, 0,255,Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU);
    Imgproc.Canny(dst, dst, 50, 200, 3, false);
    //      Canny(src, dst, 20, 60, 3);

    List<MatOfPoint> contours = new LinkedList<>();
    Mat hierarchy = new Mat();
    /// Find contours
    Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE,
            new Point(0, 0));

    Mat color = new Mat();
    Imgproc.cvtColor(src, color, Imgproc.COLOR_GRAY2BGR);
    for (int k = 0; k < contours.size(); k++) {
        byte[] vals = ITools.getHeatMapColor((float) k / (float) contours.size());
        Imgproc.drawContours(color, contours, k, new Scalar(vals[0], vals[1], vals[2]), 1);
    }
    new IViewer("LowRes Contours ", BReaderTools.bufferedImageFromMat(color));

    for (int k = 0; k < contours.size(); k++) {
        MatOfPoint2f tMat = new MatOfPoint2f();
        Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(k).toArray()), tMat, 5, true);
        contours.set(k, new MatOfPoint(tMat.toArray()));
    }

    List<Point> points = new LinkedList<Point>();
    for (int i = 0; i < contours.size(); i++) {
        points.addAll(contours.get(i).toList());
    }

    MatOfInt tHull = new MatOfInt();
    Imgproc.convexHull(new MatOfPoint(points.toArray(new Point[points.size()])), tHull);

    //get bounding box
    Point[] tHullPoints = new Point[tHull.rows()];
    for (int i = 0; i < tHull.rows(); i++) {
        int pIndex = (int) tHull.get(i, 0)[0];
        tHullPoints[i] = points.get(pIndex);
    }
    Rect out = Imgproc.boundingRect(new MatOfPoint(tHullPoints));
    return out;
}

From source file:mvision.Bhattacharyya.java

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

    //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:org.usfirst.frc.team2084.CMonster2016.vision.BallProcessor.java

License:Open Source License

@Override
public void process(Mat cameraImage) {
    Imgproc.blur(cameraImage, hsvImage, new Size(20, 20));

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

    // Threshold image to find blue/green
    Core.inRange(hsvImage, thresholdMin, thresholdMax, thresholdImage);

    Imgproc.erode(thresholdImage, thresholdImage, KERNEL);
    Imgproc.dilate(thresholdImage, thresholdImage, KERNEL);

    thresholdImage.copyTo(contourImage);

    contours.clear();// w w w  . j  a  va 2s  .c o  m
    hulls.clear();

    Imgproc.findContours(contourImage, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    Imgproc.drawContours(cameraImage, contours, -1, CONTOUR_COLOR);

    for (int i = 0; i < contours.size(); i++) {
        MatOfPoint contour = contours.get(i);
        MatOfInt hullIndices = new MatOfInt();
        MatOfPoint hull;
        hulls.add(hull = new MatOfPoint());
        Imgproc.convexHull(contour, hullIndices);

        for (int c = 0; c < hullIndices.rows(); c++) {
            int v = (int) hullIndices.get(c, 0)[0];
            hull.put(c, 0, contour.get(v, 0));
            // System.out.println(v);
        }

        // System.out.println(hull.size());

    }

    Imgproc.drawContours(cameraImage, hulls, -1, CONTOUR_COLOR);

    debugImage("Threshold Image", thresholdImage);
    // debugImage("Contour Image", contourImage);
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.BoulderProcessor.java

License:Open Source License

private static MatOfPoint convexHull(MatOfPoint contour) {
    MatOfInt hullMatrix = new MatOfInt();
    Imgproc.convexHull(contour, hullMatrix); // perform convex hull, gap
                                             // filler
    MatOfPoint hull = new MatOfPoint();
    hull.create(hullMatrix.rows(), 1, CvType.CV_32SC2);

    for (int r = 0; r < hullMatrix.rows(); r++) {
        hull.put(r, 0, contour.get((int) hullMatrix.get(r, 0)[0], 0));
    }/* www .  j  a v a  2s  .c  om*/

    return hull;
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.HighGoalProcessor.java

License:Open Source License

private MatOfPoint convexHull(MatOfPoint contour) {
    MatOfInt hullMatrix = new MatOfInt();
    Imgproc.convexHull(contour, hullMatrix); // perform convex hull, gap
                                             // filler
    MatOfPoint hull = new MatOfPoint();
    hull.create(hullMatrix.rows(), 1, CvType.CV_32SC2);

    for (int r = 0; r < hullMatrix.rows(); r++) {
        hull.put(r, 0, contour.get((int) hullMatrix.get(r, 0)[0], 0));
    }/*w w  w. j a  v a  2  s .  c o  m*/

    return hull;
}

From source file:servlets.processScribble.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  ww  w  .j a  va  2s .c  om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        String imageForTextRecognition = request.getParameter("imageForTextRecognition") + ".png";

        Mat original = ImageUtils.loadImage(imageForTextRecognition, request);
        Mat image = original.clone();
        Mat mask = Mat.zeros(image.rows() + 2, image.cols() + 2, CvType.CV_8UC1);

        String samplingPoints = request.getParameter("samplingPoints");

        Gson gson = new Gson();
        Point[] userPoints = gson.fromJson(samplingPoints, Point[].class);

        MatOfPoint points = new MatOfPoint(new Mat(userPoints.length, 1, CvType.CV_32SC2));
        int cont = 0;

        for (Point point : userPoints) {
            int y = (int) point.y;
            int x = (int) point.x;
            int[] data = { x, y };
            points.put(cont++, 0, data);
        }

        MatOfInt hull = new MatOfInt();
        Imgproc.convexHull(points, hull);

        MatOfPoint mopOut = new MatOfPoint();
        mopOut.create((int) hull.size().height, 1, CvType.CV_32SC2);

        int totalPoints = (int) hull.size().height;

        Point[] convexHullPoints = new Point[totalPoints];
        ArrayList<Point> seeds = new ArrayList<>();

        for (int i = 0; i < totalPoints; i++) {
            int index = (int) hull.get(i, 0)[0];
            double[] point = new double[] { points.get(index, 0)[0], points.get(index, 0)[1] };
            mopOut.put(i, 0, point);

            convexHullPoints[i] = new Point(point[0], point[1]);
            seeds.add(new Point(point[0], point[1]));

        }

        MatOfPoint mop = new MatOfPoint();
        mop.fromArray(convexHullPoints);

        ArrayList<MatOfPoint> arrayList = new ArrayList<MatOfPoint>();
        arrayList.add(mop);

        Random random = new Random();
        int b = random.nextInt(256);
        int g = random.nextInt(256);
        int r = random.nextInt(256);
        Scalar newVal = new Scalar(b, g, r);

        FloodFillFacade floodFillFacade = new FloodFillFacade();

        for (int i = 0; i < seeds.size(); i++) {
            Point seed = seeds.get(i);
            image = floodFillFacade.fill(image, mask, (int) seed.x, (int) seed.y, newVal);
        }

        Imgproc.drawContours(image, arrayList, 0, newVal, -1);

        Imgproc.resize(mask, mask, image.size());

        Scalar meanColor = Core.mean(original, mask);

        //            Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\the_convexHull.png", image);
        ImageUtils.saveImage(image, imageForTextRecognition + "_the_convexHull.png", request);

        newVal = new Scalar(255, 255, 0);

        floodFillFacade.setMasked(false);
        System.out.println("Last one:");
        floodFillFacade.fill(image, mask, 211, 194, newVal);

        Core.circle(image, new Point(211, 194), 5, new Scalar(0, 0, 0), -1);
        ImageUtils.saveImage(image, imageForTextRecognition + "_final.png", request);
        //            Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\final.png", image);

        Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1));
        Imgproc.morphologyEx(mask, mask, Imgproc.MORPH_CLOSE, element, new Point(-1, -1), 3);

        Imgproc.resize(mask, mask, image.size());

        //            ImageUtils.saveImage(mask, "final_mask_dilated.png", request);
        //            Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\final_mask_dilated.png", mask);

        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(mask.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL,
                Imgproc.CHAIN_APPROX_NONE);
        double contourArea = 0;
        String path = "";

        MatOfPoint biggestContour = contours.get(0); // getting the biggest contour
        contourArea = Imgproc.contourArea(biggestContour);

        if (contours.size() > 1) {
            biggestContour = Collections.max(contours, new ContourComparator()); // getting the biggest contour in case there are more than one
        }

        Point[] biggestContourPoints = biggestContour.toArray();
        path = "M " + (int) biggestContourPoints[0].x + " " + (int) biggestContourPoints[0].y + " ";
        for (int i = 1; i < biggestContourPoints.length; ++i) {
            Point v = biggestContourPoints[i];
            path += "L " + (int) v.x + " " + (int) v.y + " ";
        }
        path += "Z";

        System.out.println("path:");
        System.out.println(path);

        Rect computedSearchWindow = Imgproc.boundingRect(biggestContour);
        Point massCenter = computedSearchWindow.tl();

        FindingResponse findingResponse = new FindingResponse(path, meanColor, massCenter, -1, contourArea);
        String jsonResponse = gson.toJson(findingResponse, FindingResponse.class);

        out.println(jsonResponse);

        //            String jsonResponse = gson.toJson(path);
        //            out.println(jsonResponse);
    }
}