Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generalize contouring handwritten characters using OpenCV in Python?

I try yo detect and crop handwritten characters from an image. Some characters can be recognized and enclosed in a rectangle, but for others the same parameters do not work. How can I generlize it?

Raw Image

import cv2
import numpy as np
import matplotlib.pyplot as plt

im = cv2.imread('mission.png',0)
img_blured = cv2.GaussianBlur(im,(5,5),7)
closing = cv2.morphologyEx(img_blured, cv2.MORPH_CLOSE, (31,31))
thresh = 195
ret, bw_img = cv2.threshold(closing, thresh, 255, cv2.THRESH_BINARY)


_,contours, hierarchy = cv2.findContours(bw_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
   x,y,w,h = cv2.boundingRect(cnt)
   cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),3)
i=0
for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)

    if w>150 and h>150:
    
        cv2.imwrite(str(i)+".jpg",bw_img[y:y+h,x:x+w])
        i=i+1
plt.imshow(im)
plt.show()
cv2.imwrite("output.png",im)

Processed Image

like image 658
Muhammed Erem Avatar asked Jul 15 '26 20:07

Muhammed Erem


1 Answers

Maybe this can help you:

# Import preprocessors
import os
import cv2
import numpy as np

# Read image
dir = os.path.abspath(os.path.dirname(__file__))
im = cv2.imread(dir+'/nvCXT.png')

# Add padding around the original image
pad = 5
h, w = im.shape[:2]
im2 = ~(np.ones((h+pad*2, w+pad*2, 3), dtype=np.uint8))
im2[pad:pad+h, pad:pad+w] = im[:]
im = im2

# Blur it to remove noise
im = cv2.GaussianBlur(im, (5, 5), 5)

# Gray and B/W version
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
bw = cv2.threshold(im, 200, 255, cv2.THRESH_BINARY)[1]

# Find contours and sort them by position
cnts, _ = cv2.findContours(bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnts.sort(key=lambda x:cv2.boundingRect(x)[0])

# Find and save blocks
s1, s2 = w/2, w/10
i = 0
x2, y2, w2, h2 = 0, 0, 0, 0
for cnt in cnts:
    x, y, w, h = cv2.boundingRect(cnt)
    if (w+h < s1 and w+h > s2) and (i==0 or (x2+w2) < x):
        i += 1
        cv2.imwrite(dir+'/_'+str(i)+".jpg", im[y:y+h, x:x+w])
        cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 2)
    x2, y2, w2, h2 = x, y, w, h

# Save the processed images
cv2.imwrite(dir+'/out.png', im)
cv2.imwrite(dir+'/out_bw.png', bw)

enter image description here

enter image description here

enter image description here

enter image description here

like image 108
Shamshirsaz.Navid Avatar answered Jul 18 '26 08:07

Shamshirsaz.Navid