【问题标题】:Finding all the bounding rectangles of all non-transparent regions in PIL在 PIL 中查找所有非透明区域的所有边界矩形
【发布时间】:2016-08-12 13:12:23
【问题描述】:

我有一张带有一些不透明文字的透明背景图片。

我想找到文本中每个单词的所有边界框。

这是创建透明图像并绘制一些文本(例如“Hello World”)的代码,然后进行仿射变换和缩略图。

from PIL import Image, ImageFont, ImageDraw, ImageOps
import numpy as np

fontcolor = (255,255,255)
fontsize  = 180
# padding rate for setting the image size of font
fimg_padding = 1.1
# check code bbox padding rate
bbox_gap = fontsize * 0.05
# Rrotation +- N degree

# Choice a font type for output---
font = ImageFont.truetype('Fonts/Bebas.TTF', fontsize)

# the text is "Hello World"
code = "Hello world"
# Get the related info of font---
code_w, code_h = font.getsize(code)

# Setting the image size of font---
img_size = int((code_w) * fimg_padding)

# Create a RGBA image with transparent background
img = Image.new("RGBA", (img_size,img_size),(255,255,255,0))
d = ImageDraw.Draw(img)

# draw white text
code_x = (img_size-code_w)/2
code_y = (img_size-code_h)/2
d.text( ( code_x, code_y ), code, fontcolor, font=font)
# img.save('initial.png')

# Transform the image---
img = img_transform(img)

# crop image to the size equal to the bounding box of whole text
alpha = img.split()[-1]
img = img.crop(alpha.getbbox())

# resize the image
img.thumbnail((512,512), Image.ANTIALIAS)

# img.save('myimage.png')

# what I want is to find all the bounding box of each individual word
boxes=find_all_bbx(img)

这是关于仿射变换的代码(这里提供给想要做一些实验的人)

def find_coeffs(pa, pb):
    matrix = []
    for p1, p2 in zip(pa, pb):
        matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0]*p1[0], -p2[0]*p1[1]])
        matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1]*p1[0], -p2[1]*p1[1]])

    A = np.matrix(matrix, dtype=np.float)
    B = np.array(pb).reshape(8)

    res = np.dot(np.linalg.inv(A.T * A) * A.T, B)
    return np.array(res).reshape(8)

def rand_degree(st,en,gap):
    return (np.fix(np.random.random()* (en-st) * gap )+st)

def img_transform(img):
    width, height = img.size
    print  img.size
    m = -0.5
    xshift = abs(m) * width
    new_width = width + int(round(xshift))
    img = img.transform((new_width, height), Image.AFFINE,
            (1, m, -xshift if m > 0 else 0, 0, 1, 0), Image.BICUBIC)

    range_n = width*0.2
    gap_n = 1

    x1 = rand_degree(0,range_n,gap_n)
    y1 = rand_degree(0,range_n,gap_n)

    x2 = rand_degree(width-range_n,width,gap_n)
    y2 = rand_degree(0,range_n,gap_n)

    x3 = rand_degree(width-range_n,width,gap_n)
    y3 = rand_degree(height-range_n,height,gap_n)

    x4 = rand_degree(0,range_n,gap_n)
    y4 = rand_degree(height-range_n,height,gap_n)

    coeffs = find_coeffs(
             [(x1, y1), (x2, y2), (x3, y3), (x4, y4)],
            [(0, 0), (width, 0), (new_width, height), (xshift, height)])

    img = img.transform((width, height), Image.PERSPECTIVE, coeffs, Image.BICUBIC)
    return img

如何实现find_all_bbx找到每个单词的边界框?

例如,可以在“H”中找到其中一个框(您可以下载图像以查看部分结果)。

【问题讨论】:

    标签: python numpy image-processing python-imaging-library bounding-box


    【解决方案1】:

    对于您想要做的事情,您需要标记各个单词,然后使用相同的标签计算每个对象的边界框。 这里最直接的方法就是获取构成该单词的像素的最小和最大位置。 标记有点困难。例如,您可以使用形态学操作来组合单词的字母(形态学开头,see PIL documentation),然后使用ImageDraw.floodfill。或者您可以尝试从您第一次绘制文本的位置预测单词的位置 code_xcode_y 以及所选字体和字母大小以及间距(我认为这会更棘手)。

    【讨论】:

    猜你喜欢
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2012-04-30
    相关资源
    最近更新 更多