2011-07-13 22 views
1

有沒有人有一個不錯的定點數學1通道銳化濾鏡? 應該用C語言使用定點數學來完成。定點數學銳化濾鏡

可能聲明:

void sharpen(uint8_t *src, uint8_t *dest, int srcdstpitch, int height); 

主編: 「最好的算法實現得到300代表賞金」。似乎不可能在你自己的問題上得到賞金oo。儘管如此 - 我會仔細審閱任何獲獎者的答案和30個答案:)。

+1

「銳化過濾器」通常非常難看。要執行實際銳化,您需要一個非線性濾鏡,在圖像上執行某種扭曲。任何線性濾波器或任何僅僅是相對於目標像素的固定位置處的像素值函數的濾波器都會給出非常難看的結果。我想我的觀點是,銳化是一個*不同的難題*,找到一個定點執行可能會更難...... –

+0

您的聲明不能編譯。 'int srcdst pitch'是什麼意思? –

+0

@Edgar Bonet錯字,感謝 – Ulterior

回答

2

好的,這是我的嘗試。很簡單線性過濾器,只着眼於最近的鄰居,但它的工作原理:

編輯:改變了代碼分別處理圖像的邊緣, 性能的目的。使功能的銳化 參數的強度。

/* Simple Laplacian sharpening. */ 
void sharpen(uint8_t *src, uint8_t *dest, int width, int height, int strength) 
{ 
    int i, j; 
    int here, north, south, west, east; 
    int sharpening; 
    static const int scale = 1024; 

    /* Handle interior pixels. */ 
    for (i = 1; i < height-1; i++) for (j = 1; j < width-1; j++) { 

     /* This pixel and it's neighbors. */ 
     here = src[width*i+j]; 
     north = src[width*(i-1)+j]; 
     south = src[width*(i+1)+j]; 
     west = src[width*i+(j-1)]; 
     east = src[width*i+(j+1)]; 

     /* Filter. */ 
     sharpening = 4 * here - (north + south + west + east); 
     here += strength * sharpening/scale; 

     /* Store clipped result. */ 
     dest[width*i+j] = here<0 ? 0 : here>255 ? 255 : here; 
    } 

    /* Optimization: handle edges separately. */ 
    for (i = 0; i < height; i++) { 
     int j_step = (i==0 || i==height-1) ? 1 : width-1; 

     for (j = 0; j < width; j += j_step) { 

      /* Expand the image by symmetry. */ 
      north = i==0 ? src[width*(1)+j] : src[width*(i-1)+j]; 
      south = i==height-1 ? src[width*(height-2)+j] : src[width*(i+1)+j]; 
      west = j==0 ? src[width*i+(1)] : src[width*i+(j-1)]; 
      east = j==width-1 ? src[width*i+(width-2)] : src[width*i+(j+1)]; 

      /* Same as the code for the interior. */ 
      here = src[width*i+j]; 
      sharpening = 4 * here - (north + south + west + east); 
      here += strength * sharpening/scale; 
      dest[width*i+j] = here<0 ? 0 : here>255 ? 255 : here; 
     } 
    } 
} 

我試過用PGM圖像。您可以用最後一個參數調整銳化 的強度。 100的優勢是一個很好的起點。

+0

非常好,比我的舊算法快3倍 – Ulterior

+0

如果你在追趕速度,你可能想要在單獨的循環中處理圖像的邊緣。這樣內部的循環被簡化了('north = src [width *(i-1)+ j];'等等)。我的測試表明,這樣可以提高23%的速度。 –

+0

你可以在這裏更新代碼嗎?我願意接受你的答案,因爲我不期望比那更好 – Ulterior

1

你必須從this gimp plug-in提取代碼;似乎並不難。

+0

我猜,他不會忘記許可證條款。 –