+ Imgproc.findContours(filtered, contours, matHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
+
+ for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) {
+ MatOfPoint matOfPoint = iterator.next();
+ Rect rec = Imgproc.boundingRect(matOfPoint);
+ if (rec.height < THE_BEST_MAGIC_NUMBER || rec.width < THE_BEST_MAGIC_NUMBER) {
+
+ iterator.remove();
+ }
+
+ }
+ Rect largestRec = null;
+ double largestRecArea = 0;
+
+ for (MatOfPoint mop : contours) {
+ double recArea = Imgproc.contourArea(mop);
+ if (largestRecArea < recArea) {
+ largestRec = Imgproc.boundingRect(mop);
+ largestRecArea = recArea;
+ }
+ }
+ Imgproc.rectangle(matOriginal, largestRec.br(), largestRec.tl(), GRAY, 1);
+
+ Imgcodecs.imwrite("output.png", filtered);
+
+ }
+
+ public static double distanceToCamera(double knownWidth, int focalLength, double perWidth) {
+ return (knownWidth * ((double) (focalLength))) / perWidth;