X-Git-Url: http://challenge-bot.com/repos/?a=blobdiff_plain;f=IsolateImage.java;h=7586c82f25a9d9753c0623f54c56c9c4e4db400b;hb=0e1260e3edcfa502c5e706f0c17928e3d767a8c9;hp=18b76a2c11c9908f6b6066e8171e63b161d66176;hpb=c621c3781d69539d81d8d8ac174511dddb82ba7c;p=3501%2FOpenCVShowImage diff --git a/IsolateImage.java b/IsolateImage.java index 18b76a2..7586c82 100644 --- a/IsolateImage.java +++ b/IsolateImage.java @@ -35,7 +35,7 @@ public class IsolateImage { kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3)); - processImage(); + findContours(); } public static void findContours() { @@ -50,12 +50,47 @@ public class IsolateImage { 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 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() {