简体   繁体   中英

blacklisting part of an image with opencv

so my code is using opencv with tesseract to extract a text from a image and what i want to do is to blacklist some parts of the image so the code don't check if there is a text here the code :

import numpy as np

img = cv2.imread('test.jpeg')

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)


sensitivity = 70
lower_range = np.array([0,0,255-sensitivity])
upper_range = np.array([255,sensitivity,255])

mask = cv2.inRange(hsv, lower_range, upper_range)


cv2.imshow('image', img)
cv2.imshow('mask', mask)

cv2.waitKey(0)
cv2.destroyAllWindows()

Base image:

基础镜像

Parts of the image i want to blacklist (in red):

我想加入黑名单的图像部分(红色)

can someone help me doing this if this is possible ?

Your objective here is to extract only the text from the resulting mask image and you already did most of the heavy lifting. I have tried using easyOCR library on mask which gives the result you are looking for.

Using mask as input image, here is the remaining code:

# import library and initialize the reader
from easyocr import Reader
reader = Reader(['en'])

# pass input image
results = reader.readtext(mask)

Output :

[([[93, 85], [245, 85], [245, 129], [93, 129]], 'SWHSY', 0.9746386534414473)]

It returns the bounding box position of the text, the text itself along with confidence score.

The following snippet allows you to draw the bounding box around the detected text:

for (bbox, text, prob) in results[:5]:
    (tl, tr, br, bl) = bbox
    top_left = (int(tl[0]), int(tl[1]))
    bottom_right = (int(br[0]), int(br[1]))
    img = cv2.rectangle(img, top_left, bottom_right, (0, 0, 255), 3)
    img = cv2.putText(img, text, (tl[0], tl[1] - 20),   cv2.FONT_HERSHEY_SIMPLEX, 1.1, (255, 255, 0), 5)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM