V
V
Vladimir Kuts2018-09-07 15:12:12
Python
Vladimir Kuts, 2018-09-07 15:12:12

Select center character in image?

I cut the image into symbols for recognition like this:

ret, im_th = cv2.threshold(im_gray, th, 255, cv2.THRESH_BINARY_INV)
    im2, ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    rects = [cv2.boundingRect(ctr) for ctr in ctrs]

    i = 0

    for rect in rects:
        
        leng = int(rect[3] * 1.6)
        pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
        pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
        roi = im_th[pt1:pt1+leng, pt2:pt2+leng]

        if (roi.shape[0]>20 and roi.shape[0]<100) and (roi.shape[1]>20 and roi.shape[1]<100):
            roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
            roi = cv2.dilate(roi, (3, 3))
            cv2.rectangle(img, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
            print(roi)
            i += 1
            cv2.imwrite(f'./indata/sample_c{i}.jpg', roi)

It turns out a lot of images of the form:
5b9269b3d2154957949153.jpeg5b9269c57c7a9326814090.jpeg5b9269d19f34b035651696.jpeg5b9269db96ea1089858371.jpeg
What is the easiest way to select and leave only the central character and remove the garbage on the right and left (pieces of other characters)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AAEgorovich, 2018-10-01
@AAEgorovich

You can remember the region of the symbol and fill with zeros along its boundaries:
x1 = rect[0] - pt2
x2 = rect[0] + rect[2] - pt2
....
if (roi.shape[0]>20 and roi.shape [0]<100) and (roi.shape[1]>20 and roi.shape[1]<100):
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
x1=int (x1*28/leng)
x2=int(x2*28/leng)
roi[:,:x1]=0
roi[:,x2:]=0
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question