Example usage for org.opencv.imgproc Imgproc minAreaRect

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

Introduction

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

Prototype

public static RotatedRect minAreaRect(MatOfPoint2f points) 

Source Link

Usage

From source file:OCV_MinAreaRect.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    byte[] byteArray = (byte[]) ip.getPixels();
    int w = ip.getWidth();
    int h = ip.getHeight();
    int num_slice = ip.getSliceNumber();

    ArrayList<Point> lstPt = new ArrayList<Point>();
    MatOfPoint2f pts = new MatOfPoint2f();

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            if (byteArray[x + w * y] != 0) {
                lstPt.add(new Point((double) x, (double) y));
            }/*from  w  ww  .ja v  a2 s .com*/
        }
    }

    if (lstPt.isEmpty()) {
        return;
    }

    pts.fromList(lstPt);
    RotatedRect rect = Imgproc.minAreaRect(pts);
    showData(rect, num_slice);
}

From source file:LicenseDetection.java

public void run() {

    // ------------------ set up tesseract for later use ------------------
    ITesseract tessInstance = new Tesseract();
    tessInstance.setDatapath("/Users/BradWilliams/Downloads/Tess4J");
    tessInstance.setLanguage("eng");

    // ------------------  Save image first ------------------
    Mat img;//from   ww w. j  a va 2 s.c  o m
    img = Imgcodecs.imread(getClass().getResource("/resources/car_2_shopped2.jpg").getPath());
    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/True_Image.png", img);

    // ------------------ Convert to grayscale ------------------
    Mat imgGray = new Mat();
    Imgproc.cvtColor(img, imgGray, Imgproc.COLOR_BGR2GRAY);
    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/Gray.png", imgGray);

    // ------------------ Blur so edge detection wont pick up noise ------------------
    Mat imgGaussianBlur = new Mat();
    Imgproc.GaussianBlur(imgGray, imgGaussianBlur, new Size(3, 3), 0);
    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/gaussian_blur.png", imgGaussianBlur);

    // ****************** Create image that will be cropped at end of program before OCR ***************************

    // ------------------ Binary theshold for OCR (used later)------------------
    Mat imgThresholdOCR = new Mat();
    Imgproc.adaptiveThreshold(imgGaussianBlur, imgThresholdOCR, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C,
            Imgproc.THRESH_BINARY, 7, 10);
    //Imgproc.threshold(imgSobel,imgThreshold,120,255,Imgproc.THRESH_TOZERO);
    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/imgThresholdOCR.png", imgThresholdOCR);

    // ------------------ Erosion operation------------------
    Mat kern = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_CROSS, new Size(3, 3));
    Mat imgErodeOCR = new Mat();
    Imgproc.morphologyEx(imgThresholdOCR, imgErodeOCR, Imgproc.MORPH_DILATE, kern); //Imgproc.MORPH_DILATE is performing erosion, wtf?
    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/imgErodeOCR.png", imgErodeOCR);

    //------------------ Dilation operation  ------------------
    Mat kernall = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(3, 3));
    Mat imgDilateOCR = new Mat();
    Imgproc.morphologyEx(imgErodeOCR, imgDilateOCR, Imgproc.MORPH_ERODE, kernall);
    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/imgDilateOCR.png", imgDilateOCR);

    // *************************************************************************************************************

    //        // ------------------ Close operation (dilation followed by erosion) to reduce noise ------------------
    //        Mat k = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(3, 3));
    //        Mat imgCloseOCR = new Mat();
    //        Imgproc.morphologyEx(imgThresholdOCR,imgCloseOCR,1,k);
    //        Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/imgCloseOCR.png", imgCloseOCR);

    // ------------------ Sobel vertical edge detection ------------------
    Mat imgSobel = new Mat();
    Imgproc.Sobel(imgGaussianBlur, imgSobel, -1, 1, 0);
    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/imgSobel.png", imgSobel);

    // ------------------ Binary theshold ------------------
    Mat imgThreshold = new Mat();
    Imgproc.adaptiveThreshold(imgSobel, imgThreshold, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C,
            Imgproc.THRESH_BINARY, 99, -60);
    //Imgproc.threshold(imgSobel,imgThreshold,120,255,Imgproc.THRESH_TOZERO);
    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/imgThreshold.png", imgThreshold);

    //        // ------------------ Open operation (erosion followed by dilation) ------------------
    //        Mat ker = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_CROSS, new Size(3, 2));
    //        Mat imgOpen = new Mat();
    //        Imgproc.morphologyEx(imgThreshold,imgOpen,0,ker);
    //        Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/imgOpen.png", imgOpen);

    // ------------------ Close operation (dilation followed by erosion) to reduce noise ------------------
    Mat kernel = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(22, 8));
    Mat imgClose = new Mat();
    Imgproc.morphologyEx(imgThreshold, imgClose, 1, kernel);
    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/imgClose.png", imgClose);

    // ------------------ Find contours ------------------
    List<MatOfPoint> contours = new ArrayList<>();

    Imgproc.findContours(imgClose, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    // **************************** DEBUG CODE **************************

    Mat contourImg = new Mat(imgClose.size(), imgClose.type());
    for (int i = 0; i < contours.size(); i++) {
        Imgproc.drawContours(contourImg, contours, i, new Scalar(255, 255, 255), -1);
    }

    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/contours.png", contourImg);

    // ******************************************************************

    // --------------  Convert contours --------------------

    //Convert to MatOfPoint2f so that minAreaRect can be called
    List<MatOfPoint2f> newContours = new ArrayList<>();

    for (MatOfPoint mat : contours) {

        MatOfPoint2f newPoint = new MatOfPoint2f(mat.toArray());
        newContours.add(newPoint);

    }

    //Get minAreaRects
    List<RotatedRect> minAreaRects = new ArrayList<>();

    for (MatOfPoint2f mat : newContours) {

        RotatedRect rect = Imgproc.minAreaRect(mat);

        /*
         --------------- BUG WORK AROUND ------------
                
        Possible bug:
        When converting from MatOfPoint2f to RotatectRect the width height were reversed and the
        angle was -90 degrees from what it would be if the width and height were correct.
                
        When painting rectangle in image, the correct boxes were produced, but performing calculations on rect.angle
        rect.width, or rect.height yielded unwanted results.
                
        The following work around is buggy but works for my purpose
         */

        if (rect.size.width < rect.size.height) {
            double temp;

            temp = rect.size.width;
            rect.size.width = rect.size.height;
            rect.size.height = temp;
            rect.angle = rect.angle + 90;

        }

        //check aspect ratio and area and angle
        if (rect.size.width / rect.size.height > 1 && rect.size.width / rect.size.height < 5
                && rect.size.width * rect.size.height > 10000 && rect.size.width * rect.size.height < 50000
                && Math.abs(rect.angle) < 20) {
            minAreaRects.add(rect);
        }

        //minAreaRects.add(rect);
    }

    // **************************** DEBUG CODE **************************
    /*
    The following code is used to draw the rectangles on top of the original image for debugging purposes
     */
    //Draw Rotated Rects
    Point[] vertices = new Point[4];

    Mat imageWithBoxes = img;

    // Draw color rectangles on top of binary contours
    //        Mat imageWithBoxes = new Mat();
    //        Mat temp = imgDilateOCR;
    //        Imgproc.cvtColor(temp, imageWithBoxes, Imgproc.COLOR_GRAY2RGB);

    for (RotatedRect rect : minAreaRects) {

        rect.points(vertices);

        for (int i = 0; i < 4; i++) {
            Imgproc.line(imageWithBoxes, vertices[i], vertices[(i + 1) % 4], new Scalar(0, 0, 255), 2);
        }

    }

    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/imgWithBoxes.png", imageWithBoxes);

    // ******************************************************************

    // **************************** DEBUG CODE **************************
    //        for(RotatedRect rect : minAreaRects) {
    //            System.out.println(rect.toString());
    //        }
    // ******************************************************************

    /*
    In order to rotate image without cropping it:
            
    1. Create new square image with dimension = diagonal of initial image.
    2. Draw initial image into the center of new image.
     Insert initial image at ROI (Region of Interest) in new image
    3. Rotate new image
     */

    //Find diagonal/hypotenuse
    int hypotenuse = (int) Math.sqrt((img.rows() * img.rows()) + (img.cols() * img.cols()));

    //New Mat with hypotenuse as height and width
    Mat rotateSpace = new Mat(hypotenuse, hypotenuse, 0);

    int ROI_x = (rotateSpace.width() - imgClose.width()) / 2; //x start of ROI
    int ROI_y = (rotateSpace.height() - imgClose.height()) / 2; //x start of ROI

    //designate region of interest
    Rect r = new Rect(ROI_x, ROI_y, imgClose.width(), imgClose.height());

    //Insert image into region of interest
    imgDilateOCR.copyTo(rotateSpace.submat(r));

    Mat rotatedTemp = new Mat(); //Mat to hold temporarily rotated mat
    Mat rectMat = new Mat();//Mat to hold rect contents (needed for looping through pixels)
    Point[] rectVertices = new Point[4];//Used to build rect to make ROI
    Rect rec = new Rect();

    List<RotatedRect> edgeDensityRects = new ArrayList<>(); //populate new arraylist with rects that satisfy edge density

    int count = 0;

    //Loop through Rotated Rects and find edge density
    for (RotatedRect rect : minAreaRects) {

        count++;

        rect.center = new Point((float) ROI_x + rect.center.x, (float) ROI_y + rect.center.y);

        //rotate image to math orientation of rotated rect
        rotate(rotateSpace, rotatedTemp, rect.center, rect.angle);

        //remove rect rotation
        rect.angle = 0;

        //get vertices from rotatedRect
        rect.points(rectVertices);

        // **************************** DEBUG CODE **************************
        //
        //            for (int k = 0; k < 4; k++) {
        //                System.out.println(rectVertices[k]);
        //                Imgproc.line(rotatedTemp, rectVertices[k], rectVertices[(k + 1) % 4], new Scalar(0, 0, 255), 2);
        //            }
        //
        //            Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/rotated" + count + ".png", rotatedTemp);

        // *****************************************************************

        //build rect to use as ROI
        rec = new Rect(rectVertices[1], rectVertices[3]);

        rectMat = rotatedTemp.submat(rec);

        Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/extracted" + count + ".png", rectMat);

        //find edge density

        //            // ------------------------ edge density check NOT IMPLEMENTED --------------------
        //            /*
        //            Checking for edge density was not necessary for this image so it was not implemented due to lack of time
        //             */
        //            for(int i = 0; i < rectMat.rows(); ++i){
        //                for(int j = 0; j < rectMat.cols(); ++j){
        //
        //                  //add up white pixels
        //                }
        //            }
        //
        //            //check number of white pixels against total pixels
        //            //only add rects to new arraylist that satisfy threshold

        edgeDensityRects.add(rect);
    }

    // **************************** DEBUG CODE **************************

    Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/rotatedSpace.png", rotateSpace);
    //Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/rotatedSpaceROTATED.png", rotatedTemp);

    //System.out.println(imgGray.type());

    // *****************************************************************

    // if there is only one rectangle left, its the license plate
    if (edgeDensityRects.size() == 1) {

        String result = ""; //Hold result from OCR
        BufferedImage bimg;
        Mat cropped;

        cropped = rectMat.submat(new Rect(20, 50, rectMat.width() - 40, rectMat.height() - 70));

        Imgcodecs.imwrite("/Users/BradWilliams/ComputerVisionOut/rectMatCropped.png", cropped);

        bimg = matToBufferedImage(cropped);

        BufferedImage image = bimg;

        try {
            result = tessInstance.doOCR(image);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }

        for (int i = 0; i < 10; ++i) {

        }

        result = result.replace("\n", "");

        System.out.println(result);

        CarProfDBImpl db = new CarProfDBImpl();

        db.connect("localhost:3306/computer_vision", "root", "*******");

        CarProf c = db.getCarProf(result);

        System.out.print(c.toString());

        db.close();

    }

}

From source file:com.github.mbillingr.correlationcheck.ImageProcessor.java

License:Open Source License

public List<Point> extractPoints() {
    Mat gray = new Mat();//work_width, work_height, CvType.CV_8UC1);
    Mat binary = new Mat();

    Mat kernel = Mat.ones(3, 3, CvType.CV_8UC1);

    debugreset();/*w ww  .jav  a  2  s . c  o  m*/

    Mat image = load_transformed();
    working_image = image.clone();
    debugsave(image, "source");

    Imgproc.cvtColor(image, gray, Imgproc.COLOR_RGB2GRAY);
    debugsave(gray, "grayscale");

    Imgproc.GaussianBlur(gray, gray, new Size(15, 15), 0);
    debugsave(gray, "blurred");

    //Imgproc.equalizeHist(gray, gray);
    //debugsave(gray, "equalized");

    Imgproc.adaptiveThreshold(gray, binary, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV,
            129, 5);
    //Imgproc.threshold(gray, binary, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
    //Imgproc.threshold(gray, binary, 128, 255, Imgproc.THRESH_BINARY_INV);
    debugsave(binary, "binary");

    Imgproc.morphologyEx(binary, binary, Imgproc.MORPH_CLOSE, kernel);
    debugsave(binary, "closed");

    Imgproc.morphologyEx(binary, binary, Imgproc.MORPH_OPEN, kernel);
    debugsave(binary, "opened");

    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(binary, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); // is binary is now changed
    Imgproc.drawContours(image, contours, -1, new Scalar(0, 0, 255), 3);
    debugsave(image, "contours");

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

    for (MatOfPoint cnt : contours) {
        MatOfPoint2f c2f = new MatOfPoint2f();
        c2f.fromArray(cnt.toArray());
        RotatedRect rr = Imgproc.minAreaRect(c2f);

        double area = Imgproc.contourArea(cnt);

        if (rr.size.width / rr.size.height < 3 && rr.size.height / rr.size.width < 3 && rr.size.width < 64
                && rr.size.height < 64 && area > 9 && area < 10000) {
            points.add(new PointAndArea((int) area, rr.center));
        }
    }

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

    Collections.sort(points);
    Collections.reverse(points);
    int prev = -1;
    for (PointAndArea p : points) {
        Log.i("area", Integer.toString(p.area));
        if (prev == -1 || p.area >= prev / 2) {
            prev = p.area;
            Imgproc.circle(image, p.point, 10, new Scalar(0, 255, 0), 5);
            final_points.add(new Point(1 - p.point.y / work_height, 1 - p.point.x / work_width));
        }
    }
    debugsave(image, "circles");

    return final_points;
}

From source file:com.shootoff.camera.autocalibration.AutoCalibrationManager.java

License:Open Source License

private MatOfPoint2f estimatePatternRect(Mat traceMat, MatOfPoint2f boardCorners) {
    // Turn the chessboard into corners
    final MatOfPoint2f boardRect = calcBoardRectFromCorners(boardCorners);

    // We use this to calculate the angle
    final RotatedRect boardBox = Imgproc.minAreaRect(boardRect);
    final double boardBoxAngle = boardBox.size.height > boardBox.size.width ? 90.0 + boardBox.angle
            : boardBox.angle;//  w ww.j  ava 2 s . c  om

    // This is the board corners with the angle eliminated
    final Mat unRotMat = getRotationMatrix(massCenterMatOfPoint2f(boardRect), boardBoxAngle);
    final MatOfPoint2f unRotatedRect = rotateRect(unRotMat, boardRect);

    // This is the estimated projection area that has minimum angle (Not
    // rotated)
    final MatOfPoint2f estimatedPatternSizeRect = estimateFullPatternSize(unRotatedRect);

    // This is what we'll use as the transformation target and bounds given
    // back to the cameramanager
    boundsRect = Imgproc.minAreaRect(estimatedPatternSizeRect);

    // We now rotate the estimation back to the original angle to use for
    // transformation source
    final Mat rotMat = getRotationMatrix(massCenterMatOfPoint2f(estimatedPatternSizeRect), -boardBoxAngle);

    final MatOfPoint2f rotatedPatternSizeRect = rotateRect(rotMat, estimatedPatternSizeRect);

    if (logger.isTraceEnabled()) {
        logger.trace("center {} angle {} width {} height {}", boardBox.center, boardBoxAngle,
                boardBox.size.width, boardBox.size.height);

        logger.debug("boundsRect {} {} {} {}", boundsRect.boundingRect().x, boundsRect.boundingRect().y,
                boundsRect.boundingRect().x + boundsRect.boundingRect().width,
                boundsRect.boundingRect().y + boundsRect.boundingRect().height);

        Core.circle(traceMat, new Point(boardRect.get(0, 0)[0], boardRect.get(0, 0)[1]), 1,
                new Scalar(255, 0, 0), -1);
        Core.circle(traceMat, new Point(boardRect.get(1, 0)[0], boardRect.get(1, 0)[1]), 1,
                new Scalar(255, 0, 0), -1);
        Core.circle(traceMat, new Point(boardRect.get(2, 0)[0], boardRect.get(2, 0)[1]), 1,
                new Scalar(255, 0, 0), -1);
        Core.circle(traceMat, new Point(boardRect.get(3, 0)[0], boardRect.get(3, 0)[1]), 1,
                new Scalar(255, 0, 0), -1);

        Core.line(traceMat, new Point(unRotatedRect.get(0, 0)[0], unRotatedRect.get(0, 0)[1]),
                new Point(unRotatedRect.get(1, 0)[0], unRotatedRect.get(1, 0)[1]), new Scalar(0, 255, 0));
        Core.line(traceMat, new Point(unRotatedRect.get(1, 0)[0], unRotatedRect.get(1, 0)[1]),
                new Point(unRotatedRect.get(2, 0)[0], unRotatedRect.get(2, 0)[1]), new Scalar(0, 255, 0));
        Core.line(traceMat, new Point(unRotatedRect.get(3, 0)[0], unRotatedRect.get(3, 0)[1]),
                new Point(unRotatedRect.get(2, 0)[0], unRotatedRect.get(2, 0)[1]), new Scalar(0, 255, 0));
        Core.line(traceMat, new Point(unRotatedRect.get(3, 0)[0], unRotatedRect.get(3, 0)[1]),
                new Point(unRotatedRect.get(0, 0)[0], unRotatedRect.get(0, 0)[1]), new Scalar(0, 255, 0));

        Core.line(traceMat,
                new Point(estimatedPatternSizeRect.get(0, 0)[0], estimatedPatternSizeRect.get(0, 0)[1]),
                new Point(estimatedPatternSizeRect.get(1, 0)[0], estimatedPatternSizeRect.get(1, 0)[1]),
                new Scalar(255, 255, 0));
        Core.line(traceMat,
                new Point(estimatedPatternSizeRect.get(1, 0)[0], estimatedPatternSizeRect.get(1, 0)[1]),
                new Point(estimatedPatternSizeRect.get(2, 0)[0], estimatedPatternSizeRect.get(2, 0)[1]),
                new Scalar(255, 255, 0));
        Core.line(traceMat,
                new Point(estimatedPatternSizeRect.get(3, 0)[0], estimatedPatternSizeRect.get(3, 0)[1]),
                new Point(estimatedPatternSizeRect.get(2, 0)[0], estimatedPatternSizeRect.get(2, 0)[1]),
                new Scalar(255, 255, 0));
        Core.line(traceMat,
                new Point(estimatedPatternSizeRect.get(3, 0)[0], estimatedPatternSizeRect.get(3, 0)[1]),
                new Point(estimatedPatternSizeRect.get(0, 0)[0], estimatedPatternSizeRect.get(0, 0)[1]),
                new Scalar(255, 255, 0));

        Core.line(traceMat, new Point(rotatedPatternSizeRect.get(0, 0)[0], rotatedPatternSizeRect.get(0, 0)[1]),
                new Point(rotatedPatternSizeRect.get(1, 0)[0], rotatedPatternSizeRect.get(1, 0)[1]),
                new Scalar(255, 255, 0));
        Core.line(traceMat, new Point(rotatedPatternSizeRect.get(1, 0)[0], rotatedPatternSizeRect.get(1, 0)[1]),
                new Point(rotatedPatternSizeRect.get(2, 0)[0], rotatedPatternSizeRect.get(2, 0)[1]),
                new Scalar(255, 255, 0));
        Core.line(traceMat, new Point(rotatedPatternSizeRect.get(3, 0)[0], rotatedPatternSizeRect.get(3, 0)[1]),
                new Point(rotatedPatternSizeRect.get(2, 0)[0], rotatedPatternSizeRect.get(2, 0)[1]),
                new Scalar(255, 255, 0));
        Core.line(traceMat, new Point(rotatedPatternSizeRect.get(3, 0)[0], rotatedPatternSizeRect.get(3, 0)[1]),
                new Point(rotatedPatternSizeRect.get(0, 0)[0], rotatedPatternSizeRect.get(0, 0)[1]),
                new Scalar(255, 255, 0));
    }

    return rotatedPatternSizeRect;
}

From source file:karthik.Barcode.MatrixBarcode.java

License:Open Source License

public List<CandidateResult> locateBarcode() throws IOException {

    calcGradientDirectionAndMagnitude();
    for (int tileSize = searchParams.tileSize; tileSize < rows && tileSize < cols; tileSize *= 4) {
        img_details.probabilities = calcProbabilityMatrix(tileSize); // find areas with low variance in gradient direction

        //    connectComponents();
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        // findContours modifies source image so probabilities pass it a clone of img_details.probabilities
        // img_details.probabilities will be used again shortly to expand the barcode region
        Imgproc.findContours(img_details.probabilities.clone(), contours, hierarchy, Imgproc.RETR_LIST,
                Imgproc.CHAIN_APPROX_SIMPLE);

        double bounding_rect_area = 0;
        RotatedRect minRect;/*from  www .ja  va 2  s . c o m*/
        CandidateResult ROI;
        int area_multiplier = (searchParams.RECT_HEIGHT * searchParams.RECT_WIDTH)
                / (searchParams.PROB_MAT_TILE_SIZE * searchParams.PROB_MAT_TILE_SIZE);
        // pictures were downsampled during probability calc so we multiply it by the tile size to get area in the original picture

        for (int i = 0; i < contours.size(); i++) {
            double area = Imgproc.contourArea(contours.get(i));

            if (area * area_multiplier < searchParams.THRESHOLD_MIN_AREA) // ignore contour if it is of too small a region
                continue;

            minRect = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(i).toArray()));
            bounding_rect_area = minRect.size.width * minRect.size.height;
            if (DEBUG_IMAGES) {
                System.out.println("Area is " + area * area_multiplier + " MIN_AREA is "
                        + searchParams.THRESHOLD_MIN_AREA);
                System.out.println("area ratio is " + ((area / bounding_rect_area)));
            }

            if ((area / bounding_rect_area) > searchParams.THRESHOLD_AREA_RATIO) // check if contour is of a rectangular object
            {
                CandidateMatrixBarcode cb = new CandidateMatrixBarcode(img_details, minRect, searchParams);
                if (DEBUG_IMAGES)
                    cb.debug_drawCandidateRegion(new Scalar(0, 255, 128), img_details.src_scaled);
                // get candidate regions to be a barcode

                // rotates candidate region to straighten it based on the angle of the enclosing RotatedRect                
                ROI = cb.NormalizeCandidateRegion(Barcode.USE_ROTATED_RECT_ANGLE);
                if (postProcessResizeBarcode)
                    ROI.ROI = scale_candidateBarcode(ROI.ROI);

                candidateBarcodes.add(ROI);

                if (DEBUG_IMAGES)
                    cb.debug_drawCandidateRegion(new Scalar(0, 0, 255), img_details.src_scaled);
            }
        }
    }
    return candidateBarcodes;
}

From source file:org.openpnp.vision.FluentCv.java

License:Open Source License

public FluentCv getContourRects(List<MatOfPoint> contours, List<RotatedRect> rects) {
    for (int i = 0; i < contours.size(); i++) {
        MatOfPoint2f contour_ = new MatOfPoint2f();
        contours.get(i).convertTo(contour_, CvType.CV_32FC2);
        if (contour_.empty()) {
            continue;
        }/*from  w ww  .j a va 2  s .c om*/
        RotatedRect rect = Imgproc.minAreaRect(contour_);
        rects.add(rect);
    }
    return this;
}

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

License:Open Source License

/**
 * Creates a new possible target based on the specified blob and calculates
 * its score.//  w  w w.  j a  v  a2  s.  c  o m
 *
 * @param p the shape of the possible target
 */
public Target(MatOfPoint contour, Mat grayImage) {
    // Simplify contour to make the corner finding algorithm work better
    MatOfPoint2f fContour = new MatOfPoint2f();
    contour.convertTo(fContour, CvType.CV_32F);
    Imgproc.approxPolyDP(fContour, fContour, VisionParameters.getGoalApproxPolyEpsilon(), true);
    fContour.convertTo(contour, CvType.CV_32S);

    this.contour = contour;

    // Check area, and don't do any calculations if it is not valid
    if (validArea = validateArea()) {

        // Find a bounding rectangle
        RotatedRect rect = Imgproc.minAreaRect(fContour);

        Point[] rectPoints = new Point[4];
        rect.points(rectPoints);

        for (int j = 0; j < rectPoints.length; j++) {
            Point rectPoint = rectPoints[j];

            double minDistance = Double.MAX_VALUE;
            Point point = null;

            for (int i = 0; i < contour.rows(); i++) {
                Point contourPoint = new Point(contour.get(i, 0));
                double dist = distance(rectPoint, contourPoint);
                if (dist < minDistance) {
                    minDistance = dist;
                    point = contourPoint;
                }
            }

            rectPoints[j] = point;
        }
        MatOfPoint2f rectMat = new MatOfPoint2f(rectPoints);
        // Refine the corners to improve accuracy
        Imgproc.cornerSubPix(grayImage, rectMat, new Size(4, 10), new Size(-1, -1),
                new TermCriteria(TermCriteria.EPS + TermCriteria.COUNT, 30, 0.1));
        rectPoints = rectMat.toArray();

        // Identify each corner
        SortedMap<Double, List<Point>> x = new TreeMap<>();
        Arrays.stream(rectPoints).forEach((p) -> {
            List<Point> points;
            if ((points = x.get(p.x)) == null) {
                x.put(p.x, points = new LinkedList<>());
            }
            points.add(p);
        });

        int i = 0;
        for (Iterator<List<Point>> it = x.values().iterator(); it.hasNext();) {
            List<Point> s = it.next();

            for (Point p : s) {
                switch (i) {
                case 0:
                    topLeft = p;
                    break;
                case 1:
                    bottomLeft = p;
                    break;
                case 2:
                    topRight = p;
                    break;
                case 3:
                    bottomRight = p;
                }
                i++;
            }
        }

        // Organize corners
        if (topLeft.y > bottomLeft.y) {
            Point p = bottomLeft;
            bottomLeft = topLeft;
            topLeft = p;
        }

        if (topRight.y > bottomRight.y) {
            Point p = bottomRight;
            bottomRight = topRight;
            topRight = p;
        }

        // Create corners for centroid calculation
        corners = new MatOfPoint2f(rectPoints);

        // Calculate center
        Moments moments = Imgproc.moments(corners);
        center = new Point(moments.m10 / moments.m00, moments.m01 / moments.m00);

        // Put the points in the correct order for solvePNP
        rectPoints[0] = topLeft;
        rectPoints[1] = topRight;
        rectPoints[2] = bottomLeft;
        rectPoints[3] = bottomRight;
        // Recreate corners in the new order
        corners = new MatOfPoint2f(rectPoints);

        widthTop = distance(topLeft, topRight);
        widthBottom = distance(bottomLeft, bottomRight);
        width = (widthTop + widthBottom) / 2.0;
        heightLeft = distance(topLeft, bottomLeft);
        heightRight = distance(topRight, bottomRight);
        height = (heightLeft + heightRight) / 2.0;

        Mat tvec = new Mat();

        // Calculate target's location
        Calib3d.solvePnP(OBJECT_POINTS, corners, CAMERA_MAT, DISTORTION_MAT, rotation, tvec, false,
                Calib3d.CV_P3P);

        // =======================================
        // Position and Orientation Transformation
        // =======================================

        double armAngle = VisionResults.getArmAngle();

        // Flip y axis to point upward
        Core.multiply(tvec, SIGN_NORMALIZATION_MATRIX, tvec);

        // Shift origin to arm pivot point, on the robot's centerline
        CoordinateMath.translate(tvec, CAMERA_X_OFFSET, CAMERA_Y_OFFSET, ARM_LENGTH);

        // Align axes with ground
        CoordinateMath.rotateX(tvec, -armAngle);
        Core.add(rotation, new MatOfDouble(armAngle, 0, 0), rotation);

        // Shift origin to robot center of rotation
        CoordinateMath.translate(tvec, 0, ARM_PIVOT_Y_OFFSET, -ARM_PIVOT_Z_OFFSET);

        double xPosFeet = tvec.get(0, 0)[0];
        double yPosFeet = tvec.get(1, 0)[0];
        double zPosFeet = tvec.get(2, 0)[0];

        // Old less effective aiming heading and distance calculation
        // double pixelsToFeet = TARGET_WIDTH / width;

        // distance = (TARGET_WIDTH * HighGoalProcessor.IMAGE_SIZE.width
        // / (2 * width ** Math.tan(VisionParameters.getFOVAngle() / 2)));
        // double xPosFeet = (center.x - (HighGoalProcessor.IMAGE_SIZE.width
        // / 2)) * pixelsToFeet;
        // double yPosFeet = -(center.y -
        // (HighGoalProcessor.IMAGE_SIZE.height / 2)) * pixelsToFeet;

        distance = Math.sqrt(xPosFeet * xPosFeet + zPosFeet * zPosFeet);

        position = new Point3(xPosFeet, yPosFeet, zPosFeet);

        xGoalAngle = Math.atan(xPosFeet / zPosFeet);
        yGoalAngle = Math.atan(yPosFeet / zPosFeet);

        validate();
        score = calculateScore();
    } else {
        valid = false;
    }
}

From source file:processdata.ExperimentalDataProcessingUI.java

private void jButtonProcessImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonProcessImageActionPerformed
    try {/*from  w w w .j  av  a  2s  .  c  om*/
        // TODO add your handling code here:
        //load library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        // TODO add your handling code here:
        folderName = textField2.getText();
        int currentFrameIndex = Integer.parseInt(initialFrameIndexBox.getText()) - 1;
        datasetIndex = Integer.parseInt(textField1.getText());
        String videoImageFileName = "./videoFrames//" + folderName + "//" + "frame_outVideo_"
                + currentFrameIndex + ".jpg";

        String depthFrameFileName = initialImagePath + datasetIndex + "//" + folderName + "//" + "depthData//"
                + "outDepthByte_" + currentFrameIndex;

        rgbFrame = Highgui.imread(videoImageFileName, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

        depthFrame = depthDataProcessingUtilities.processDepthDataFile(depthFrameFileName, jSlider2.getValue(),
                jSlider1.getValue());

        Mat[] backgroundFrames = readBackground();
        rgbBackgroundFrame = backgroundFrames[0];
        depthBackgroundFrame = backgroundFrames[1];

        //subtract depth background
        Mat depthFrameBackgroundSubtracted = new Mat();
        Core.subtract(depthBackgroundFrame, depthFrame, depthFrameBackgroundSubtracted);
        Imgproc.threshold(depthFrameBackgroundSubtracted, depthFrameBackgroundSubtracted, 0, 255,
                Imgproc.THRESH_BINARY);
        displayImage(Mat2BufferedImage(
                videoProcessingUtilities.resizeImage(depthFrameBackgroundSubtracted, new Size(448, 234))),
                depthBckgSubtractedFrames);

        //remove the red-colored elements from depth image and leave only blue ones
        Mat depthImageCleaned = new Mat();
        Core.inRange(depthFrameBackgroundSubtracted, new Scalar(253, 0, 0), new Scalar(255, 0, 0),
                depthImageCleaned);

        //apply morphologic opening to remove noise
        Imgproc.morphologyEx(depthImageCleaned, depthImageCleaned, 2,
                Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3)));
        displayImage(
                Mat2BufferedImage(videoProcessingUtilities.resizeImage(depthImageCleaned, new Size(448, 234))),
                depthCleanedFramesPanel);

        //apply the homographic transform to cleaned depth image
        Mat hDepthImageCleaned = videoProcessingUtilities.performHomographyTransformation(depthImageCleaned,
                new Size(1920, 1080));

        //extract all contours
        //sort all extracted contours and choose top 2
        //overlay top 2 contours on the image and fill them with white color
        //mask the rgb frame
        // do all necessary rotation operations
        //offer user to save the image

        //extract all suitable contours between MIN and MAX areas:
        MatOfPoint[] contours = videoProcessingUtilities.extractLargestContours(hDepthImageCleaned, 100000,
                160000);
        System.out.println("Number of contorus extracted " + contours.length);

        //draw contours
        List<MatOfPoint> tempContours = new ArrayList<MatOfPoint>();
        Mat hDepthImageCleanedContours = hDepthImageCleaned.clone();
        for (MatOfPoint cnt : contours) {
            System.out.println("Extracted Contour Area is " + Imgproc.contourArea(cnt));
            tempContours.add(cnt);
        }
        Imgproc.cvtColor(hDepthImageCleanedContours, hDepthImageCleanedContours, Imgproc.COLOR_GRAY2BGR);
        Imgproc.drawContours(hDepthImageCleanedContours, tempContours, -1, new Scalar(0, 0, 255), 5);
        displayImage(
                Mat2BufferedImage(
                        videoProcessingUtilities.resizeImage(hDepthImageCleanedContours, new Size(448, 234))),
                extractedContoursPanel);

        //prepare final mask
        Mat hDepthImageFilledContours = new Mat(hDepthImageCleaned.rows(), hDepthImageCleaned.cols(),
                hDepthImageCleaned.type());
        Imgproc.drawContours(hDepthImageFilledContours, tempContours, -1, new Scalar(255, 255, 255), -1);
        displayImage(
                Mat2BufferedImage(
                        videoProcessingUtilities.resizeImage(hDepthImageFilledContours, new Size(448, 234))),
                maskedContoursPanel);

        //subtract video background
        //            Mat rgbFrameBackgroundSubtracted = new Mat();
        //            Core.subtract(rgbBackgroundFrame,rgbFrame, rgbFrameBackgroundSubtracted, hDepthImageCleaned);
        //            displayImage(Mat2BufferedImage(videoProcessingUtilities.resizeImage(rgbFrameBackgroundSubtracted, new Size(448,234))),videoBckgSubtractedFrames);
        //            
        //mask
        Mat preMaskedRGBFrame = new Mat();
        rgbFrame.copyTo(preMaskedRGBFrame, hDepthImageCleaned);
        displayImage(
                Mat2BufferedImage(videoProcessingUtilities.resizeImage(preMaskedRGBFrame, new Size(448, 234))),
                videoBckgSubtractedFrames);

        //postmask
        Mat betterMaskedRGBFrame = new Mat();
        rgbFrame.copyTo(betterMaskedRGBFrame, hDepthImageFilledContours);
        displayImage(
                Mat2BufferedImage(
                        videoProcessingUtilities.resizeImage(betterMaskedRGBFrame, new Size(448, 234))),
                videoMaskedPanel);

        //clear ArrayList containig all processed images
        finalImages.clear();
        javax.swing.JLabel[] jLabArray = { extractedShoePanel1, extractedShoePanel2 };
        //segment all images
        int panelIdx = 0;
        for (MatOfPoint contour : tempContours) {
            MatOfPoint2f newMatOfPoint2fContour = new MatOfPoint2f(contour.toArray());
            RotatedRect finalROI = Imgproc.minAreaRect(newMatOfPoint2fContour);
            Mat newMask = videoProcessingUtilities.getContourMasked(hDepthImageFilledContours.clone(), contour);
            Mat imageROIRegistred = new Mat();
            betterMaskedRGBFrame.copyTo(imageROIRegistred, newMask);
            Mat maskedRGBFrameROI = videoProcessingUtilities.rotateExtractedShoeprint(imageROIRegistred,
                    finalROI, new Size(500, 750), 2);
            finalImages.add(maskedRGBFrameROI);
            displayImage(
                    Mat2BufferedImage(
                            videoProcessingUtilities.resizeImage(maskedRGBFrameROI, new Size(203, 250))),
                    jLabArray[panelIdx]);
            panelIdx++;
        }

        //MatOfInt parameters = new MatOfInt();
        //parameters.fromArray(Highgui.CV_IMWRITE_JPEG_QUALITY, 100);
        //Highgui.imwrite(".//backgrounds//"+"test.jpg", depthFrameBackgroundSubtracted, parameters);

    } catch (FileNotFoundException ex) {
        Logger.getLogger(ExperimentalDataProcessingUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:tv.danmaku.ijk.media.example.activities.VideoActivity.java

License:Apache License

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();//from   w w w. j a  v  a 2  s .  co  m
    mGray = inputFrame.gray();

    //        return mRgba;
    //        iThreshold = 10000;

    //Imgproc.blur(mRgba, mRgba, new Size(5,5));
    Imgproc.GaussianBlur(mRgba, mRgba, new org.opencv.core.Size(3, 3), 1, 1);
    //Imgproc.medianBlur(mRgba, mRgba, 3);

    if (!mIsColorSelected)
        return mRgba;

    List<MatOfPoint> contours = mDetector.getContours();
    mDetector.process(mRgba);

    Log.d(TAG, "Contours count: " + contours.size());

    if (contours.size() <= 0) {
        return mRgba;
    }

    RotatedRect rect = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(0).toArray()));

    double boundWidth = rect.size.width;
    double boundHeight = rect.size.height;
    int boundPos = 0;

    for (int i = 1; i < contours.size(); i++) {
        rect = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(i).toArray()));
        if (rect.size.width * rect.size.height > boundWidth * boundHeight) {
            boundWidth = rect.size.width;
            boundHeight = rect.size.height;
            boundPos = i;
        }
    }

    Rect boundRect = Imgproc.boundingRect(new MatOfPoint(contours.get(boundPos).toArray()));
    Imgproc.rectangle(mRgba, boundRect.tl(), boundRect.br(), CONTOUR_COLOR_WHITE, 2, 8, 0);

    Log.d(TAG, " Row start [" + (int) boundRect.tl().y + "] row end [" + (int) boundRect.br().y
            + "] Col start [" + (int) boundRect.tl().x + "] Col end [" + (int) boundRect.br().x + "]");

    int rectHeightThresh = 0;
    double a = boundRect.br().y - boundRect.tl().y;
    a = a * 0.7;
    a = boundRect.tl().y + a;

    Log.d(TAG, " A [" + a + "] br y - tl y = [" + (boundRect.br().y - boundRect.tl().y) + "]");

    //Core.rectangle( mRgba, boundRect.tl(), boundRect.br(), CONTOUR_COLOR, 2, 8, 0 );
    Imgproc.rectangle(mRgba, boundRect.tl(), new Point(boundRect.br().x, a), CONTOUR_COLOR, 2, 8, 0);

    MatOfPoint2f pointMat = new MatOfPoint2f();
    Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(boundPos).toArray()), pointMat, 3, true);
    contours.set(boundPos, new MatOfPoint(pointMat.toArray()));

    MatOfInt hull = new MatOfInt();
    MatOfInt4 convexDefect = new MatOfInt4();
    Imgproc.convexHull(new MatOfPoint(contours.get(boundPos).toArray()), hull);

    if (hull.toArray().length < 3)
        return mRgba;

    Imgproc.convexityDefects(new MatOfPoint(contours.get(boundPos).toArray()), hull, convexDefect);

    List<MatOfPoint> hullPoints = new LinkedList<MatOfPoint>();
    List<Point> listPo = new LinkedList<Point>();
    for (int j = 0; j < hull.toList().size(); j++) {
        listPo.add(contours.get(boundPos).toList().get(hull.toList().get(j)));
    }

    MatOfPoint e = new MatOfPoint();
    e.fromList(listPo);
    hullPoints.add(e);

    List<MatOfPoint> defectPoints = new LinkedList<MatOfPoint>();
    List<Point> listPoDefect = new LinkedList<Point>();
    for (int j = 0; j < convexDefect.toList().size(); j = j + 4) {
        Point farPoint = contours.get(boundPos).toList().get(convexDefect.toList().get(j + 2));
        Integer depth = convexDefect.toList().get(j + 3);
        if (depth > iThreshold && farPoint.y < a) {
            listPoDefect.add(contours.get(boundPos).toList().get(convexDefect.toList().get(j + 2)));
        }
        Log.d(TAG, "defects [" + j + "] " + convexDefect.toList().get(j + 3));
    }

    MatOfPoint e2 = new MatOfPoint();
    e2.fromList(listPo);
    defectPoints.add(e2);

    Log.d(TAG, "hull: " + hull.toList());
    Log.d(TAG, "defects: " + convexDefect.toList());

    Imgproc.drawContours(mRgba, hullPoints, -1, CONTOUR_COLOR, 3);

    int defectsTotal = (int) convexDefect.total();
    Log.d(TAG, "Defect total " + defectsTotal);

    this.numberOfFingers = listPoDefect.size();
    if (this.numberOfFingers > 5)
        this.numberOfFingers = 5;

    mHandler.post(mUpdateFingerCountResults);

    for (Point p : listPoDefect) {
        Imgproc.circle(mRgba, p, 6, new Scalar(255, 0, 255));
    }

    return mRgba;
}