Advertisement
Guest User

extractdigits

a guest
Nov 28th, 2015
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     Imgproc.findContours(cont, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
  2.         //cleanedMatList = new ArrayList<Mat>();
  3.         //int c = 0;
  4.  
  5.     // points of the contours whose heights are bigger than the threshold
  6.     List<Point> digits = new ArrayList<Point>();
  7.  
  8.     // don't iterate the hierarchy here. iterate the contours instead
  9.         //for (int i = 0; i >= hierarchy.cols(); i++) {
  10.     for (int i = 0; i < contours.size(); i++)
  11.             Rect rect = Imgproc.boundingRect(contours.get(i));
  12.             if (rect.height > HTRESH) {
  13.         // this rectangle drawing in the binary image is only for visualization. it has nothing to do with final result
  14.                 Imgproc.rectangle(binary, new Point(rect.x, rect.y), new Point(rect.x + rect.width - 1, rect.y + rect.height - 1), new Scalar(0, 0, 255), 3);
  15.                 //cleanedMatList.add(c, binary);
  16.                 //c++;
  17.  
  18.         // append this contour points to digits list
  19.         digits.addAll(contours.get(i).toList());
  20.             }
  21.         }
  22.     // now we have all the digit contour points in digits list. now we can find the convexhull of these points
  23.  
  24.     // we dont need a convexhull for each contour. we need only one convexhull for the points in the digits list
  25.         /*List<MatOfInt> digitsHull = new ArrayList<MatOfInt>();
  26.         for(int i=0; i < contours.size(); i++){
  27.             digitsHull.add(new MatOfInt());
  28.         }
  29.         for(int i=0; i < contours.size(); i++){
  30.             Imgproc.convexHull(contours.get(i), digitsHull.get(i));
  31.         }*/
  32.  
  33.     // get the convex hull
  34.     MatOfInt digitsHullIdx = new MatOfInt();        // this contains the indices
  35.     MatOfPoint hullPoints = new MatOfPoint();
  36.     hullPoints.fromList(digits);
  37.     Imgproc.convexHull(hullPoints, digitsHullIdx);
  38.  
  39.     // now you have to convert the convexhull digitsHullIdx into points to use it in a Imgproc.drawContours
  40.     MatOfPoint digitsHullPoints = new MatOfPoint();
  41.     // TODO: please find out how you can extract the points indexed by digitsHullIdx and add them to digitsHullPoints and do it here
  42.  
  43.     // now we have filled digitsHullPoints with points in the convexhull
  44.         List<MatOfPoint> digitRegions = new ArrayList<MatOfPoint>();
  45.     // digitsHullPoints to regions so we can use Imgproc.drawContours to create a mask
  46.     digitRegions.add(digitsHullPoints);
  47.  
  48.     // digitRegions was just created and is empty, but you are iterating it. this has no effect
  49.         /*for (int i = 0; i< digitRegions.size(); i++) {
  50.             MatOfPoint dr = digitRegions.get(i);
  51.             dr.push_back(digitsHull.get(i));
  52.         }*/
  53.  
  54.         Mat digitsMask = new Mat(srcImage.rows(), srcImage.cols(), CvType.CV_8U);
  55.         Imgproc.drawContours(digitsMask, digitRegions, 0, new Scalar(255, 255, 255), -1);
  56.         Imgproc.morphologyEx(digitsMask, digitsMask, Imgproc.MORPH_DILATE, kernal);
  57.  
  58.         Mat cleaned = new Mat(srcImage.rows(), srcImage.cols(), CvType.CV_8U);
  59.         diwb.copyTo(cleaned, digitsMask);
  60.  
  61.         return diwb;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement