M
M
maryaturova2021-06-09 09:51:46
Python
maryaturova, 2021-06-09 09:51:46

How to cut out a light area?

Dear! There was a question about the decision of recognition of the selected text from the picture.
60c025f2a1c04670334736.png
60c02605efdf8293056453.png
60c0261ad6485397325112.png
To select an object I tried this:

from PIL import Image
image = Image.open('1.png')
mask=image.convert("L")
th=100 # the value has to be adjusted for an image of interest 
mask = mask.point(lambda i: i < th and 250)
mask.save('test.png')

But this did not give a result, because. contrast is different.
Tell me how you can cut out a light rectangular area? And is there an easy way to further recognize text on it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ramis, 2021-06-09
@maryaturova

Tell me how you can cut a light rectangular area?

Googled it and found it right away
import cv2
import numpy as np

img = cv2.imread("C:\\Users\\ramas\\Documents\\321.png")

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY)
    img_blur = cv2.GaussianBlur(thresh, (5, 5), 2)
    img_canny = cv2.Canny(img_blur, 0, 0)
    return img_canny

def get_contours(img):
    contours, _ = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    r1, r2 = sorted(contours, key=cv2.contourArea)[-3:-1]
    x, y, w, h = cv2.boundingRect(np.r_[r1, r2])
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
    crop = img[y:y+h, x:x+w]
    cv2.imwrite('result.png', crop)

get_contours(img)
cv2.imshow("img_processed", img)
cv2.waitKey(0)

60c0a9620aae9598186695.png
60c0a97b66628590471684.png
Is there an easy way to further OCR text on it?

Read this simple guide
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
print(pytesseract.image_to_string('result.png'))

and rejoice)
60c0adb9e1be6188985022.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question