【问题标题】:How to draw Bigger bounding box and Crop Only bounding box Text Python Opencv如何绘制更大的边界框和仅裁剪边界框文本 Python Opencv
【发布时间】:2021-11-26 01:04:06
【问题描述】:

我正在使用 easyocr 来检测图像中的文本。该方法给出了输出边界框。输入图像如下所示

图片 1

图 2

使用下面的代码获得输出图像。

但我想绘制一个包含所有文本的单个/更大的边界框,并相对于边界框裁剪图像并删除剩余的不需要的区域或文本。

这是附上的代码 要求

pip 安装 pytesseract

点安装easyocr

使用 python main.py -i image1.jpg 运行代码

# USAGE
# python localize_text_tesseract.py --image apple_support.png
# python localize_text_tesseract.py --image apple_support.png --min-conf 50

# import the necessary packages
from pytesseract import Output
import pytesseract
import argparse
import cv2
from matplotlib import pyplot as plt
import numpy as np
import os
import easyocr
from PIL import ImageDraw, Image



def remove_lines(image):
    result = image.copy()
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Remove horizontal lines
    horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1))
    remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
    cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        cv2.drawContours(result, [c], -1, (255,255,255), 5)


    # Remove vertical lines
    vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,40))
    remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
    cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        cv2.drawContours(result, [c], -1, (255,255,255), 5)

    plt.imshow(result)
    plt.show()

    return result



# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to input image to be OCR'd")
ap.add_argument("-c", "--min-conf", type=int, default=0,
    help="mininum confidence value to filter weak text detection")
args = vars(ap.parse_args())


reader = easyocr.Reader(['ch_sim','en']) # need to run only once to load model into memory



# load the input image, convert it from BGR to RGB channel ordering,
# and use Tesseract to localize each area of text in the input image
image = cv2.imread(args["image"])
image = remove_lines(image)

results = reader.readtext(image)
#print('originalresult',results)

low_precision = []
for text in results:
    if text[2]<0.45: # precision here
        low_precision.append(text)
for i in low_precision:
    results.remove(i) # remove low precision
print(results)

#import pdb; pdb.set_trace()


image2 = Image.fromarray(image)

draw = ImageDraw.Draw(image2)
for i in range(0, len(results)):
    p0, p1, p2, p3 = results[i][0]
    draw.line([*p0, *p1, *p2, *p3, *p0], fill='red', width=1)

plt.imshow(np.asarray(image2))
plt.show()




【问题讨论】:

    标签: python opencv image-processing computer-vision opencv-contour


    【解决方案1】:

    去除低精度结果后,您可以将所有有效点组合成一个二维数组,并使用cv2.boundingRect 获取边界框。

    代码:

    points = []
    for result in results:
        points.extend(result[0])
    
    rect = cv2.boundingRect(np.array(points))
    
    x, y, w, h = rect
    
    image2 = image.copy()
    cv2.rectangle(image2, (x, y), (x + w, y + h), (255, 0, 0), 1)
    
    plt.imshow(image2)
    plt.show()
    

    图片:

    要裁剪文本,请使用以下行:

    image_cropped = image[y:y+h, x:x+w]
    

    或者如果需要更精确的裁剪:

    mask = np.zeros_like(image)
    # grayscale or color image
    color = 255 if len(mask.shape) == 2 else mask.shape[2] * [255]
    # create a mask
    for result in results:
        cv2.fillConvexPoly(mask, np.array(result[0]), color)
    
    # mask the text, and invert the mask to preserve white background
    image_masked = cv2.bitwise_or(cv2.bitwise_and(image, mask), cv2.bitwise_not(mask))
    
    image_cropped = image_masked[y:y+h, x:x+w]
    

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 1970-01-01
      • 2014-02-20
      • 2021-12-16
      • 2020-03-19
      • 2021-10-20
      • 2020-07-07
      • 2022-01-05
      • 2021-01-22
      相关资源
      最近更新 更多