Example usage for org.opencv.imgproc Imgproc morphologyEx

List of usage examples for org.opencv.imgproc Imgproc morphologyEx

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc morphologyEx.

Prototype

public static void morphologyEx(Mat src, Mat dst, int op, Mat kernel, Point anchor, int iterations) 

Source Link

Usage

From source file:classes.BlobsFinder.java

public void findBlobContours() {

    Mat grayImage = new Mat();
    Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY);
    ImageUtils.saveImage(grayImage, outImageName + "_grayImage.png", request);

    Mat gaussianImage = new Mat();
    Imgproc.GaussianBlur(grayImage, gaussianImage, new Size(0, 0), 3);
    Core.addWeighted(grayImage, 1.5, gaussianImage, -1, 0, gaussianImage);
    ImageUtils.saveImage(gaussianImage, outImageName + "_gaussianGrayImage.png", request);

    Mat binaryImage = new Mat();
    Imgproc.adaptiveThreshold(gaussianImage, binaryImage, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
            Imgproc.THRESH_BINARY_INV, 15, 4);
    ImageUtils.saveImage(binaryImage, outImageName + "_binaryImage.png", request);

    Mat erodedImage = new Mat();

    binaryImage.copyTo(erodedImage);//  ww  w.ja v a 2  s.c o m

    Mat structuringElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
    Point anchor = new Point(-1, -1);

    Imgproc.morphologyEx(erodedImage, erodedImage, Imgproc.MORPH_CLOSE, structuringElement, anchor, 1);
    ImageUtils.saveImage(erodedImage, outImageName + "_erodedImage.png", request);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Imgproc.findContours(erodedImage, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    Mat originalContoursImage = new Mat(image.size(), CvType.CV_8UC1, new Scalar(0));
    Scalar contourColor = new Scalar(255);
    int thickness = -1; // Thicknes should be lower than zero in order to drawn the filled contours
    Imgproc.drawContours(originalContoursImage, contours, -1, contourColor, thickness); // Drawing all the contours found
    ImageUtils.saveImage(originalContoursImage, outImageName + "_originalContoursImage.png", request);

    Mat erodedContoursImage = new Mat();
    Imgproc.erode(originalContoursImage, erodedContoursImage, structuringElement, anchor, 1);
    ImageUtils.saveImage(erodedContoursImage, outImageName + "_erodedContoursImage.png", request);

    ArrayList<MatOfPoint> finalContours = new ArrayList<MatOfPoint>();
    Mat finalContourImage = new Mat(image.size(), CvType.CV_8UC1, new Scalar(0));
    Imgproc.findContours(erodedContoursImage, finalContours, new Mat(), Imgproc.RETR_EXTERNAL,
            Imgproc.CHAIN_APPROX_SIMPLE);

    for (int i = 0; i < finalContours.size(); i++) {
        MatOfPoint currentContour = finalContours.get(i);
        double area = Imgproc.contourArea(currentContour);
        if (area > MIN_AREA) {

            validContours.add(currentContour);

            String fabricPath = generateFabricPathString(currentContour);
            contourPaths.add(fabricPath);

            Rect boundingRect = Imgproc.boundingRect(currentContour);
            topLeftCorners.add(boundingRect.tl());

            contoursAreas.add(area);
        }
    }

    // Drawing ALL the valid contours
    Imgproc.drawContours(finalContourImage, validContours, -1, contourColor, thickness);
    ImageUtils.saveImage(finalContourImage, outImageName + "_finalContourImage.png", request);

}

From source file:classes.FloodFiller.java

private void fillFrom(Point seed, int lo, int up, Scalar backgroundColor, Scalar contourFillingColor) {

    Mat object = ObjectGenerator.extract(image, seed.x, seed.y, 10, 10);
    this.meanColor = Core.mean(object);

    Rect ccomp = new Rect();
    Mat mask = Mat.zeros(image.rows() + 2, image.cols() + 2, CvType.CV_8UC1);

    int connectivity = 4;
    int newMaskVal = 255;
    int ffillMode = 1;

    int flags = connectivity + (newMaskVal << 8) + (ffillMode == 1 ? Imgproc.FLOODFILL_FIXED_RANGE : 0);

    Scalar newVal = new Scalar(0.299, 0.587, 0.114);

    Imgproc.threshold(mask, mask, 1, 128, Imgproc.THRESH_BINARY);

    filledArea = Imgproc.floodFill(image.clone(), mask, seed, newVal, ccomp, new Scalar(lo, lo, lo),
            new Scalar(up, up, up), flags);

    //        Highgui.imwrite("mask.png", mask);
    ImageUtils.saveImage(mask, "mask.png", request);

    morphologicalImage = new Mat(image.size(), CvType.CV_8UC3);

    Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1));

    ArrayList<Mat> mask3 = new ArrayList<Mat>();
    mask3.add(mask);// w  w w.j  a  v a  2 s .c om
    mask3.add(mask);
    mask3.add(mask);
    Core.merge(mask3, mask);

    // Applying morphological filters
    Imgproc.erode(mask, morphologicalImage, element);
    Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_CLOSE, element,
            new Point(-1, -1), 9);
    Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_OPEN, element, new Point(-1, -1),
            2);
    Imgproc.resize(morphologicalImage, morphologicalImage, image.size());

    //        Highgui.imwrite("morphologicalImage.png", morphologicalImage);
    ImageUtils.saveImage(morphologicalImage, "morphologicalImage.png", request);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Core.split(mask, mask3);
    Mat binarymorphologicalImage = mask3.get(0);

    Imgproc.findContours(binarymorphologicalImage.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL,
            Imgproc.CHAIN_APPROX_NONE);

    contoursImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor);

    int thickness = -1; // Thicknes should be lower than zero in order to drawn the filled contours
    Imgproc.drawContours(contoursImage, contours, -1, contourFillingColor, thickness); // Drawing all the contours found
    //        Highgui.imwrite("allContoursImage.png", contoursImage);
    ImageUtils.saveImage(contoursImage, "allContoursImage.png", request);

    if (contours.size() > 1) {

        int minContourWith = 20;
        int minContourHeight = 20;
        int maxContourWith = 6400 / 2;
        int maxContourHeight = 4800 / 2;

        contours = filterContours(contours, minContourWith, minContourHeight, maxContourWith, maxContourHeight);
    }

    if (contours.size() > 0) {

        MatOfPoint biggestContour = contours.get(0); // getting the biggest contour
        contourArea = Imgproc.contourArea(biggestContour);

        if (contours.size() > 1) {
            biggestContour = Collections.max(contours, new ContourComparator()); // getting the biggest contour in case there are more than one
        }

        Point[] points = biggestContour.toArray();
        path = "M " + (int) points[0].x + " " + (int) points[0].y + " ";
        for (int i = 1; i < points.length; ++i) {
            Point v = points[i];
            path += "L " + (int) v.x + " " + (int) v.y + " ";
        }
        path += "Z";

        biggestContourImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor);

        Imgproc.drawContours(biggestContourImage, contours, 0, contourFillingColor, thickness);

        //            Highgui.imwrite("biggestContourImage.png", biggestContourImage);
        ImageUtils.saveImage(biggestContourImage, "biggestContourImage.png", request);

        Mat maskForColorExtraction = biggestContourImage.clone();

        if (isWhite(backgroundColor)) {
            Imgproc.dilate(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3);
        } else {
            Imgproc.erode(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3);
        }

        //            Highgui.imwrite("maskForColorExtraction.png", maskForColorExtraction);
        ImageUtils.saveImage(maskForColorExtraction, "maskForColorExtraction.png", request);

        Mat extractedColor = new Mat();

        if (isBlack(backgroundColor) && isWhite(contourFillingColor)) {
            Core.bitwise_and(maskForColorExtraction, image, extractedColor);

        } else {
            Core.bitwise_or(maskForColorExtraction, image, extractedColor);
        }

        //            Highgui.imwrite("extractedColor.png", extractedColor);
        ImageUtils.saveImage(extractedColor, "extractedColor.png", request);

        computedSearchWindow = Imgproc.boundingRect(biggestContour);
        topLeftCorner = computedSearchWindow.tl();

        Rect croppingRect = new Rect(computedSearchWindow.x, computedSearchWindow.y,
                computedSearchWindow.width - 1, computedSearchWindow.height - 1);

        Mat imageForTextRecognition = new Mat(extractedColor.clone(), croppingRect);
        //            Highgui.imwrite(outImageName, imageForTextRecognition);
        ImageUtils.saveImage(imageForTextRecognition, outImageName, request);

        //            
        //
        //            Mat data = new Mat(imageForTextRecognition.size(), CvType.CV_8UC3, backgroundColor);
        //            imageForTextRecognition.copyTo(data);
        //            data.convertTo(data, CvType.CV_8UC3);
        //
        //            // The meanColor variable represents the color in the GBR space, the following line transforms this to the RGB color space, which
        //            // is assumed in the prepareImage method of the TextRecognitionPreparer class
        //            Scalar userColor = new Scalar(meanColor.val[2], meanColor.val[1], meanColor.val[0]);
        //
        //            ArrayList<String> recognizableImageNames = TextRecognitionPreparer.generateRecognizableImagesNames(data, backgroundColor, userColor);
        //            for (String imageName : recognizableImageNames) {
        //
        //                try {
        //                    // First recognition step
        //                    String recognizedText = TextRecognizer.recognize(imageName, true).trim();
        //                    if (recognizedText != null && !recognizedText.isEmpty()) {
        //                        recognizedStrings.add(recognizedText);
        //                    }
        //                    // Second recognition step
        //                    recognizedText = TextRecognizer.recognize(imageName, false).trim();
        //                    if (recognizedText != null && !recognizedText.isEmpty()) {
        //                        recognizedStrings.add(recognizedText);
        //                    }
        //                    
        //                } catch (Exception e) {
        //                }
        //            }
        //            
        ////            ArrayList<BufferedImage> recognizableBufferedImages = TextRecognitionPreparer.generateRecognizableBufferedImages(data, backgroundColor, userColor);
        ////            for (BufferedImage bufferedImage : recognizableBufferedImages) {
        ////                try {
        ////                    // First recognition step
        ////                    String recognizedText = TextRecognizer.recognize(bufferedImage, true).trim();
        ////                    if (recognizedText != null && !recognizedText.isEmpty()) {
        ////                        recognizedStrings.add(recognizedText);
        ////                    }
        ////                    // Second recognition step
        ////                    recognizedText = TextRecognizer.recognize(bufferedImage, false).trim();
        ////                    if (recognizedText != null && !recognizedText.isEmpty()) {
        ////                        recognizedStrings.add(recognizedText);
        ////                    }
        ////                    
        ////                } catch (Exception e) {
        ////                }
        ////            }
        //
        //            
        //            

        // compute all moments
        Moments mom = Imgproc.moments(biggestContour);
        massCenter = new Point(mom.get_m10() / mom.get_m00(), mom.get_m01() / mom.get_m00());

        // draw black dot
        Core.circle(contoursImage, massCenter, 4, contourFillingColor, 8);
    }

}

From source file:classes.ObjectFinder.java

private void applyMorphologicalFilters() {
    Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1));
    Imgproc.erode(thresholdedBackprojection, morphologicalImage, element);
    Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_CLOSE, element,
            new Point(-1, -1), 2);
    Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_OPEN, element, new Point(-1, -1),
            2);//from  w w  w.j  a v  a  2s . c  o m
}

From source file:com.astrocytes.core.operationsengine.OperationsImpl.java

License:Open Source License

@Override
public Mat applyMathMorphology(Integer radius) {
    Mat dest = new Mat();
    int instrumentSize = radius * 2 + 1;
    Mat kernel = getStructuringElement(Imgproc.CV_SHAPE_ELLIPSE, new Size(instrumentSize, instrumentSize),
            new Point(radius, radius));

    Imgproc.morphologyEx(currentImage, dest, MORPH_CLOSE, kernel, new Point(-1, -1), 1);

    dest.copyTo(currentImage);/*ww  w.  j  a  va 2s.c  om*/
    dest.release();
    return currentImage;
}

From source file:servlets.FillAreaByScribble.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w  w  w  .  j a  v a2s. c o  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        String imageForTextRecognition = request.getParameter("imageForTextRecognition") + ".png";
        String isSingleRegion = request.getParameter("isSingleRegion");
        boolean makeSingleRegion = isSingleRegion.toLowerCase().equals("true");

        Mat original = ImageUtils.loadImage(imageForTextRecognition, request);
        Mat image = original.clone();
        Mat mask = Mat.zeros(image.rows() + 2, image.cols() + 2, CvType.CV_8UC1);

        String samplingPoints = request.getParameter("samplingPoints");

        Gson gson = new Gson();
        Point[] tmpPoints = gson.fromJson(samplingPoints, Point[].class);

        ArrayList<Point> userPoints = new ArrayList<Point>(Arrays.asList(tmpPoints));

        Mat userPointsImage = image.clone();

        ArrayList<Mat> maskRegions = new ArrayList<>();

        Random random = new Random();
        int b = random.nextInt(256);
        int g = random.nextInt(256);
        int r = random.nextInt(256);
        Scalar newVal = new Scalar(b, g, r);
        FloodFillFacade floodFillFacade = new FloodFillFacade();

        int k = 0;

        for (int i = 0; i < userPoints.size(); i++) {
            Point point = userPoints.get(i);

            image = floodFillFacade.fill(image, mask, (int) point.x, (int) point.y, newVal);

            Mat seedImage = original.clone();
            Core.circle(seedImage, point, 9, new Scalar(0, 0, 255), -1);
            Core.putText(userPointsImage, "" + k, new Point(point.x + 5, point.y + 5), 3, 0.5,
                    new Scalar(0, 0, 0));
            //                ImageUtils.saveImage(seedImage, "mask_" + k + "_seed" + imageForTextRecognition + ".png", request);

            if (!makeSingleRegion) {
                Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1));
                Imgproc.morphologyEx(mask, mask, Imgproc.MORPH_CLOSE, element, new Point(-1, -1), 3);
                Imgproc.resize(mask, mask, original.size());
            }

            //                ImageUtils.saveImage(mask, "mask_" + k + "" + imageForTextRecognition + ".png", request);

            Mat dilatedMask = new Mat();

            int elementSide = 21;
            Mat element = new Mat(elementSide, elementSide, CvType.CV_8U, new Scalar(1));
            Imgproc.morphologyEx(mask, dilatedMask, Imgproc.MORPH_DILATE, element, new Point(-1, -1), 1);
            Imgproc.resize(dilatedMask, dilatedMask, original.size());

            //                ImageUtils.saveImage(dilatedMask, "mask_" + k + "_dilated" + imageForTextRecognition + ".png", request);

            maskRegions.add(mask);

            if (!makeSingleRegion) {
                int totalRemovedPoints = filterPoints(userPoints, dilatedMask);
                if (totalRemovedPoints > 0) {
                    i = -1; // so that the algorithm starts again at the first element of the userPoints array
                }
            } else {
                filterPoints(userPoints, mask);
            }

            //                System.out.println("Total points after filtering:");
            //                System.out.println(userPoints.size());

            if (!makeSingleRegion) {
                mask = Mat.zeros(original.rows() + 2, original.cols() + 2, CvType.CV_8UC1);
            }

            k++;
        }

        ArrayList<FindingResponse> findingResponses = new ArrayList<>();

        if (makeSingleRegion) {

            Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1));
            Imgproc.morphologyEx(mask, mask, Imgproc.MORPH_CLOSE, element, new Point(-1, -1), 3);

            Imgproc.resize(mask, mask, image.size());

            List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
            Imgproc.findContours(mask.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL,
                    Imgproc.CHAIN_APPROX_NONE);

            MatOfPoint biggestContour = contours.get(0); // getting the biggest contour
            double contourArea = Imgproc.contourArea(biggestContour);

            if (contours.size() > 1) {
                biggestContour = Collections.max(contours, new ContourComparator()); // getting the biggest contour in case there are more than one
            }

            Point[] biggestContourPoints = biggestContour.toArray();
            String path = "M " + (int) biggestContourPoints[0].x + " " + (int) biggestContourPoints[0].y + " ";
            for (int i = 1; i < biggestContourPoints.length; ++i) {
                Point v = biggestContourPoints[i];
                path += "L " + (int) v.x + " " + (int) v.y + " ";
            }
            path += "Z";

            //                System.out.println("path:");
            //                System.out.println(path);

            Rect computedSearchWindow = Imgproc.boundingRect(biggestContour);
            Point massCenter = computedSearchWindow.tl();

            Scalar meanColor = Core.mean(original, mask);

            //                ImageUtils.saveImage(mask, "single_mask_" + imageForTextRecognition + ".png", request);

            FindingResponse findingResponse = new FindingResponse(path, meanColor, massCenter, -1, contourArea);
            findingResponses.add(findingResponse);

        } else {

            float imageArea = image.cols() * image.rows();

            for (int j = 0; j < maskRegions.size(); j++) {
                Mat region = maskRegions.get(j);

                List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
                Imgproc.findContours(region.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL,
                        Imgproc.CHAIN_APPROX_NONE);

                MatOfPoint biggestContour = contours.get(0); // getting the biggest contour

                if (contours.size() > 1) {
                    biggestContour = Collections.max(contours, new ContourComparator()); // getting the biggest contour in case there are more than one
                }

                double contourArea = Imgproc.contourArea(biggestContour);

                if (contourArea / imageArea < 0.8) { // only areas less than 80% of that of the image are accepted

                    Point[] biggestContourPoints = biggestContour.toArray();
                    String path = "M " + (int) biggestContourPoints[0].x + " " + (int) biggestContourPoints[0].y
                            + " ";
                    for (int i = 1; i < biggestContourPoints.length; ++i) {
                        Point v = biggestContourPoints[i];
                        path += "L " + (int) v.x + " " + (int) v.y + " ";
                    }
                    path += "Z";

                    Rect computedSearchWindow = Imgproc.boundingRect(biggestContour);
                    Point massCenter = computedSearchWindow.tl();

                    //                        System.out.println("Contour area: " + contourArea);

                    Mat contoursImage = userPointsImage.clone();
                    Imgproc.drawContours(contoursImage, contours, 0, newVal, 1);

                    Scalar meanColor = Core.mean(original, region);

                    FindingResponse findingResponse = new FindingResponse(path, meanColor, massCenter, -1,
                            contourArea);
                    findingResponses.add(findingResponse);

                    //                        ImageUtils.saveImage(contoursImage, "mask_" + j + "_contourned" + imageForTextRecognition + ".png", request);

                }

            }

        }

        String jsonResponse = gson.toJson(findingResponses, ArrayList.class);

        out.println(jsonResponse);

    }
}

From source file:servlets.processScribble.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from ww  w.j ava 2s.co m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        String imageForTextRecognition = request.getParameter("imageForTextRecognition") + ".png";

        Mat original = ImageUtils.loadImage(imageForTextRecognition, request);
        Mat image = original.clone();
        Mat mask = Mat.zeros(image.rows() + 2, image.cols() + 2, CvType.CV_8UC1);

        String samplingPoints = request.getParameter("samplingPoints");

        Gson gson = new Gson();
        Point[] userPoints = gson.fromJson(samplingPoints, Point[].class);

        MatOfPoint points = new MatOfPoint(new Mat(userPoints.length, 1, CvType.CV_32SC2));
        int cont = 0;

        for (Point point : userPoints) {
            int y = (int) point.y;
            int x = (int) point.x;
            int[] data = { x, y };
            points.put(cont++, 0, data);
        }

        MatOfInt hull = new MatOfInt();
        Imgproc.convexHull(points, hull);

        MatOfPoint mopOut = new MatOfPoint();
        mopOut.create((int) hull.size().height, 1, CvType.CV_32SC2);

        int totalPoints = (int) hull.size().height;

        Point[] convexHullPoints = new Point[totalPoints];
        ArrayList<Point> seeds = new ArrayList<>();

        for (int i = 0; i < totalPoints; i++) {
            int index = (int) hull.get(i, 0)[0];
            double[] point = new double[] { points.get(index, 0)[0], points.get(index, 0)[1] };
            mopOut.put(i, 0, point);

            convexHullPoints[i] = new Point(point[0], point[1]);
            seeds.add(new Point(point[0], point[1]));

        }

        MatOfPoint mop = new MatOfPoint();
        mop.fromArray(convexHullPoints);

        ArrayList<MatOfPoint> arrayList = new ArrayList<MatOfPoint>();
        arrayList.add(mop);

        Random random = new Random();
        int b = random.nextInt(256);
        int g = random.nextInt(256);
        int r = random.nextInt(256);
        Scalar newVal = new Scalar(b, g, r);

        FloodFillFacade floodFillFacade = new FloodFillFacade();

        for (int i = 0; i < seeds.size(); i++) {
            Point seed = seeds.get(i);
            image = floodFillFacade.fill(image, mask, (int) seed.x, (int) seed.y, newVal);
        }

        Imgproc.drawContours(image, arrayList, 0, newVal, -1);

        Imgproc.resize(mask, mask, image.size());

        Scalar meanColor = Core.mean(original, mask);

        //            Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\the_convexHull.png", image);
        ImageUtils.saveImage(image, imageForTextRecognition + "_the_convexHull.png", request);

        newVal = new Scalar(255, 255, 0);

        floodFillFacade.setMasked(false);
        System.out.println("Last one:");
        floodFillFacade.fill(image, mask, 211, 194, newVal);

        Core.circle(image, new Point(211, 194), 5, new Scalar(0, 0, 0), -1);
        ImageUtils.saveImage(image, imageForTextRecognition + "_final.png", request);
        //            Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\final.png", image);

        Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1));
        Imgproc.morphologyEx(mask, mask, Imgproc.MORPH_CLOSE, element, new Point(-1, -1), 3);

        Imgproc.resize(mask, mask, image.size());

        //            ImageUtils.saveImage(mask, "final_mask_dilated.png", request);
        //            Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\final_mask_dilated.png", mask);

        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(mask.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL,
                Imgproc.CHAIN_APPROX_NONE);
        double contourArea = 0;
        String path = "";

        MatOfPoint biggestContour = contours.get(0); // getting the biggest contour
        contourArea = Imgproc.contourArea(biggestContour);

        if (contours.size() > 1) {
            biggestContour = Collections.max(contours, new ContourComparator()); // getting the biggest contour in case there are more than one
        }

        Point[] biggestContourPoints = biggestContour.toArray();
        path = "M " + (int) biggestContourPoints[0].x + " " + (int) biggestContourPoints[0].y + " ";
        for (int i = 1; i < biggestContourPoints.length; ++i) {
            Point v = biggestContourPoints[i];
            path += "L " + (int) v.x + " " + (int) v.y + " ";
        }
        path += "Z";

        System.out.println("path:");
        System.out.println(path);

        Rect computedSearchWindow = Imgproc.boundingRect(biggestContour);
        Point massCenter = computedSearchWindow.tl();

        FindingResponse findingResponse = new FindingResponse(path, meanColor, massCenter, -1, contourArea);
        String jsonResponse = gson.toJson(findingResponse, FindingResponse.class);

        out.println(jsonResponse);

        //            String jsonResponse = gson.toJson(path);
        //            out.println(jsonResponse);
    }
}