Handwritten Tigrigna Character Recognition

13 Views Asked by At

the problem that i have got while doing Tigrigna handwritten character recogniton is the order of the characters. my objective was to order the cropped characters from left-top to right-bottom but the output is not as expected. so i need some help how do i arrange them according the order that i expected.

Here is the code that i wrote.

import cv2
import numpy as np
import os

#import image
image = cv2.imread('tigrigna_alphabet.jpg')

#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

#binary
# ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

#dilation
kernel = np.ones((1,1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)

#find contours
ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[1])

# Create a directory to save cropped characters
os.makedirs('cropped_characters', exist_ok=True)

# Initialize a counter for the filenames
i = 0

for i, ctr in enumerate(sorted_ctrs):

    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y+h, x:x+w]

    # Resize the character to 28x28
    resized_character = cv2.resize(roi, (28, 28))

    # show ROI
    cv2.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2)
    #cv2.waitKey(0)

    # Save the resized character as an image file
    if w > 15 and h > 15:
        filename = os.path.join('cropped_characters', f'char_{i}.png')
        cv2.imwrite(filename, resized_character)


cv2.imshow('marked areas',image)
cv2.waitKey(0)

the masked image: enter image description here but the output is: enter image description here

0

There are 0 best solutions below