2016-03-04 65 views
-3

从Python中的某些字符串文本中获取“关键”点集的最简单方法是什么?请看例子。关键点以蓝色和黄色突出显示。 我需要在以后的基础上建立3D图形。将Python字符串文本设置为点

enter image description here

+0

获得“密钥”从图像或从一个字符串设定点?需要从字符串中获取 –

+0

。 – sevatster

+3

这将取决于字体,对不对?你还需要澄清你的想法,关键点的定义是什么?字母O是否有重点?目前,这个问题太广泛/不清楚。 – timgeb

回答

0

所以对于3D模型,你或许需要比只有一些几点思考字母“S”更多。创建了一些代码以获得通用的坐标。你可以玩弄字体大小和图片大小,以获得更多或更少的点数。该ARIAL.TTF从https://github.com/JotJunior/PHP-Boleto-ZF2/blob/master/public/assets/fonts/arial.ttf

from PIL import Image 
from PIL import ImageDraw 
from PIL import ImageFont 
import numpy as np 
import cv2 


print np.__version__ 

def getPixelColor(pixel, x, y): 
    try: 
     return pixel[x,y] 
    except IndexError: 
     return (255,255,255) 

def hasNeighbourColor(color, pixels, x, y): 
    if getPixelColor(pixels,x-1,y-1) == color or getPixelColor(pixels,x,y-1) == color or getPixelColor(pixels,x+1,y-1) == color \ 
     or getPixelColor(pixels,x-1,y) == color or getPixelColor(pixels,x+1,y) == color\ 
     or getPixelColor(pixels,x-1,y+1) == color or getPixelColor(pixels,x,y+1) == color or getPixelColor(pixels,x+1,y+1) == color: 
     return True 
    return False 

def createMapOfLetter(letter): 
    print "LETTER %s:" % letter 
    print "---------" 

    img = Image.new('RGB', (230, 230), "white") 
    d = ImageDraw.Draw(img) 
    font = ImageFont.truetype("arial.ttf", 300) 
    d.text((15, -50), letter, fill=(0, 0, 0), font=font) 
    img.save('letter-%s.png' % letter, 'png') 

    pixels = img.load() 

    ans = [] 

    for x in range(230): 
     for y in range(230): 
      if pixels[x,y] == (0,0,0): 
       if hasNeighbourColor((255,255,255), pixels, x, y) and not hasNeighbourColor((255,0,0), pixels, x, y): 
        pixels[x,y] = (255,0,0) 
        ans.append([x,y]) 

    for x in range(230): 
     for y in range(230): 
      if pixels[x,y] != (255,0,0): 
       pixels[x,y] = (255,255,255) 

    img.save('letter_map-%s.png' % letter, 'png') 
    print ans 


createMapOfLetter('A') 

下载的脚本创建信纸[信]巴纽看起来像这样。留下了一个正确s

lettera letters

,然后创建坐标列表。图片中的坐标看起来像这样。该脚本自动创建一个letter_map- [letter] .png。左A,右S

lettermap lettermap-s

坐标的一看这个样子输出。

[[15, 221], [17, 215], [17, 221], [19, 221], [21, 221], [22, 202], [23, 221], [25, 194], [25, 221], [27, 221], 
[29, 221], [30, 181], [31, 221], [33, 173], [33, 221], [35, 221], [37, 221], [38, 160], [39, 221], [41, 152], 
[41, 221], [43, 221], [45, 218], [46, 139], [49, 131], [52, 198], [53, 195], [54, 118], [54, 192], [57, 110], 
[61, 172], [62, 97], [62, 169], [63, 166], [65, 89], [67, 156], [69, 156], [70, 76], [71, 156], [73, 68], 
[73, 156], [75, 156], [76, 134], [77, 129], [77, 156], [78, 55], [78, 134], [79, 156], [80, 134], [81, 47], 
[81, 118], [81, 156], [82, 134], [83, 156], [84, 134], [85, 107], [85, 156], [86, 34], [86, 134], [87, 156], 
[88, 134], [89, 26], [89, 96], [89, 156], [90, 134], [91, 156], [92, 134], [93, 85], [93, 156], [94, 13], [94, 134], 
[95, 156], [96, 134], [97, 7], [97, 74], [97, 156], [98, 71], [98, 134], [99, 7], [99, 156], [100, 134], [101, 7], 
[101, 156], [102, 134], [103, 7], [103, 56], [103, 156], [104, 134], [105, 7], [105, 156], [106, 46], [106, 134], 
[107, 7], [107, 156], [108, 134], [109, 7], [109, 35], [109, 156], [110, 31], [110, 134], [111, 7], [111, 156], 
[112, 134], [113, 7], [113, 156], [114, 37], [114, 134], [115, 7], [115, 40], [115, 156], [116, 43], [116, 134], 
[117, 7], [117, 46], [117, 156], [118, 49], [118, 134], [119, 7], [119, 156], [120, 134], [121, 7], [121, 156], 
[122, 134], [123, 7], [123, 63], [123, 156], [124, 66], [124, 134], [125, 7], [125, 156], [126, 134], [127, 74], 
[127, 156], [128, 134], [129, 156], [130, 134], [131, 85], [131, 156], [132, 134], [133, 156], [134, 134], [135, 96], 
[135, 156], [136, 134], [137, 32], [137, 156], [138, 104], [138, 134], [139, 37], [139, 156], [140, 134], [141, 42], 
[141, 156], [142, 115], [142, 134], [143, 47], [143, 156], [144, 134], [145, 52], [145, 123], [145, 156], [146, 134], 
[147, 57], [147, 156], [148, 134], [149, 62], [149, 156], [151, 67], [151, 156], [153, 72], [153, 156], [155, 77], 
[155, 156], [157, 82], [157, 156], [159, 87], [160, 161], [164, 172], [168, 183], [171, 191], [175, 202], [178, 210], 
[180, 139], [182, 144], [182, 221], [184, 149], [184, 221], [186, 154], [186, 221], [188, 159], [188, 221], [190, 164], 
[190, 221], [192, 169], [192, 221], [194, 174], [194, 221], [196, 179], [196, 221], [198, 184], [198, 221], [200, 189], 
[200, 221], [202, 194], [202, 221], [204, 199], [204, 221], [206, 221], [208, 221], [210, 221], [212, 221]] 

输出为S的坐标如下所示

[[30, 162], [31, 167], [35, 179], [38, 52], [39, 187], [41, 81], [43, 193], [47, 198], 
[49, 28], [52, 203], [57, 101], [57, 161], [58, 19], [61, 172], [62, 174], [64, 15], 
[66, 52], [66, 68], [67, 49], [67, 181], [68, 73], [68, 108], [69, 45], [70, 76], [70, 109], 
[71, 42], [71, 185], [74, 10], [75, 81], [77, 36], [78, 190], [78, 218], [80, 34], [85, 220], [86, 31], 
[89, 30], [89, 116], [89, 221], [90, 88], [91, 5], [91, 196], [93, 89], [97, 4], [98, 28], [103, 120], 
[107, 93], [107, 121], [111, 94], [111, 122], [127, 28], [128, 4], [130, 127], [134, 223], [135, 100], 
[136, 30], [139, 6], [139, 31], [140, 130], [140, 222], [143, 131], [146, 8], [148, 133], [154, 136], 
[156, 44], [157, 189], [157, 217], [159, 49], [161, 141], [162, 15], [162, 109], [163, 61], [163, 143], 
[164, 110], [166, 111], [167, 149], [167, 212], [168, 19], [169, 171], [177, 118], [177, 205], [182, 200], 
[188, 129], [189, 49], [190, 132], [190, 189], [195, 178], [197, 150]] 
+0

哇,非常感谢! 这正是我想要的! – sevatster

+0

@sevatster不客气。如果您需要分辨率更高的分辨率,只需说出来,只需在将图像读取成黑白图像之前转换图像即可。 – Kordi

相关问题