2014-09-22 19 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