List of usage examples for org.opencv.core MatOfPoint2f MatOfPoint2f
public MatOfPoint2f(Point... a)
From source file:logic.featurepointextractor.MouthFPE.java
/** * Detect mouth feature points/*w w w . ja v a 2 s .c o m*/ * Algorithm: Equalize histogram of mouth rect * Implement Sobel horizontal filter * Find corners * Invert color + Binarization * Find lip up and down points * @param mc * @return */ @Override public Point[] detect(MatContainer mc) { /**Algorithm * find pix(i) = (R-G)/R * normalize: 2arctan(pix(i))/pi */ //find pix(i) = (R-G)/R Mat mouthRGBMat = mc.origFrame.submat(mc.mouthRect); List mouthSplitChannelsList = new ArrayList<Mat>(); Core.split(mouthRGBMat, mouthSplitChannelsList); //extract R-channel Mat mouthR = (Mat) mouthSplitChannelsList.get(2); mouthR.convertTo(mouthR, CvType.CV_64FC1); //extract G-channel Mat mouthG = (Mat) mouthSplitChannelsList.get(1); mouthG.convertTo(mouthG, CvType.CV_64FC1); //calculate (R-G)/R Mat dst = new Mat(mouthR.rows(), mouthR.cols(), CvType.CV_64FC1); mc.mouthProcessedMat = new Mat(mouthR.rows(), mouthR.cols(), CvType.CV_64FC1); Core.absdiff(mouthR, mouthG, dst); // Core.divide(dst, mouthR, mc.mouthProcessedMat); mc.mouthProcessedMat = dst; mc.mouthProcessedMat.convertTo(mc.mouthProcessedMat, CvType.CV_8UC1); Imgproc.equalizeHist(mc.mouthProcessedMat, mc.mouthProcessedMat); // Imgproc.blur(mc.mouthProcessedMat, mc.mouthProcessedMat, new Size(4,4)); // Imgproc.morphologyEx(mc.mouthProcessedMat, mc.mouthProcessedMat, Imgproc.MORPH_OPEN, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(4,4))); Imgproc.threshold(mc.mouthProcessedMat, mc.mouthProcessedMat, 230, 255, THRESH_BINARY); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(mc.mouthProcessedMat, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); //find the biggest contour int maxSize = -1; int tmpSize = -1; int index = -1; Rect centMouthRect = new Rect(mc.mouthRect.x + mc.mouthRect.width / 4, mc.mouthRect.y + mc.mouthRect.height / 4, mc.mouthRect.width / 2, mc.mouthRect.height / 2); if (contours.size() != 0) { maxSize = contours.get(0).toArray().length; tmpSize = 0; index = 0; } //find max contour for (int j = 0; j < contours.size(); ++j) { //if contour is vertical, exclude it Rect boundRect = Imgproc.boundingRect(contours.get(j)); int centX = mc.mouthRect.x + boundRect.x + boundRect.width / 2; int centY = mc.mouthRect.y + boundRect.y + boundRect.height / 2; // LOG.info("Center = " + centX + "; " + centY); // LOG.info("Rect = " + centMouthRect.x + "; " + centMouthRect.y); if (!centMouthRect.contains(new Point(centX, centY))) continue; tmpSize = contours.get(j).toArray().length; LOG.info("Contour " + j + "; size = " + tmpSize); if (tmpSize > maxSize) { maxSize = tmpSize; index = j; } } //appproximate curve Point[] p1 = contours.get(index).toArray(); MatOfPoint2f p2 = new MatOfPoint2f(p1); MatOfPoint2f p3 = new MatOfPoint2f(); Imgproc.approxPolyDP(p2, p3, 1, true); p1 = p3.toArray(); MatOfInt tmpMatOfPoint = new MatOfInt(); Imgproc.convexHull(new MatOfPoint(p1), tmpMatOfPoint); Rect boundRect = Imgproc.boundingRect(new MatOfPoint(p1)); if (boundRect.area() / mc.mouthRect.area() > 0.3) return null; int size = (int) tmpMatOfPoint.size().height; Point[] _p1 = new Point[size]; int[] a = tmpMatOfPoint.toArray(); _p1[0] = new Point(p1[a[0]].x + mc.mouthRect.x, p1[a[0]].y + mc.mouthRect.y); Core.circle(mc.origFrame, _p1[0], 3, new Scalar(0, 0, 255), -1); for (int i = 1; i < size; i++) { _p1[i] = new Point(p1[a[i]].x + mc.mouthRect.x, p1[a[i]].y + mc.mouthRect.y); Core.circle(mc.origFrame, _p1[i], 3, new Scalar(0, 0, 255), -1); Core.line(mc.origFrame, _p1[i - 1], _p1[i], new Scalar(255, 0, 0), 2); } Core.line(mc.origFrame, _p1[size - 1], _p1[0], new Scalar(255, 0, 0), 2); /* contours.set(index, new MatOfPoint(_p1)); mc.mouthProcessedMat.setTo(new Scalar(0)); Imgproc.drawContours(mc.mouthProcessedMat, contours, index, new Scalar(255), -1); */ mc.mouthMatOfPoint = _p1; MatOfPoint matOfPoint = new MatOfPoint(_p1); mc.mouthBoundRect = Imgproc.boundingRect(matOfPoint); mc.features.mouthBoundRect = mc.mouthBoundRect; /**extract feature points: 1 most left * 2 most right * 3,4 up * 5,6 down */ // mc.mouthMatOfPoint = extractFeaturePoints(contours.get(index)); return null; }
From source file:objectdetection.ObjectDetector.java
public void findObjects() { preProcessImg();/*from ww w . j a v a 2 s .c om*/ Imgproc.findContours(imgCanny, contours, imgCanny, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); for (MatOfPoint mop : contours) { MatOfPoint2f m2p; m2p = new MatOfPoint2f(mop.toArray()); Double peri = Imgproc.arcLength(m2p, true); Imgproc.approxPolyDP(m2p, m2p, 0.02 * peri, true); //Imgproc.drawContours(img, contours, -1, new Scalar(0, 0, 255), 2); float area = img.width() * img.height(); Rect rect = Imgproc.boundingRect(mop); objList.add(rect); //if (rect.height * rect.width > area*5/100) { Imgproc.rectangle(img, rect.tl(), rect.br(), new Scalar(255, 0, 0)); //} } Collections.sort(objList, new Comparator<Rect>() { @Override public int compare(Rect r1, Rect r2) { return (int) (r2.area() - r1.area()); } }); List<Rect> arr = objList; Rect bigRect = arr.get(0); Rect bigRect2 = arr.get(1); while (!equals(bigRect, bigRect2)) { bigRect2 = bigRect; for (int i = 1; i < arr.size(); ++i) { if (doOverlap(bigRect, arr.get(i))) { bigRect = union(bigRect, arr.get(i)); arr.remove(i); break; } } } Imgproc.rectangle(img, bigRect.tl(), bigRect.br(), new Scalar(255, 255, 0)); mainRect = bigRect; }
From source file:org.akvo.caddisfly.sensor.colorimetry.strip.util.OpenCVUtil.java
License:Open Source License
private static Mat transformMatrix(double[] p1Src, double[] p2Src, double[] p3Src, double[] p4Src, double[] p1Dst, double[] p2Dst, double[] p3Dst, double[] p4Dst) { //source quad Point[] srcQuad = new Point[4]; //destination quad corresponding with srcQuad Point[] dstQuad = new Point[4]; srcQuad[0] = new Point(p1Src[0], p1Src[1]); srcQuad[1] = new Point(p2Src[0], p2Src[1]); srcQuad[2] = new Point(p3Src[0], p3Src[1]); srcQuad[3] = new Point(p4Src[0], p4Src[1]); dstQuad[0] = new Point(p1Dst[0], p1Dst[1]); dstQuad[1] = new Point(p2Dst[0], p2Dst[1]); dstQuad[2] = new Point(p3Dst[0], p3Dst[1]); dstQuad[3] = new Point(p4Dst[0], p4Dst[1]); //srcQuad and destQuad to MatOfPoint2f objects, needed in perspective transform MatOfPoint2f srcMat2f = new MatOfPoint2f(srcQuad); MatOfPoint2f dstMat2f = new MatOfPoint2f(dstQuad); //get a perspective transform matrix return Imgproc.getPerspectiveTransform(srcMat2f, dstMat2f); }
From source file:org.lasarobotics.vision.detection.objects.Contour.java
License:Open Source License
/** * Get data as a double * * @return OpenCV matrix of points */ public MatOfPoint2f getDoubleData() { return new MatOfPoint2f(mat.toArray()); }
From source file:org.lasarobotics.vision.detection.PrimitiveDetection.java
License:Open Source License
/** * Locate ellipses within an image/*from w w w . j ava2 s . co m*/ * * @param grayImage Grayscale image * @return Ellipse locations */ public static EllipseLocationResult locateEllipses(Mat grayImage) { Mat gray = grayImage.clone(); Filter.downsample(gray, 2); Filter.upsample(gray, 2); Imgproc.Canny(gray, gray, 5, 75, 3, true); Filter.dilate(gray, 2); Mat cacheHierarchy = new Mat(); List<MatOfPoint> contoursTemp = new ArrayList<>(); //Find contours - the parameters here are very important to compression and retention Imgproc.findContours(gray, contoursTemp, cacheHierarchy, Imgproc.CV_RETR_TREE, Imgproc.CHAIN_APPROX_TC89_KCOS); //List contours List<Contour> contours = new ArrayList<>(); for (MatOfPoint co : contoursTemp) { contours.add(new Contour(co)); } //Find ellipses by finding fit List<Ellipse> ellipses = new ArrayList<>(); for (MatOfPoint co : contoursTemp) { contours.add(new Contour(co)); //Contour must have at least 6 points for fitEllipse if (co.toArray().length < 6) continue; //Copy MatOfPoint to MatOfPoint2f MatOfPoint2f matOfPoint2f = new MatOfPoint2f(co.toArray()); //Fit an ellipse to the current contour Ellipse ellipse = new Ellipse(Imgproc.fitEllipse(matOfPoint2f)); //Draw ellipse ellipses.add(ellipse); } return new EllipseLocationResult(contours, ellipses); }
From source file:processdata.ExperimentalDataProcessingUI.java
private void jButtonProcessImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonProcessImageActionPerformed try {/*from ww w . ja va 2 s.co m*/ // 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 ww w. j a v a 2 s .c o 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; }
From source file:uom.research.thalassemia.logic.BloodCellData.java
/** * get min and max diameters./* w w w . j a va 2s. co 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.//from ww w . j a va 2 s . co m * */ 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.// w w w. ja 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); }