OpenCV 矩形填满整个屏幕

OpenCV Rectangle Fills Entire Screen

我正在尝试围绕一些 OCR 文本绘制一个填充矩形。

    d = pytesseract.image_to_data(image, output_type=Output.DICT)
    os.remove(filename);

    n_boxes = len(d['level'])
    for i in range(n_boxes):
        (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 0), 1)

    cv2.imwrite("./output/{}.png".format("blurred_" + str(os.getpid())), image);

这一行似乎是问题所在: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 0), 1)

当我用 -1 替换 1 时,它应该绘制填充的矩形,而是用填充的矩形填充整个图像。

没有填充,您可以看到应该着色的内容:

有填充:

如果需要,这里是完整代码:

from PIL import Image
import pytesseract
from pytesseract import Output
import argparse, cv2, os
import numpy as np

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path of image to be blurrified")
ap.add_argument("-b", "--blurtype", required=True,
    help="blur [words] or [character]s")
ap.add_argument("-p", "--preprocess", type=str, default="thresh",
    help="preprocess type")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
h, w, _ = image.shape
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

if args["preprocess"] == "thresh":
    gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
elif args["preprocess"] == "blur":
    gray = cv2.medianBlur(gray, 3)

filename = "./temp/{}.png".format("grayscale_" + str(os.getpid()))
cv2.imwrite(filename, gray)

if args["blurtype"] == "character":
    
    text = pytesseract.image_to_boxes(Image.open(filename))
    os.remove(filename);

    for b in text.splitlines():
        b = b.split(' ')
        image = cv2.rectangle(image, (int(b[1]), h - int(b[2])), (int(b[3]), h - int(b[4])), (0, 0, 0), -1)

    cv2.imwrite("./output/{}.png".format("blurred_" + str(os.getpid())), image);
elif args["blurtype"]== "words":

    d = pytesseract.image_to_data(image, output_type=Output.DICT)
    os.remove(filename);

    n_boxes = len(d['level'])
    for i in range(n_boxes):
        (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 0), 1)

    cv2.imwrite("./output/{}.png".format("blurred_" + str(os.getpid())), image);

不胜感激!

pytesseract 也在制作完整图像的边界框。要查看它,厚度为 1,制作颜色为 (0, 255, 0) 的矩形。因此,当您传递 thickness = -1 时,图像的边界框也会着色,因此竞争图像也会着色。为避免这种情况,请拒绝该矩形。