Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imgproc.findContours(cont, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
- //cleanedMatList = new ArrayList<Mat>();
- //int c = 0;
- // points of the contours whose heights are bigger than the threshold
- List<Point> digits = new ArrayList<Point>();
- // don't iterate the hierarchy here. iterate the contours instead
- //for (int i = 0; i >= hierarchy.cols(); i++) {
- for (int i = 0; i < contours.size(); i++)
- Rect rect = Imgproc.boundingRect(contours.get(i));
- if (rect.height > HTRESH) {
- // this rectangle drawing in the binary image is only for visualization. it has nothing to do with final result
- 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);
- //cleanedMatList.add(c, binary);
- //c++;
- // append this contour points to digits list
- digits.addAll(contours.get(i).toList());
- }
- }
- // now we have all the digit contour points in digits list. now we can find the convexhull of these points
- // we dont need a convexhull for each contour. we need only one convexhull for the points in the digits list
- /*List<MatOfInt> digitsHull = new ArrayList<MatOfInt>();
- for(int i=0; i < contours.size(); i++){
- digitsHull.add(new MatOfInt());
- }
- for(int i=0; i < contours.size(); i++){
- Imgproc.convexHull(contours.get(i), digitsHull.get(i));
- }*/
- // get the convex hull
- MatOfInt digitsHullIdx = new MatOfInt(); // this contains the indices
- MatOfPoint hullPoints = new MatOfPoint();
- hullPoints.fromList(digits);
- Imgproc.convexHull(hullPoints, digitsHullIdx);
- // now you have to convert the convexhull digitsHullIdx into points to use it in a Imgproc.drawContours
- MatOfPoint digitsHullPoints = new MatOfPoint();
- // TODO: please find out how you can extract the points indexed by digitsHullIdx and add them to digitsHullPoints and do it here
- // now we have filled digitsHullPoints with points in the convexhull
- List<MatOfPoint> digitRegions = new ArrayList<MatOfPoint>();
- // digitsHullPoints to regions so we can use Imgproc.drawContours to create a mask
- digitRegions.add(digitsHullPoints);
- // digitRegions was just created and is empty, but you are iterating it. this has no effect
- /*for (int i = 0; i< digitRegions.size(); i++) {
- MatOfPoint dr = digitRegions.get(i);
- dr.push_back(digitsHull.get(i));
- }*/
- Mat digitsMask = new Mat(srcImage.rows(), srcImage.cols(), CvType.CV_8U);
- Imgproc.drawContours(digitsMask, digitRegions, 0, new Scalar(255, 255, 255), -1);
- Imgproc.morphologyEx(digitsMask, digitsMask, Imgproc.MORPH_DILATE, kernal);
- Mat cleaned = new Mat(srcImage.rows(), srcImage.cols(), CvType.CV_8U);
- diwb.copyTo(cleaned, digitsMask);
- return diwb;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement