1

Input Image from which I tried extract character

output image

I want to crop all character from image to folder and give them name line by line but character not cropped well and also I can't name them correctly. In output image my code recognize character but also some extra lines.

I tried this code but it not working for all size image.

import cv2
import numpy as np
cropped_Image_Location = "./"


image = cv2.imread(cropped_Image_Location+"Sample/17.jpg")
gray= cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
(thresh, im_bw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)
edges = cv2.Canny(im_bw, 100, 150, apertureSize=5)
cv2.imwrite(cropped_Image_Location + 'CroppedImages/edges-50-150.jpg', edges)
minLineLength = 100
lines = cv2.HoughLinesP(image=edges, rho=1, theta=np.pi / 180, 
threshold=200, lines=np.array([]),minLineLength=minLineLength, maxLineGap=20000)

try:
    a, b, c = lines.shape
    print(a,b,c)

    for i in range(a):
        cv2.line(im_bw, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 3, cv2.LINE_AA)
except:
    pass




_,thresh = cv2.threshold(im_bw,70,255,cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 0)
contours, hierarchy = 

cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

i=1
for contour in contours:

    [x,y,w,h] = cv2.boundingRect(contour)
    print(x,y,w,h)
    if w>1 and h>1:
        print(x, y, w, h,"........")
        cv2.rectangle(im_bw, (x-3, y-3), (x + w+3, y + h+3), (0, 0, 0), 1)

        cv2.imwrite(cropped_Image_Location+"CroppedImages/"+str(i)+".jpg",image[y-2:y+h+2,x-2:x+w+2])

        i=i+1
cv2.imwrite(cropped_Image_Location + 'CroppedImages/lectureAll.jpg', im_bw)
# cv2.imshow("gray",im_bw)
cv2.waitKey(0)
cv2.destroyAllWindows()
stock_code
  • 23
  • 1
  • 6
  • hi i would try to approach this problem by first identifying all the grid boxes containing characters and then using those grid boxes to name the character, as it would be easy to recognize those boxes from the given input image without having much trouble. – Vaibhav gusain Mar 17 '19 at 07:20

0 Answers0