2

I'm working on a software which should recognise several digits. For cropping the digits of an image I am using openCV. The problem I've got is that the bounding box algorithm is not only detecting the digits. It is detecting the structure in a digit too.

enter image description here

The simplest way to solve this would be to set a minimum size the structure have to have. This doesn't work because I have to detect digits of any size. Has anybody an idea to solve this problem?

This is the code:

im = cv2.imread('img.jpg')
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    contours,hierarchy = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    idx = 0
    for cnt in contours:

            xe,ye,we,he = cv2.boundingRect(cnt)
            roi=im[ye-100:ye+he+100,xe-100:xe+we+100]

            if xe > 30:
                    if ye > 30:
                            if he > 30:
                                    if we > 30:
                                            idx += 1
                                            cv2.imwrite(str(idx) + '.jpg', roi)
                                            cv2.rectangle(im,(xe,ye),(xe+we,ye+he),(200,0,0),2)


                                            cv2.imwrite('dev.jpg', im)
4
  • 1
    try RETR_EXTERNAL instead of RETR_LIST It should extract only the outer contour if i understood the documentation correct. Commented Jan 23, 2016 at 17:10
  • Thank you very much. This works Commented Jan 23, 2016 at 17:18
  • @M4rtini: could you turn that into an answer for future reference? Commented Jan 23, 2016 at 17:26
  • @gariepy Yes, i've now done so. Included links to the docs and a list of the available modes of the function. Commented Jan 23, 2016 at 17:48

1 Answer 1

3

According to the documentation.

You can change the mode of the findContours method to only return the outermost contours by changing the paramter cv2.RETR_LIST to cv2.RETR_EXTERNAL

Contour retrieval mode (if you use Python see also a note below).

  • CV_RETR_EXTERNAL retrieves only the extreme outer contours. It sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours.

  • CV_RETR_LIST retrieves all of the contours without establishing any hierarchical relationships.

  • CV_RETR_CCOMP retrieves all of the contours and organizes them into a two-level hierarchy. At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.

  • CV_RETR_TREE retrieves all of the contours and reconstructs a full hierarchy of nested contours. This full hierarchy is built and shown
    in the OpenCV contours.c demo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.