SHARE
TWEET

part1

a guest Aug 25th, 2019 80 in 7 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     public void colorMask(View view)
  3.     {
  4.         Log.v("message","Start of function call");
  5.         ImageView imageViewMy = findViewById(R.id.imageViewMatches);
  6.         TextView textViewMy = findViewById(R.id.textViewDist);
  7.         //textViewMy.setMovementMethod(new ScrollingMovementMethod());
  8.  
  9.         int BLUR_MAX_KERNEL_LENGTH = 25;
  10.  
  11.         //Image Input :
  12.         Bitmap one =
  13.                 drawableToBitmap(getResources().getDrawable(R.drawable.dsc_1304_cutted_bigger, this.getTheme()));
  14.         Mat img1 = new Mat();
  15.         Utils.bitmapToMat(one, img1, true);// moving one to img1 Mat structure.
  16.        
  17.        
  18.         /*Strat of algorithm:*/
  19.  
  20.  
  21.  
  22.         // downsize the image.
  23.         Mat pyrDowntmp = new Mat();
  24.         Imgproc.resize(img1, pyrDowntmp, new Size(img1.cols() / 4, img1.rows() / 4));
  25.         img1.release();
  26.  
  27.         Mat pyrDown = new Mat();
  28.         cvtColor(pyrDowntmp, pyrDown, COLOR_RGBA2RGB);
  29.         pyrDowntmp.release();
  30.  
  31.         //pyrDowntmp.dump();
  32.         //img1.dump();
  33.  
  34.  
  35.         // Blur the image.
  36.         Mat pyrDownBlur = new Mat();
  37.         System.gc();
  38.  
  39.         Imgproc.medianBlur(pyrDown, pyrDownBlur, 11);
  40.  
  41.  
  42.         for(int i = 1 ; i < BLUR_MAX_KERNEL_LENGTH ; i = i + 2 )
  43.         {
  44.             Imgproc.medianBlur(pyrDownBlur, pyrDownBlur, i);
  45.             //Imgproc.blur(pyrDownBlur, pyrDownBlur, new Size(i,i));
  46.             //Imgproc.GaussianBlur(pyrDownBlur, pyrDownBlur, new Size(i,i), 1, 1);
  47.  
  48.             //System.gc();
  49.         }
  50.  
  51.         for(int i = BLUR_MAX_KERNEL_LENGTH ; 0 < i ; i = i - 2 )
  52.         {
  53.             Imgproc.medianBlur(pyrDownBlur, pyrDownBlur, i);
  54.             //Imgproc.blur(pyrDownBlur, pyrDownBlur, new Size(i,i));
  55.             //Imgproc.GaussianBlur(pyrDownBlur, pyrDownBlur, new Size(i,i), 1, 1);
  56.  
  57.             //System.gc();
  58.         }
  59.  
  60.  
  61.  
  62.         Mat blurHsvImg = new Mat();
  63.         cvtColor(pyrDownBlur, blurHsvImg, COLOR_RGB2HSV);
  64.         //pyrDown.release();
  65.  
  66.         Mat yellowMask = new Mat();
  67.         //Mat greenMask = new Mat();
  68.         //Scalar yellowRGB = new Scalar();
  69.         //inRange(img2, new Scalar(23, 41, 133), new Scalar(40, 150, 255), yellow);
  70.         //inRange(hsvImg, new Scalar(50, 200, 40), new Scalar(70, 255, 255), greenMask); // green
  71.         //inRange(hsvImg, new Scalar(20, 100, 100), new Scalar(30, 255, 255), yellowMask); // yellow.
  72.         //hsvImg.release();
  73.  
  74.         //Mat res = new Mat(img2.rows(), img2.cols(), img2.type());
  75.  
  76.         //inRange(hsvImg, new Scalar(100, 150, 0), new Scalar(140, 255, 255), greenMask); // blue - giving black isolation.
  77.         inRange(blurHsvImg, new Scalar(7, 41, 40), new Scalar(18, 255, 255), yellowMask); // try
  78.         blurHsvImg.release();
  79.  
  80.         //TODO : find out what is the true value of the signs !. Answer: 15. I used a wrong color space.
  81.         // TODO : after that isolation, what will be my next step ?.
  82.  
  83.  
  84.         for(int i = BLUR_MAX_KERNEL_LENGTH ; 0 < i ; i = i - 2 )
  85.         {
  86.             Imgproc.medianBlur(yellowMask, yellowMask, i);
  87.             //Imgproc.blur(pyrDownBlur, pyrDownBlur, new Size(i,i));
  88.             //Imgproc.GaussianBlur(yellowMask, yellowMask, new Size(i,i), 1, 1);
  89.  
  90.             //System.gc();
  91.         }
  92.  
  93.  
  94.         Mat pyrDownBlurMasked = new Mat();
  95.         Mat NotYellowMask = new Mat();
  96.         bitwise_not(yellowMask, NotYellowMask);
  97.         pyrDownBlur.copyTo(pyrDownBlurMasked, yellowMask);
  98.  
  99.         yellowMask.release();
  100.         NotYellowMask.release();
  101.  
  102.  
  103.  
  104.  
  105.         //to get black & white:
  106.         ///Mat template = pyrDown.clone();
  107.         ///template.setTo(new Scalar(255, 255, 255));
  108.         ///template.copyTo(res, yellowMask);
  109.  
  110.         //bitwise_and(img2, yellow, res);
  111.         //
  112.  
  113.  
  114.  
  115.  
  116.         // convert the image to gray scale.
  117.         Mat pyrDownBlurMaskedGray = new Mat();
  118.         Imgproc.cvtColor(pyrDownBlurMasked, pyrDownBlurMaskedGray, Imgproc.COLOR_RGB2GRAY);
  119.  
  120.         pyrDownBlurMasked.release();
  121.  
  122.         ///Bitmap imageMatched = Bitmap.createBitmap(pyrDownBlurMaskedGray.cols(), pyrDownBlurMaskedGray.rows(), Bitmap.Config.RGB_565);
  123.         ///Utils.matToBitmap(pyrDownBlurMaskedGray, imageMatched);
  124.         ///imageViewMy.setImageBitmap(imageMatched);
  125.  
  126.  
  127.  
  128.  
  129.         // AFTER
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.         int scale = 1;
  137.         int delta = 0;
  138.         int ddepth = CV_16S;
  139.         Mat grad_x = new Mat(), grad_y = new Mat();
  140.         Mat abs_grad_x = new Mat(), abs_grad_y = new Mat();
  141.  
  142.         // Sobel Edge Detection Algo.
  143.         Imgproc.Sobel(pyrDownBlurMaskedGray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
  144.         Imgproc.Sobel(pyrDownBlurMaskedGray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
  145.  
  146.         pyrDownBlurMasked.release();
  147.         pyrDownBlurMaskedGray.release();
  148.  
  149.  
  150.         Core.convertScaleAbs(grad_x, abs_grad_x);
  151.         grad_x.release();
  152.         Core.convertScaleAbs(grad_y, abs_grad_y);
  153.         grad_y.release();
  154.         // add the two grads
  155.         Mat grad = new Mat();
  156.         Core.addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
  157.         abs_grad_x.release();
  158.         abs_grad_y.release();
  159.  
  160.  
  161.         //Mat e = new Mat();
  162.         //Canny(pyrDownBlurMaskedGray, e, 10, 120);
  163.  
  164.  
  165.  
  166.  
  167.  
  168.         // Probabilistic Line Transform
  169.         Mat linesPMat = new Mat(); // will hold the results of the detection
  170.         Imgproc.HoughLinesP(grad, linesPMat, 1, (1 * Math.PI)/180,
  171.                 10, 60, 0); // runs the actual detection
  172.  
  173.         grad.release();
  174.  
  175.  
  176.  
  177.         // Divide to Ver & Hor -
  178.         // TODO: move this code to another function named 'divideOrie' or something..
  179.         List<Line> horizontals = new ArrayList<>();
  180.         List<Line> verticals = new ArrayList<>();
  181.         for (int x = 0; x < linesPMat.rows(); x++)
  182.         {
  183.             double[] vec = linesPMat.get(x, 0);
  184.             double x1 = vec[0], y1 = vec[1],
  185.                     x2 = vec[2], y2 = vec[3];
  186.  
  187.             Point start = new Point(x1, y1);
  188.             Point end = new Point(x2, y2);
  189.             Line line = new Line(start, end);
  190.  
  191.             if (Math.abs(x1 - x2) > Math.abs(y1 - y2))
  192.             {
  193.                 horizontals.add(line); // Add to the horizontals lines list.
  194.             }
  195.             else if (Math.abs(x2 - x1) < Math.abs(y2 - y1))
  196.             {
  197.                 verticals.add(line); // Add to the verticals lines list.
  198.             }
  199.         }
  200.  
  201.         linesPMat.release();
  202.  
  203.  
  204.         // Lior : Now Find Intersection
  205.         // computeIntersection - calculate the intersection of two lines.
  206.         // for each two lines - find intersection.
  207.  
  208.         List<Point> intersections1 = new ArrayList<>();
  209.         for (Line horLine : horizontals)
  210.         {
  211.             for (Line verLine: verticals)
  212.             {
  213.                 // calculate the intersection.
  214.                 // Store it in an array of points.
  215.                 intersections1.add(computeIntersection(horLine, verLine));
  216.  
  217.             }
  218.  
  219.             System.gc();
  220.  
  221.         }
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.         List<Point> intersectionsToBeDelete = new ArrayList<>();
  229.         for(int index = 0; index < intersections1.size(); index++)
  230.         {
  231.             for(int innerIndex = index + 1; innerIndex < intersections1.size(); innerIndex++)
  232.             {
  233.                 double horDist = Math.pow(intersections1.get(index).x - intersections1.get(innerIndex).x, 2);
  234.                 double verDist = Math.pow(intersections1.get(index).y - intersections1.get(innerIndex).y, 2);
  235.                 double distance = Math.sqrt(horDist + verDist);
  236.                 if(distance < 6)
  237.                 {
  238.                     intersectionsToBeDelete.add(intersections1.get(index));
  239.                     break;
  240.                 }
  241.             }
  242.         }
  243.  
  244.         intersections1.removeAll(intersectionsToBeDelete);
  245.  
  246.  
  247.  
  248.         org.opencv.core.Point pointsArray[] = new org.opencv.core.Point[ intersections1.size() ];
  249.         intersections1.toArray(pointsArray);
  250.         //MatOfPoint2f intersectionMat = new MatOfPoint2f(pointsArray);
  251.  
  252.         //Processing on mMOP2f1 which is in type MatOfPoint2f
  253.         //MatOfPoint2f approxCurve = new MatOfPoint2f();
  254.         //double approxDistance = Imgproc.arcLength(intersectionMat, true) * 0.02;
  255.         //Imgproc.approxPolyDP(intersectionMat, approxCurve, approxDistance, true);
  256.  
  257.  
  258.  
  259.  
  260.         Mat intersections = new Mat(pyrDown.size(), CvType.CV_32F);
  261.  
  262.         Mat intersections32f = new Mat();
  263.  
  264.  
  265.         // draw all the intersections. (see it ok)
  266.         for (Point point: pointsArray)
  267.         {
  268.             // pyrDownGray - the blured image
  269.             // pyrDown - the unblured image.
  270.             circle(pyrDown, point, 5, new Scalar(255, 0, 0), 2);//Red
  271.  
  272.             circle(intersections, point, 0, new Scalar(255, 0, 0), 2);//Red
  273.  
  274.             //intersections32f.put((int)point.x, (int)point.y, 1.0);
  275.  
  276.         }
  277.        
  278.        
  279.         /*
  280.         intersections.convertTo(intersections32f, CvType.CV_8UC1);
  281.  
  282.  
  283.  
  284.         Mat labels = Mat.zeros(intersections.rows(), intersections.cols(), CvType.CV_32F);
  285.         labels.put(0, 0, 1.0); // topLeft
  286.         labels.put(0, labels.rows() - 1, 1); // bottomLeft
  287.         labels.put(labels.cols() - 1, 0, 1); // topRight
  288.         labels.put(labels.rows() - 1, labels.cols() - 1, 1); // bottomRight
  289.  
  290.  
  291.         TermCriteria criteria = new TermCriteria(TermCriteria.COUNT, 100, 1);
  292.         int attempts = 50;
  293.         int flags = Core.KMEANS_USE_INITIAL_LABELS;
  294.         Mat centers = new Mat();
  295.  
  296.         double res = kmeans(intersections, 4, labels, criteria, attempts, flags, centers);
  297.  
  298.  
  299.         centers.convertTo(intersections32f, CvType.CV_8UC1);
  300.  
  301.  
  302.         Bitmap imageMatched = Bitmap.createBitmap(intersections32f.cols(), intersections32f.rows(), Bitmap.Config.RGB_565);
  303.         Utils.matToBitmap(intersections32f, imageMatched);
  304.         imageViewMy.setImageBitmap(imageMatched);
  305.  
  306.         */
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.         Point bottomRight = new Point(0,0);
  314.         Point topLeft = new Point(pointsArray[0].x, pointsArray[0].y);
  315.  
  316.  
  317.  
  318.         for(Point point: pointsArray)
  319.         {
  320.             if(point.x + point.y > bottomRight.x + bottomRight.y)
  321.             {
  322.                 bottomRight.x = point.x;
  323.                 bottomRight.y = point.y;
  324.             }
  325.             else if(point.x + point.y < topLeft.x + topLeft.y)
  326.             {
  327.                 topLeft.x = point.x;
  328.                 topLeft.y = point.y;
  329.             }
  330.  
  331.         }
  332.  
  333.         circle(pyrDown, bottomRight, 5, new Scalar(255, 255, 0), 4);//YELLOW
  334.         circle(pyrDown, topLeft, 5, new Scalar(255, 255, 0), 4);//YELLOW
  335.  
  336.  
  337.  
  338.         Point topRight = new Point(bottomRight.x, topLeft.y);
  339.         Point bottomLeft = new Point(topLeft.x, bottomRight.y);
  340.        
  341.        
  342.         // End , now you have the 4 points, by name.
  343.         //*************************************************
  344.         //
  345.         // TODO : At this point you have all 4 points.
  346.         //          You can do what you wishes..
  347.         //
  348.         //*************************************************
  349.        
  350.        
  351.        
  352.         /*
  353.  
  354.  
  355.  
  356.         //pyrDown.copyTo(intersections);
  357.  
  358.  
  359.         //intersections.convertTo(intersections32f, CvType.CV_32F);
  360.         intersections.release();
  361.  
  362.         Mat labels = Mat.zeros(intersections32f.rows(), intersections32f.cols(), CvType.CV_32F);
  363.  
  364.         //double[] o = labels.get(0, 0);
  365.         //double[] o1 = labels.get(labels.rows() - 1, 0);
  366.         //double[] o2 = labels.get(labels.rows() - 1, labels.cols() - 1);
  367.         //double[] o3 =labels.get(0, labels.cols() - 1);
  368.  
  369.         labels.put(0, 0, 1.0); // topLeft
  370.         labels.put(0, labels.rows() - 1, 1); // bottomLeft
  371.         labels.put(labels.cols() - 1, 0, 1); // topRight
  372.         labels.put(labels.rows() - 1, labels.cols() - 1, 1); // bottomRight
  373.  
  374.         //o = labels.get(0, 0);
  375.         //o1 = labels.get(labels.rows() - 1, 0);
  376.         //o2 = labels.get(labels.rows() - 1, labels.cols() - 1);
  377.         //o3 =labels.get(0, labels.cols() - 1);
  378.  
  379.         TermCriteria criteria = new TermCriteria(TermCriteria.COUNT, 100, 1);
  380.         int attempts = 50;
  381.         int flags = Core.KMEANS_PP_CENTERS;
  382.         Mat centers = new Mat();
  383.         double res = kmeans(intersections32f, 4, labels, criteria, attempts, flags, centers);
  384.  
  385.  
  386.         //public static double kmeans(@NotNull Mat data,
  387.         //int K,
  388.         //@NotNull Mat bestLabels,
  389.         //@NotNull TermCriteria criteria,
  390.         //int attempts,
  391.         //int flags)
  392.  
  393.  
  394.  
  395.         String centersString = centers.toString();
  396.  
  397.         Bitmap imageMatched = Bitmap.createBitmap(centers.cols(), centers.rows(), Bitmap.Config.RGB_565);
  398.         Utils.matToBitmap(centers, imageMatched);
  399.         imageViewMy.setImageBitmap(imageMatched);
  400.  
  401.  
  402.         /*
  403.  
  404.         // Draw the horizontals lines
  405.         for (int i = 0; i < horizontals.size(); i++)
  406.         {
  407.             // pyrDownGray - the blured image
  408.             // pyrDown - the unblured image.
  409.             Point start = horizontals.get(i)._start;
  410.             Point end = horizontals.get(i)._end;
  411.  
  412.             Imgproc.line(pyrDown, start, end, new Scalar(0, 0, 255), 2, Imgproc.LINE_AA, 0);//Blue
  413.             //Imgproc.line(grad, start, end, new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
  414.         }
  415.         //
  416.  
  417.  
  418.         // Draw the verticals lines
  419.         for (int j = 0; j < verticals.size(); j++)
  420.         {
  421.             // pyrDownGray - the blured image
  422.             // pyrDown - the unblured image.
  423.             Point start = verticals.get(j)._start;
  424.             Point end = verticals.get(j)._end;
  425.  
  426.             Imgproc.line(pyrDown, start, end, new Scalar(0, 255, 0), 2, Imgproc.LINE_AA, 0);//Green
  427.             //Imgproc.line(pyrDownGrayCanny, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
  428.         }
  429.         //
  430.  
  431.  
  432.  
  433.         Bitmap imageMatched = Bitmap.createBitmap(pyrDown.cols(), pyrDown.rows(), Bitmap.Config.RGB_565);
  434.         Utils.matToBitmap(pyrDown, imageMatched);
  435.         imageViewMy.setImageBitmap(imageMatched);
  436.  
  437.  
  438.         //*/
  439.  
  440.         Log.v("message","End of function call");
  441.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top