Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV - Extract letters from string using python

I have an image

enter image description here

from where I want to extract each and every character individually.

As i want something like THIS OUTPUT and so on.

What would be the appropriate approach to do this using OpenCV and python?

like image 746
Bits Avatar asked Nov 05 '25 23:11

Bits


1 Answers

A short addition to Amitay's awesome answer. You should negate the image using

cv2.THRESH_BINARY_INV

to capture black letters on white paper.

Another idea could be the MSER blob detector like that:

img = cv2.imread('path to image')
(h, w) = img.shape[:2]
image_size = h*w
mser = cv2.MSER_create()
mser.setMaxArea(image_size/2)
mser.setMinArea(10)

gray = cv2.cvtColor(filtered, cv2.COLOR_BGR2GRAY) #Converting to GrayScale
_, bw = cv2.threshold(gray, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

regions, rects = mser.detectRegions(bw)

# With the rects you can e.g. crop the letters
for (x, y, w, h) in rects:
    cv2.rectangle(img, (x, y), (x+w, y+h), color=(255, 0, 255), thickness=1)

This also leads to a full letter recognition.

enter image description here

like image 101
crazzle Avatar answered Nov 07 '25 13:11

crazzle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!