Answer the question
In order to leave comments, you need to log in
How to cut out a light area?
Dear! There was a question about the decision of recognition of the selected text from the picture.
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')
Answer the question
In order to leave comments, you need to log in
Tell me how you can cut a light rectangular area?
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)
Is there an easy way to further OCR text on it?
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
print(pytesseract.image_to_string('result.png'))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question