1

투명하지 않은 텍스트가있는 투명한 배경 이미지가 있습니다.PIL의 모든 불투명 영역의 모든 경계 사각형 찾기

텍스트에서 각 단어의 모든 경계 상자를 찾고 싶습니다.

다음은 투명 이미지를 만들고 텍스트 (예 : "Hello World")를 그리는 코드입니다. 그 후 affine 변형 및 축소판을 만듭니다. 여기

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'(부분 결과를 보려면 이미지를 다운로드 할 수 있음)에서 찾을 수 있습니다. 당신이 원하는 것을 들어

result

답변

0

는 개별 단어에 라벨을하고 같은 레이블 각 개체의 경계 상자를 계산해야합니다. 가장 많은 앞쪽 접근법은 그 단어를 구성하는 픽셀의 최소 및 최대 위치를 취하는 것입니다. 라벨 지정이 조금 더 어려워졌습니다. 예를 들어, 형태학 연산을 사용하여 단어의 문자 (형태학적인 시작, see PIL documentation)를 결합한 다음 ImageDraw.floodfill을 사용할 수 있습니다. 또는 처음에 텍스트를 그려 넣은 위치에서 단어의 위치를 ​​예상 할 수 있습니다. code_xcode_y 선택한 글자와 글자의 크기 및 간격 (이것은 생각보다 까다 롭습니다).

+0

어떤 형태 조작을 사용할 수 있습니까? –

+0

형태학적인 개방이라고합니다. 구조체 요소로 사용하는 것에 따라 바이너리 "블롭"영역을 일정량만큼 확장합니다. https://en.wikipedia.org/wiki/Opening_(morphology) – meetaig

관련 문제