Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Findcontours retrieve contours unsorted

Tags:

opencv

ocr

WE are implementing OCR using opencv for android, everything went fine till the part of findning contours using Imgproc.findcontours() it gives back the contours not in the same order as they were in the input image i.e : input image M N O P the first contour gets from findcontours() is P input image E F G H the first contour gets from findcontours() is E (here is right) input image I J K L the first contour gets from findcontours() is J so it seems that it extracts the contours randomly how can we fix this? because we want to give back the word as it was written exactly in the image

like image 875
Hend Mohamad Avatar asked Dec 27 '22 22:12

Hend Mohamad


1 Answers

OpenCV doesn't seems to have any order while finding contours. If we need it sorted we should do it manually, which is an extra task.

But for OCR purposes, I too had this problem. So what i did was to find the centroids of the detected contours. It can be found out using moments.

Finding centroid using Moments

Or you can find the bounding box of the contour, then find the center of that bounding box. Later after ocr put the data on the same centroid.

This was the method I used when I prepared OCR myself. You can find the complete details in this SOF question : Simple Digit Recognition OCR in OpenCV-Python

EDIT : Order of FindContours, (after comment from vasile)

Original image :

enter image description here

Order in which contours where found :

enter image description here

like image 76
Abid Rahman K Avatar answered Jan 07 '23 08:01

Abid Rahman K