1

私は透明でない背景のイメージをいくつかの不透明なテキストで持っています。PIL内のすべての非透明領域のすべての境界矩形を見つける

そして、テキスト内の個々の単語のすべての境界ボックスを探したいと思います。

透明な画像を作成してテキスト(「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を実装する方法ですか?

たとえば、ボックスの1つは 'H'にあります(画像をダウンロードして部分的な結果を確認できます)。あなたが何をしたいかについては

result

答えて

0

あなたは個々の単語をラベル付けし、同じラベルで、各オブジェクトのバウンディングボックスを計算する必要があります。 ここで最も真っ直ぐなアプローチは、その単語を構成するピクセルの最小位置と最大位置を取ることです。 ラベルは少し難しいです。たとえば、モルフォロジー演算を使用して単語の文字を組み合わせると(see PIL documentation)、ImageDraw.floodfillを使用することができます。または、最初にテキストを描く位置からの単語の位置を予測することができます code_xcode_y と選択された文字のフォントとサイズとスペース(これは私が考えるとやや難しいでしょう)。

+0

私はどの形態操作を使用できますか? –

+0

それは形態学的開口部と呼ばれる。これは、バイナリ "ブロブ"の領域を、構造要素として使用するものに応じて一定量だけ拡張します。 https://en.wikipedia.org/wiki/Opening_(morphology) – meetaig

関連する問題