2011-05-24 49 views
2

我正在尋找一個簡單的剪切算法。要剪切的圖像是二進制的(0-背景像素,1-前景像素),由二維數組表示。它將用於手寫數字傾斜校正,因此剪切只需在x軸上完成。二進制圖像剪切算法

我發現了一些數學解釋,但不知道如何正確實現它。

謝謝!

+0

你的問題如何成爲語言不可知*和*需要實現細節? – YXD 2011-05-24 16:03:42

+1

@MrE我認爲他意味着他無法從數學公式轉換爲基於代碼的算法。在這種情況下,提供僞代碼可能會有幫助。 – Diego 2011-05-24 16:21:24

+1

@Diego是的,這就是我的意思。 – 2011-05-24 16:23:58

回答

3

只需循環遍歷行,從最下面一行開始,並跟蹤沿x軸的當前像素轉換(作爲浮點數或定點數)。在每行之後,按照所需的恆定斜率增加移位。爲了繪圖的目的,你可以在每一行取對應的pixelshift的最接近的整數。

僞代碼,這將是:

slope = 0.2; // one pixel shift every five rows 
shift = 0.0; // current pixelshift along x-axis 
for (row = rows-1; row>=0; row--) { 
    integershift = round(shift) // round to nearest integer 
    for (column = columns-1; column>=0; column--) { 
    sourcecolumn = column + integershift; // get the pixel from this column 
    if (sourcecolumn < columns) 
     outputImage[row][column] = inputImage[row][sourcecolumn]; 
    else // draw black if we're outside the inputImage 
     outputImage[row][column] = 0; 
    } 
    shift += slope; 
} 

這基本上是Bresenham line drawing algorithm,所以你會發現大量的實施細節這一點。