package processing;

import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.List;

import static utils.ImageShow.imshow;

public class Mask {

    public static int x, y, h, w = 0;

    public static Mat getMask(Mat image, boolean isItBoots) {
        Imgproc.cvtColor(image, image, Imgproc.COLOR_BGR2GRAY);
        List<MatOfPoint> countours = new ArrayList<MatOfPoint>();

        Imgproc.threshold(image, image, 250, 255, Imgproc.THRESH_BINARY_INV);

        Mat kernel = Mat.ones(new Size(3, 3), Imgproc.MORPH_OPEN);

        Imgproc.morphologyEx(image, image, Imgproc.MORPH_OPEN, kernel);

        Mat kernelClose = Mat.ones(new Size(5, 5), Imgproc.MORPH_CLOSE);
        Imgproc.morphologyEx(image, image, Imgproc.MORPH_CLOSE, kernelClose);
        Mat morph = image.clone();
        Imgproc.findContours(morph, countours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        Mat mask = Mat.zeros(image.rows(), image.cols(), CvType.CV_8U);
        int max = 0;

        if (!countours.isEmpty()) {
            max = 0;
            for (int i = 1; i < countours.size(); i++) {
                if (countours.get(max).height() < countours.get(i).height() ||
                        countours.get(max).width() < countours.get(i).width()) {
                    max = i;
                }
            }
            Rect boundingRect = Imgproc.boundingRect(countours.get(max));
            x = boundingRect.x;
            y = boundingRect.y;
            h = boundingRect.height;
            w = boundingRect.width;
        } else {
            throw new IllegalStateException();
        }

        Imgproc.drawContours(mask, countours, max, new Scalar(255), -1);

        Core.bitwise_and(image, mask, mask);

//        if (isItBoots) {
//            Mat rect = Mat.zeros(image.size(), image.type());
//            Imgproc.rectangle(rect, new Point(x, y), new Point(x + w, y + (int) (h * 0.8)), new Scalar(255, 255, 255), -1);
//            Core.bitwise_and(mask, rect, mask);
//        }

//        image.convertTo(image, CvType.CV_8UC3);
        Imgproc.cvtColor(image, image, Imgproc.COLOR_GRAY2BGR);
        Imgproc.cvtColor(mask, mask, Imgproc.COLOR_GRAY2BGR);

        return mask;
    }
}

@@ -223,24 +223,61 @@
  h, w = 0;%0A%0A
+    public static int percents = 1;%0A%0A
     public s
@@ -417,17 +417,16 @@
 oint%3E co
-u
 ntours =
@@ -896,25 +896,24 @@
 rs(morph, co
-u
 ntours, new 
@@ -936,16 +936,13 @@
 ETR_
-EXTERNAL
+CCOMP
 , Im
@@ -1059,24 +1059,115 @@
 t max = 0;%0A%0A
+        ArrayList%3CInteger%3E indexesForDraw = new ArrayList%3C%3E();%0A        double maxArea = 0;%0A
         if (
@@ -1169,17 +1169,16 @@
  if (!co
-u
 ntours.i
@@ -1231,44 +1231,37 @@
 or (
-int i = 1; i %3C
+MatOfPoint contour :
  co
-u
 ntours
-.size(); i++
 ) %7B%0A
@@ -1280,70 +1280,196 @@
     
-if (
+double area = Imgproc.
 co
-u
 ntour
-s.get(max).height() %3C
+Area(contour);%0A                if (area %3E maxArea) %7B%0A                    maxArea = area;%0A                    max =
  co
-u
 ntours.
-get(i).height() %7C%7C
+indexOf(contour);%0A                %7D
 %0A   
@@ -1469,32 +1469,35 @@
   %7D%0A            
+%7D%0A%0A
             coun
@@ -1496,92 +1496,229 @@
     
-countours.get(max).width() %3C 
+for (MatOfPoint contour : contours) %7B%0A                double area = Imgproc.
 co
-u
 ntour
-s.get(i).width()) %7B%0A                    max = i
+Area(contour);%0A%0A                if ((area / maxArea * 100) %3E percents) %7B%0A                    indexesForDraw.add(contours.indexOf(contour))
 ;%0A  
@@ -1739,32 +1739,33 @@
 %7D%0A            %7D%0A
+%0A
             Rect
@@ -1803,17 +1803,16 @@
 gRect(co
-u
 ntours.g
@@ -2025,32 +2025,158 @@
 n();%0A        %7D%0A%0A
+        for (Integer index : indexesForDraw) %7B%0A            if (Imgproc.contourArea(contours.get(index)) == maxArea) %7B%0A        
         Imgproc.
@@ -2200,17 +2200,16 @@
 , co
-u
 ntours, 
 max,
@@ -2204,18 +2204,20 @@
 ntours, 
-ma
+inde
 x, new S
@@ -2233,17 +2233,144 @@
 ), -1);%0A
-%0A
+            %7D else %7B%0A                Imgproc.drawContours(mask, contours, index, new Scalar(0), -1);%0A            %7D%0A        %7D%0A%0A//
         

Mask was changed