简体   繁体   English

使用opencv将图像的一部分列入黑名单

[英]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 :所以我的代码使用带有tesseract的opencv从图像中提取文本,我想做的是将图像的某些部分列入黑名单,这样代码就不会检查代码中是否有文本:

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.您的目标是仅从生成的mask图像中提取文本,并且您已经完成了大部分繁重的工作。 I have tried using easyOCR library on mask which gives the result you are looking for.我尝试在mask上使用easyOCR库,它可以提供您正在寻找的结果。

Using mask as input image, here is the remaining code:使用mask作为输入图像,这是剩余的代码:

# 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)] [([[[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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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