I
I
i_vovani2020-08-29 22:26:14
Python
i_vovani, 2020-08-29 22:26:14

High-quality text detection if it is the same color as the background?

Comrades, I was faced with the task of detecting letters in a picture with a tire, here is an example:
5f4aaa21823f1138689313.jpeg
To solve this issue, I decided to use OpenCV, but when I want to make a threshold, there are a lot of extra unnecessary pixels, like this:
5f4aaaf023470187902377.png
How can I fix this, so that the contours of the letters in the image are correctly located?
Here is the function that is responsible for the detection:

def detecting_and_extracting_letters(path,out_size=28):
    img = cv2.imread(path,0)
    thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
    img_erode = cv2.erode(thresh, np.ones((3, 3), np.uint8), iterations=1)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    
    output = img.copy()
    letters = []

    for idx, contour in enumerate(contours):
        (x, y, w, h) = cv2.boundingRect(contour)
        if hierarchy[0][idx][3] == 0:
            cv2.rectangle(output, (x, y), (x + w, y + h), (70, 0, 0), 1)



            letter_crop = gray[y:y + h, x:x + w]
            # print(letter_crop.shape)

            size_max = max(w, h)
            letter_square = 255 * np.ones(shape=[size_max, size_max], dtype=np.uint8)
            if w > h:
                y_pos = size_max//2 - h//2
                letter_square[y_pos:y_pos + h, 0:w] = letter_crop
            elif w < h:
                x_pos = size_max//2 - w//2
                letter_square[0:h, x_pos:x_pos + w] = letter_crop
            else:
                letter_square = letter_crop

            letters.append((x, w, cv2.resize(letter_square, (out_size, out_size), interpolation=cv2.INTER_AREA)))

    letters.sort(key=lambda x: x[0], reverse=False)
    return letters

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2020-08-29
@i_vovani

1. Get rid of the background by flattening: bringing the gradient to a single color.
2. Clear open areas.
3. Segment enclosed areas.
4. Repeat until the result stops changing.

D
Dimonchik, 2020-08-29
@dimonchik2013

https://www.pyimagesearch.com/2017/07/17/credit-ca...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question