標籤: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?
- /**
- ** 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;
- }
- }
- }
/**** 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實現