为什么我绘制的边界框是倒置的? [英] Why are my drawn bounding boxes inverted?

查看:63
本文介绍了为什么我绘制的边界框是倒置的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我遗漏了一些非常简单的概念,或者可能不了解 PIL.ImageDraw 或 pytesseract 创建的输出读取/绘制事物的方向......无论如何我的问题是为什么我的边界框倒置?"

I think I am missing some really simple concept or perhaps not understanding the directions in which things are read/drawn by either PIL.ImageDraw or the output created by pytesseract...In any case my question is "Why are my bounding boxes inverted?"

示例代码如下:从 PIL 导入 Image,ImageDraw导入 pytesseract从 pytesseract 导入输出

Example code as follows: from PIL import Image,ImageDraw import pytesseract from pytesseract import Output

input_image = Image.open('input_sample.jpg')
tess_boxes = pytesseract.image_to_boxes(input_image,output_type=Output.DICT)
draw = ImageDraw.Draw(input_image)

for idx,character in enumerate(tess_boxes['char']):

    #Get each point needed to draw the box
    left = tess_boxes['left'][idx]
    right = tess_boxes['right'][idx]
    bottom = tess_boxes['bottom'][idx]
    top = tess_boxes['top'][idx]

    #Re-arranging these seem to have no effect
    # y = (left,top)
    # x = (right,bottom)
    # runs the same as the following: 
    y = (right,bottom)
    x = (left,top)

    #Swapping x and y here has no visible effect
    draw.rectangle((x,y),fill=None,outline="#FF0000",width=3)

input_image.save('output_sample.png', "PNG")

输入图像

输出图像

推荐答案

您也可以使用 image_to_data.您不需要进行算术运算.

You can also use image_to_data. You don't need to do arithmetic operations.

import pytesseract

# Load the image
img = cv2.imread("cRPKk.jpg")

# Convert to gray-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# OCR
d = pytesseract.image_to_data(gry, output_type=pytesseract.Output.DICT)
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(img, (x, y), (x + w, y + h), (0, 0, 255), 2)

cv2.imshow("img", img)
cv2.waitKey(0)

结果:

这篇关于为什么我绘制的边界框是倒置的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆