0

I'm trying to run an OCR on a speed sign. I'm getting the contours like below :

    static ArrayList<MatOfPoint> getContours(Mat fgMask) {
        ArrayList<MatOfPoint> contours = new ArrayList<>();
        float threshold = 100.0f;
        Mat cannyOutput = new Mat();
        Imgproc.Canny(fgMask, cannyOutput, threshold, threshold * 3);
        Mat hierarchy = new Mat();
        Imgproc.findContours(cannyOutput, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
        hierarchy.release();
        return contours;
    }

But sometimes, I detect a 0 inside a 0. As you can see here :

enter image description here

I know that we can use the hierachy mat to do what I want. I just don't understand how to do it in Java.

Here is a solution in python

T.K
  • 434
  • 7
  • 14
  • Did you try using `RETR_EXTERNAL`? – beaker Sep 16 '20 at 15:16
  • Yes I tried but if I use `RETR_EXTERNAL` it will stop at the circle and I won't be able to detect the digits. – T.K Sep 16 '20 at 15:21
  • Okay then, if you're looking at only the 3 contours you have drawn on your image, you can throw away those for which the parent (the last column of `hierarchy`) is one of the other contours. So if the outer contour of the `0` is `7`, for example, the **parent** of the inner contour of the `0` will be `7`. – beaker Sep 16 '20 at 15:31
  • Can you show us the contents of `hierarchy` for your example image? – beaker Sep 16 '20 at 15:33
  • I'm not sure I understood what you mean. Does `0` represents the first contour ? – T.K Sep 16 '20 at 15:37
  • I meant the `0` in your image... the second digit of the `50`, whichever contour index that was assigned. – beaker Sep 16 '20 at 15:40
  • I'm not sure I've got a good result is this the way to get it correctly ? ```for (int i = 0; i < contours.size(); i ++){ Log.i("Hierarchy", Arrays.toString(hierarchy.get(0, i))); } ``` – T.K Sep 16 '20 at 15:44
  • `hierarchy` is simply a 2-dimensional array. See the last example at the bottom of the page [here](https://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html) – beaker Sep 16 '20 at 15:52
  • My `hierarchy` looks like this ```I/Hierarchy: [1.0, -1.0, -1.0, -1.0] [2.0, 0.0, -1.0, -1.0] [3.0, 1.0, -1.0, -1.0] [4.0, 2.0, -1.0, -1.0] [5.0, 3.0, -1.0, -1.0] [13.0, 4.0, 6.0, -1.0] [-1.0, -1.0, 7.0, 5.0] [11.0, -1.0, 8.0, 6.0] [-1.0, -1.0, 9.0, 7.0] [-1.0, -1.0, 10.0, 8.0] [-1.0, -1.0, -1.0, 9.0] [-1.0, 7.0, 12.0, 6.0] [-1.0, -1.0, -1.0, 11.0] [15.0, 5.0, 14.0, -1.0] [-1.0, -1.0, -1.0, 13.0] [16.0, 13.0, -1.0, -1.0] I/Hierarchy: [-1.0, 15.0, -1.0, -1.0] ``` I have less on the image because I sort by size – T.K Sep 17 '20 at 08:04
  • If you edited that into your question, it would be a lot more readable. Could you also indicate which three contours you've marked in your sample image? – beaker Sep 17 '20 at 14:08
  • Well I think I'm going to close this question I manage to delete the inner contour without `hierarchy` because it seems that the inner contour isn't the son of the other contour. I realized that when I looked at your link. Thanks for your time ! – T.K Sep 17 '20 at 14:11
  • It looks from the hierarchy that the inner contour is either contour 10, 12 or 14. (At a guess, I'd say 10, but I can't be sure without more information.) Are you saying that it's none of those? – beaker Sep 18 '20 at 16:30

0 Answers0