如何确定边界框的高度以从字体图像中提取字形?

2024-04-25 22:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个高,窄,图像有一个可打印字符(ascii32到126),每行一个,从GIMP导出为'c'文件:我用这个为嵌入式系统构建每像素1位的字体结构。你知道吗

/* GIMP RGB C-Source image dump (font.c) */

static const struct {
  unsigned int       width;
  unsigned int       height;
  unsigned int       bytes_per_pixel; /* 3:RGB, 4:RGBA */
  unsigned char      pixel_data[32 * 2400 * 3 + 1];
} gimp_image = {
  32, 2400, 3,
  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
...

我想找出3个参数来描述,行高,行间距和起始行偏移。你知道吗

我编写了一些代码,告诉我图像中哪些行是空的:

#include "font.c"

#include <stdio.h>

struct image_attributes {
    unsigned int width, height, stride;
};

int main(int argc, char **argv)
{
    const struct image_attributes * const ia = (const struct image_attributes *)&gimp_image;
    unsigned int line_number, last_line, min_seperation;
    int last_state;

    min_seperation = UINT_MAX;

    for (line_number = 0; line_number < ia->height; line_number++) {
            const unsigned char * data;
            unsigned int line_width;
            int new_state;
            size_t index = line_number * ia->stride * ia->width;

            for (data = gimp_image.pixel_data + index, line_width = 0; *data && line_width < ia->width; line_width++, data += ia->stride) {
            }
            new_state = line_width < ia->width;
            if (!line_number) {
                    last_state = new_state ^ 1;
                    last_line = line_number;
            }

            if (last_state != new_state) {
                    if (!new_state) {
                            printf("%4d - ", line_number);
                    } else {
                            unsigned seperation = line_number - last_line;
                            printf("%4d = %d\n", line_number, seperation);
                            if (min_seperation > seperation) {
                                    min_seperation = seperation;
                            }
                    }
                    last_line = line_number;
            }
            last_state = new_state;
    }

    printf("%4d = %d\n", line_number, line_number - last_line);

    printf("\nMinimum Seperation = %d\n", min_seperation);
    return 0;
}

有了这些,我想弄清楚我的算法是什么。我将首先提供行高(两个类似的glyph之间由一行分隔),但我想有一种方法可以做到这一点,而不需要先验的信息。你知道吗

我考虑的一个想法是研究连续的空行块之间的公约数。我认为这可能会导致投票机制,从而可以确定最有可能的线高度,并且从那里它成为一个在空白行数据上混叠以找到最佳匹配的练习。你知道吗

对于什么样的方法可能是成功的,我会非常感激:而且我只限于在Linux命令行上使用什么(即“c”、Python、Perl等)


Tags: imagenumbernewdatalineminwidthstruct