Extracting MICR in Cheques

Hey guys i need help, i’m new to OpenCV and i’m trying to make these work

GitHub - atul04/Banck-Cheque-OCR-and-MICR-Detection: Bank Cheque OCR using Pytesseract and MICR detection using contour detection and pattern recognition (this is what im trying since its the most recent)
GitHub - omkarudawant/MICR-Retriever: A computer vision project for retrieving information from bank cheques. (tried this too but didn’t work)

What im trying to do is extract the MICR values in a Cheque, and im just trying to get these open source versions to work and check how accurate it is in extracting.

There are a few more open-source versions of these available,
but I will only be referencing two of them here, so my problem is everytime i execute i’m always getting an error in this line:

refCnts = contours.sort_contours(refCnts, method="left-to-right")[0]

Here’s the Error Details:

Traceback (most recent call last):
  File "m_extractor.py", line 93, in <module>
    refCnts = contours.sort_contours(refCnts, method="left-to-right")[1]
  File "/project_path/check_env/lib/python3.8/site-packages/imutils/contours.py", line 23, in sort_contours
    boundingBoxes = [cv2.boundingRect(c) for c in cnts]
  File "/project_path/check_env/lib/python3.8/site-packages/imutils/contours.py", line 23, in <listcomp>
    boundingBoxes = [cv2.boundingRect(c) for c in cnts]
cv2.error: OpenCV(4.10.0) /Users/xperience/GHA-Actions-OpenCV/_work/opencv-python/opencv-python/opencv/modules/imgproc/src/geometry.cpp:692: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'

tried the solution in StackOverflow that maybe the image wasn’t properly loaded, but i’ve tried to check it and it wasn’t the problem.

I’m using OpenCV version 4.10.0

Here’s the Reference Image that is being loaded.

And here’s the Code Snippet where it throws an error:

# load the reference MICR image from disk, convert it to grayscale,
# and threshold it, such that the digits appear as *white* on a
# *black* background
ref = cv2.imread("./micr_e13b_reference.png")
ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY)
ref = imutils.resize(ref, width=400)
ref = cv2.threshold(ref, 0, 255, cv2.THRESH_BINARY_INV |
# find contours in the MICR image (i.e,. the outlines of the
# characters) and sort them from left to right
refCnts = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,
refCnts = refCnts[0] if imutils.is_cv2() else refCnts[1]

refCnts = contours.sort_contours(refCnts, method="left-to-right")[0]

Thanks in advance for any assistance!

you’re using imutils. please contact Adrian Rosebrock for support with any of his functions.

that looks questionable. you aren’t gonna use different major versions of OpenCV, right? you should target 4.x at the moment. ignore 3.x and anything older.

(contours, hierarchy) = cv.findContours(...)
1 Like

Yes, I am only using OpenCV 4.x and not any older major versions like 3.x. I’ll try to reach out to Adrian for any imutils support. Thanks for the recommendation!