Example usage for org.opencv.core MatOfPoint copyTo

List of usage examples for org.opencv.core MatOfPoint copyTo

Introduction

In this page you can find the example usage for org.opencv.core MatOfPoint copyTo.

Prototype

public void copyTo(Mat m) 

Source Link

Usage

From source file:uom.research.thalassemia.logic.BloodCellData.java

/**
 * get min and max diameters.//from w ww .j ava 2  s.c o m
 *
 * @param pcontours contours
 */
private void getMinMaxDiameter(final List<MatOfPoint> pcontours) {
    MatOfPoint allcontours = new MatOfPoint();
    List<Double> diameters = new ArrayList<>();
    for (MatOfPoint mat : pcontours) {
        mat.copyTo(allcontours);
        RotatedRect boundingEllipse;
        if (allcontours.toArray().length > 4) {
            MatOfPoint2f newMat2 = new MatOfPoint2f(allcontours.toArray());
            boundingEllipse = Imgproc.fitEllipse(newMat2);
            //ellipse centre x cordination
            double xx = boundingEllipse.center.x;
            //ellipse centre y cordination
            double yy = boundingEllipse.center.y;
            //ellipse width
            double width = boundingEllipse.size.width;
            //ellipse height
            double height = boundingEllipse.size.height;
            diameters.add(width);
        }
    }
    if (diameters.size() > 1) {
        diameters.sort(null);
    }
    minDiameter = diameters.get(0);
    maxDiameter = diameters.get(diameters.size() - 1);
}

From source file:uom.research.thalassemia.logic.BloodCellDataProcessor.java

/**
 * process circular blood cell data including both pallor and red blood
 * cells.//  ww  w . j  a v  a2s .  com
 *
 */
public void ellipseBloodCellsProcesser() {
    ellipseBloodCellsArray = new ArrayList<>();
    int index = 0;
    MatOfPoint allcontours = new MatOfPoint();
    for (MatOfPoint mat : contours) {
        mat.copyTo(allcontours);
        RotatedRect boundingEllipse;
        if (allcontours.toArray().length > FOUR) {
            MatOfPoint2f newMat2 = new MatOfPoint2f(allcontours.toArray());
            boundingEllipse = Imgproc.fitEllipse(newMat2);
            //ellipse centre x cordination
            double xx, yy, rr, width, height, area, perimeter, diameter, deviationValue, areaPreparation, sgf;

            xx = boundingEllipse.center.x;
            //ellipse centre y cordination
            yy = boundingEllipse.center.y;
            //ellipse width
            width = boundingEllipse.size.width;
            //ellipse height
            height = boundingEllipse.size.height;
            // assume radius is width. width is the hightest length.
            rr = width;
            sgf = width / height;
            //get area value
            area = calculateArea(width, height);
            //get perimeter value
            perimeter = calculatePerimeter(width, height);
            //get diameter value
            diameter = calculateDiameter(area, perimeter);
            // calculate deviational value
            if (rr > 0) {
                deviationValue = sgf / area;

                Map<Point, Double> points = getPallorBloodCellsPointList();
                areaPreparation = 0;

                Point point = new Point(xx, yy);
                if (points.containsKey(point)) {
                    areaPreparation = calculateArea(points.get(point)) / area;
                }

                Object[] ob = { (++index), xx, yy, rr, Validator.formatDouble(perimeter),
                        Validator.formatDouble(area), Validator.formatDouble(diameter),
                        Validator.formatDouble(deviationValue), Validator.formatDouble(areaPreparation) };

                totalEllipseArea += area;
                ellipseBloodCellsArray.add(ob);
            }
        }
    }
}

From source file:uom.research.thalassemia.logic.BloodCellDataProcessor.java

/**
 * get min and max diameters./*from  w  w  w .  j a  v  a 2s.c o m*/
 *
 * @param pcontours contours
 */
private void getMinMaxDiameter(final List<MatOfPoint> pcontours) {
    MatOfPoint allcontours = new MatOfPoint();
    List<Double> diameters = new ArrayList<>();
    for (MatOfPoint mat : pcontours) {
        mat.copyTo(allcontours);
        RotatedRect boundingEllipse;
        if (allcontours.toArray().length > 4) {
            MatOfPoint2f newMat2 = new MatOfPoint2f(allcontours.toArray());
            boundingEllipse = Imgproc.fitEllipse(newMat2);
            //ellipse centre x cordination
            double x = boundingEllipse.center.x;
            //ellipse centre y cordination
            double y = boundingEllipse.center.y;
            //ellipse width
            double width = boundingEllipse.size.width;
            //ellipse height
            double height = boundingEllipse.size.height;
            diameters.add(width);
        }
    }
    if (diameters.size() > 1) {
        diameters.sort(null);
    }
    minDiameter = diameters.get(0);
    maxDiameter = diameters.get(diameters.size() - 1);
}

From source file:uom.research.thalassemia.logic.BloodCellsManipulationImpl.java

/**
 * do Blood Cell Processing./*from w w  w.  j  a  v a 2 s.c  om*/
 *
 * @throws Exception Exception
 */
@Override
public void doBloodCellProcessing() throws Exception {
    if (imageFile.isFile()) {
        // Load Native Library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        // Load Selected File onto Original Mat
        original = Imgcodecs.imread(imageFile.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);

        // Create Mats with Original Mat's Size
        gray = original.clone();
        smooth = original.clone();
        canny = original.clone();
        threshold = original.clone();
        red = original.clone();
        laplacian = original.clone();
        circularsMat = original.clone();
        ellipsesMat = original.clone();

        // Sets Gray Scale to gray Map
        Imgproc.cvtColor(original, gray, Imgproc.COLOR_BGR2HSV);

        grayImage = convertMapToImage(gray);

        // Sest Red Colour Range Min and Max
        Scalar minc = new Scalar(95, 150, 75, 0);
        Scalar maxc = new Scalar(145, 255, 255, 0);

        // Sets Red Colour Mat for Given Range
        Core.inRange(original, maxc, maxc, red);

        redImage = convertMapToImage(red);

        int type = BufferedImage.TYPE_INT_RGB;
        //gray = Imgcodecs.imdecode(original, type);
        image = new BufferedImage(original.cols(), original.rows(), type);

        //Imgproc.GaussianBlur(mGray, mGray, new Size(15,15),50);
        //redImage = new BufferedImage(original.width(), original.height(), BufferedImage.TYPE_INT_RGB);
        //byte[] data2 = ((DataBufferByte) redImage.getRaster().getDataBuffer()).getData();
        Imgproc.threshold(gray, threshold, 155, 255, Imgproc.THRESH_BINARY);

        //Imgproc.Laplacian(gray, laplacian, CvType.CV_8UC3);//CV_8UC1);
        //laplacianImage = convertMapToImage(laplacian);
        thresholdImage = convertMapToImage(threshold);

        Imgproc.Canny(threshold, canny, 50, 100, 3, false);
        cannyImage = convertMapToImage(canny);

        //Imgproc.cvtColor(canny, canny, Imgproc.COLOR_BGR2GRAY);
        circles = new Mat();

        int imageArea = original.width() * original.height();

        if (imageArea > 5100000) {
            Imgproc.HoughCircles(canny, circles, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio
                    220, //Minimum distance between the centers of the detected circles. default 100
                    100, //Higher threshold for canny edge detector. default100
                    200, //Threshold at the center detection stage. default 200
                    90, //min radius. default 50
                    130 //max radius. default 90
            );
        } else if (imageArea > 1309500) {
            Imgproc.HoughCircles(canny, circles, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio
                    110, //Minimum distance between the centers of the detected circles. default 100
                    100, //Higher threshold for canny edge detector. default100
                    150, //Threshold at the center detection stage. default 200
                    50, //min radius. default 50
                    67 //max radius. default 90
            );
        } else if (imageArea > 540000) {
            Imgproc.HoughCircles(canny, circles, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio
                    110, //Minimum distance between the centers of the detected circles. default 100
                    100, //Higher threshold for canny edge detector. default100
                    200, //Threshold at the center detection stage. default 200
                    30, //min radius. default 50
                    65 //max radius. default 90
            );
        } else if (imageArea > 400000) {
            Imgproc.HoughCircles(canny, circles, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio
                    50, //Minimum distance between the centers of the detected circles. default 100
                    60, //Higher threshold for canny edge detector. default100
                    100, //Threshold at the center detection stage. default 200
                    25, //min radius. default 50
                    40 //max radius. default 90
            );
        }

        if (circles.cols() > 0) {
            for (int x = 0; x < circles.cols(); x++) {
                double vCircle[] = circles.get(0, x);
                if (vCircle == null) {
                    break;
                }
                Point pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));
                int radius = (int) Math.round(vCircle[2]);
                radiusList.add(radius);
                // draw the found circle
                Imgproc.circle(circularsMat, pt, radius, new Scalar(0, 255, 0), 3);
            }
        }

        // set Pallor circles
        circlesPallor = new Mat();
        if (imageArea > 5100000) {
            Imgproc.HoughCircles(canny, circlesPallor, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio
                    220, //Minimum distance between the centers of the detected circles
                    100, //Higher threshold for canny edge detector
                    150, //Threshold at the center detection stage
                    0, //min radius
                    70 //max radius
            );
        } else if (imageArea > 1309500) {
            Imgproc.HoughCircles(canny, circlesPallor, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio
                    110, //Minimum distance between the centers of the detected circles
                    50, //Higher threshold for canny edge detector
                    100, //Threshold at the center detection stage
                    0, //min radius
                    45 //max radius
            );
        } else if (imageArea > 540000) {
            Imgproc.HoughCircles(canny, circlesPallor, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio
                    100, //Minimum distance between the centers of the detected circles
                    50, //Higher threshold for canny edge detector
                    100, //Threshold at the center detection stage
                    0, //min radius
                    35 //max radius
            );

        } else if (imageArea > 400000) {
            Imgproc.HoughCircles(canny, circlesPallor, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio
                    50, //Minimum distance between the centers of the detected circles
                    10, //Higher threshold for canny edge detector
                    30, //Threshold at the center detection stage
                    0, //min radius
                    25 //max radius
            );

        }
        if (circlesPallor.cols() > 0) {
            for (int x = 0; x < circlesPallor.cols(); x++) {
                double vCircle[] = circlesPallor.get(0, x);
                if (vCircle == null) {
                    break;
                }
                Point pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));
                int radius = (int) Math.round(vCircle[2]);
                //radiusList.add(radius);
                // draw the found circle
                Imgproc.circle(circularsMat, pt, radius, new Scalar(0, 0, 255), 0);
            }
        }

        // set Ellipses
        contours = new ArrayList<>();
        Imgproc.findContours(canny, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
        MatOfPoint allcontours = new MatOfPoint();
        for (MatOfPoint mat : contours) {
            mat.copyTo(allcontours);
            RotatedRect boundingEllipse = null;
            if (allcontours.toArray().length > 4) {
                MatOfPoint newMat1 = new MatOfPoint(allcontours.toArray());
                MatOfPoint2f newMat2 = new MatOfPoint2f(allcontours.toArray());
                Rect boundingRect = Imgproc.boundingRect(newMat1);
                boundingEllipse = Imgproc.fitEllipse(newMat2);
            }

            if (boundingEllipse != null) {
                Imgproc.ellipse(ellipsesMat, boundingEllipse, new Scalar(255, 0, 0), 2);
            }
        }

        circleCount = circles.cols();
        circularsImage = convertMapToImage(circularsMat);
        ellipsesImage = convertMapToImage(ellipsesMat);
    }
}