雙線性插值(Bilinear interpolation)的映像展開在mobile上面的實現

來源:互聯網
上載者:User

在進入頻域變換之前, 我們還是輕鬆一下,再搞點平面上的變化來看看。這把選了一個雙線性插值(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);來得到了。以我的雷厲風行,馬上寫出了如下的代碼:

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

 

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

原圖:

%75:

125%:

250%:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.