kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
- processImage();
+ findContours();
}
public static void findContours() {
filtered = matOriginal;
- // bing bing bong bong
- // bing bing bong bong
Imgproc.cvtColor(matOriginal, filtered, Imgproc.COLOR_BGR2GRAY);
+ // takes out unnecessary colors
+
Imgproc.GaussianBlur(matOriginal, filtered, new Size(5, 5), Core.BORDER_DEFAULT);
+
+ // bing bing bong bong
+ // bing bing bong bong
+
+ // converts image to grayscale, blurs it, and detects edges
+
Imgproc.Canny(matOriginal, filtered, 35, 125);
+ 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;
}
public static void processImage() {