雙線性插值(Bilinear interpolation)
雙線性插值簡單的說,
這裡可以看到這個P點落在了ABCD區間內, 如果我們本著最朴
A點對P的影響就是Sa的面積, B點的影響就是Sb, C點就Sc, d就是Sd。這樣越近就是權重越大,基本上就是這樣的邏輯。
這樣P的像素可以簡單的用 (A*Sa+B*Sb+C*Sc+
view plaincopy to clipboardprint?
/**
** 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;
}
}
}
需要說明的事情是, 浮點數需要引入效率上一定的損失, 當然我
原圖:
%75效果圖:
125%效果圖:
250%效果圖:
其實從效果圖多少可以看出一些的問題就是, 隨著圖像的拉伸,
本文來自CSDN博客,轉載請標明出處:http://