影像處理之基礎---映像縮放中的雙線性插值c實現

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   使用   for   strong   

在進入頻域變換之前, 我們還是輕鬆一下,再搞點平面上的變化來看看。這把選了一個雙線性插值(Bilinear interpolation)來實現是源於看到了csdn上別人的問題, 權且實現一個函數,方便大家的使用吧。

雙線性插值簡單的說,就是擴充了之後的映像像素座標映射回原來的座標空間的時候, 如果出現了沒有對應到整數點的情況。這時候需要做2次線性插值計算出新的座標的像素值,比如說:

這裡可以看到這個P點落在了ABCD區間內, 如果我們本著最樸素的這個P點最靠近誰權重就越大的加權平均的思想, 我們很容易得到這樣的論斷:

A點對P的影響就是Sa的面積, B點的影響就是Sb, C點就Sc, d就是Sd。這樣越近就是權重越大,基本上就是這樣的邏輯。

這樣P的像素可以簡單的用 (A*Sa+B*Sb+C*Sc+D*Sd )/(Sa+Sb+Sc+Sd);來得到了。以我的雷厲風行,馬上寫出了如下的代碼:

[cpp] view plaincopyprint?
  1. /** 
  2. ** method to remove sharp the raw image with unsharp mask 
  3. * @param src input grayscale binary array  
  4. * @param dst output grayscale result, the memory need to be allocated outside of the function 
  5. * @param srcWidth width of the input grayscale image 
  6. * @param srcHeight height of the input grayscale image 
  7. * @param scalePercent, scale percentage (0-xxx) 
  8. */  
  9. void stretchImage (const unsigned char* src, unsigned char* dst, int srcWidth, int srcHeight, int scalePercent)  
  10. {     
  11.     if (scalePercent < 0)  
  12.         return;  
  13.     int x, y;  
  14.     int ox, oy;  
  15.     int tmpx,tmpy;  
  16.     int ratio = (100 << 8)/scalePercent;  
  17.     int dstWidth = srcWidth * scalePercent / 100;  
  18.     int dstHeight = srcHeight * scalePercent / 100;  
  19.     unsigned char color[2][2];  
  20.     for (int j = 0; j < dstHeight; j ++)  
  21.     {  
  22.         for (int i = 0; i < dstWidth; i ++)  
  23.         {  
  24.             tmpx = i * ratio;  
  25.             tmpy = j * ratio;  
  26.             ox = tmpx >> 8;  
  27.             oy = tmpy >> 8;  
  28.             x = tmpx & 0xFF;  
  29.             y = tmpy & 0xFF;  
  30.             color[0][0] = src[ oy*srcWidth + ox ];   
  31.             color[1][0] = src[ oy*srcWidth + ox +1 ];   
  32.             color[0][1] = src[ (oy+1)*srcWidth + ox ];   
  33.             color[1][1] = src[ (oy+1)*srcWidth + ox+1 ];  
  34.             int final = (0x100 - x)*(0x100 - y)*color[0][0] + x*(0x100 - y)*color[1][0] + (0x100-x)*y*color[0][1] + x*y*color[1][1];  
  35.             final = final >> 16;  
  36.             if (final>255)  
  37.                 final = 255;  
  38.             if (final<0)  
  39.                 final = 0;  
  40.             dst [ j*dstWidth + i] = (unsigned char)final;  
  41.         }  
  42.     }  
  43. }  

/**** method to remove sharp the raw image with unsharp mask* @param src input grayscale binary array * @param dst output grayscale result, the memory need to be allocated outside of the function* @param srcWidth width of the input grayscale image* @param srcHeight height of the input grayscale image* @param scalePercent, scale percentage (0-xxx)*/void stretchImage (const unsigned char* src, unsigned char* dst, int srcWidth, int srcHeight, int scalePercent){if (scalePercent < 0)return;int x, y;int ox, oy;int tmpx,tmpy;int ratio = (100 << 8)/scalePercent;int dstWidth = srcWidth * scalePercent / 100;int dstHeight = srcHeight * scalePercent / 100;unsigned char color[2][2];for (int j = 0; j < dstHeight; j ++){for (int i = 0; i < dstWidth; i ++){tmpx = i * ratio;tmpy = j * ratio;ox = tmpx >> 8;oy = tmpy >> 8;x = tmpx & 0xFF;y = tmpy & 0xFF;color[0][0] = src[ oy*srcWidth + ox ]; color[1][0] = src[ oy*srcWidth + ox +1 ]; color[0][1] = src[ (oy+1)*srcWidth + ox ]; color[1][1] = src[ (oy+1)*srcWidth + ox+1 ];int final = (0x100 - x)*(0x100 - y)*color[0][0] + x*(0x100 - y)*color[1][0] + (0x100-x)*y*color[0][1] + x*y*color[1][1];final = final >> 16;if (final>255)final = 255;if (final<0)final = 0;dst [ j*dstWidth + i] = (unsigned char)final;}}}

 

 

需要說明的事情是, 浮點數需要引入效率上一定的損失, 當然我們這裡就用大數來和諧。但是只是隨便寫寫的代碼, 我們沒有加入超出int範圍的檢查或者說明,暫時也只能這樣了:)。用了這個函數的效果還是不錯的, 我們來看看在75%,125%和250%時候的效果:

原圖:

%75:

125%:

250%:

其實從多少可以看出一些的問題就是, 隨著映像的展開, 映像的銳度其實降低了, 這個比較容易想象的,因為我們這個展開的辦法本身就是線性,無疑來擴大的時候把銳利的邊緣模糊化了,所以自然在映像擴大很多倍的時候效果不是很好了。

 

http://blog.csdn.net/hhygcy/article/details/4434870#comments

影像處理之基礎---映像縮放中的雙線性插值c實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.