2014-09-22 48 views
0

我正在使用ANN编写手写识别系统,但我遇到了一个问题: 我想分离扫描图像上的字符并获取每个图像的AABB(我不想绘制它的形象,但只计算此)计算图像上多个对象的AABB

可以认为字符只有黑色和背景只有白色(我已经写了已经阈值算法)

std::vector < unsigned char > px; // pixel data (RGBARGBARGBARGBA...) 
unsigned w, h; // width and height of image 

lodepng::decode(px, w, h, infile); // i use LodePNG to decode image 

for(int i = 0; i < px.size(); i += 4) 
{ 
    unsigned char & r = px[ i ], & g = px[ i + 1 ], & b = px[ i + 2 ], & a = px[ i + 3 ]; 

    // and what now? 
} 

lodepng::encode(outfile, px, w, h); 

Image of problem(抱歉,但我还没有得到足够的代表张贴图像:()

+0

什么是“AABB”? – MSalters 2014-09-22 09:39:36

+0

[链接](http://en.wikipedia.org/wiki/Minimum_bounding_box) – kubawal 2014-09-22 09:41:57

+0

Axis Aligned Bounding Box - ok。 – MSalters 2014-09-22 09:44:48

回答

0

图像中显示的图像处理任务称为“分割”。有很多方法可以做到这一点。最简单的方法是选取第一个黑色像素(最左上角),检查其右边(x + = 1)或其正下方(y + = 1,x + = { - 1,0,1 })也是黑色的。相邻的8个像素也是黑色的。将这些添加到属于同一个字母和递归的像素集合中。为了防止无限递归,您应该只检查在前一次递归中添加的点的邻居,而不是两次添加点。您可以通过创建一个空白画布来跟踪您添加的点,并将该像素值设置为您找到的迭代点。因此,第一个像素的值为1,其邻居的值为2,邻居的邻居的值为3等。

在某个点您已经找到该字母的左下角像素,在输入中将它们全部擦除。这确保新的左上角黑色像素属于第二个字母。

Axis Aligned Bounding Box现在只是字母所有像素的最小/最大x/y。

+0

如果信件是例如A怎么办?它左上角的像素不是其AABB的左上角...... – kubawal 2014-09-22 09:50:11

+0

是的。但是由于在每次迭代中检查的4个相邻像素之一是y + = 1,x + = - 1,因此您会将对角线移至左下角。 – MSalters 2014-09-22 09:52:37

0

我使用了不同的算法(感谢MSalters提供这个想法)。也许它可以帮助某人,所以我给这个伪代码。 (我测试过)

Copy image to image2 
for each(Pixel p in image2) 
{ 
    if(p is black) 
    { 
    Add p to container 
    Set p color to white 
    Call findNeighbours(p position) 

    left top of aabb = (lowest x of pixels in container, lowest y of pixels in container) 
    right down of aabb = (highest x of pixels in container, highest y of pixels in container) 
    Save this aabb 
    Clear container 
    } 
} 
All objects found, all pixels should be white 

function findNeighbours(x, y) 
{ 
    for each(neighbour of pixel (x, y)) 
    { 
    if(this neighbour is black) 
    { 
     Set this neighbour's color to white 
     Add this neighbour's position to container 
     Call findNeighbours(this neighbour's position) 
    } 
    } 
} 
+0

但是这个算法不适用于字符识别:它不能识别例如'我'字符:(我需要这个工作但 – kubawal 2014-09-22 12:15:10

+0

是问题,点被断开吗?这是一个公平的问题本身。 – MSalters 2014-09-23 10:31:27

+0

我将训练ANN识别'我'没有点,然后识别点,然后将它连接成一个字符 – kubawal 2014-09-23 10:43:57