欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
圖像處理之三種常見(jiàn)雙立方插值算法

圖像處理之三種常見(jiàn)雙立方插值算法

雙立方插值計算涉及到16個(gè)像素點(diǎn),其中(i’, j’)表示待計算像素點(diǎn)在源圖像中的包含

小數部分的像素坐標,dx表示X方向的小數坐標,dy表示Y方向的小數坐標。具體

可以看下圖:

根據上述圖示與雙立方插值的數學(xué)表達式可以看出,雙立方插值本質(zhì)上圖像16個(gè)像素點(diǎn)

權重卷積之和作為新的像素值。

其中R(x)表示插值表達式,可以根據需要選擇的表達式不同。常見(jiàn)有基于三角取值、Bell

分布表達、B樣條曲線(xiàn)表達式。

1. 基于三角形采樣數學(xué)公式為


最簡(jiǎn)單的線(xiàn)性分布,代碼實(shí)現如下:

  1. private double triangleInterpolation( double f )  
  2. {  
  3.     f = f / 2.0;  
  4.     if( f < 0.0 )  
  5.     {  
  6.         return ( f + 1.0 );  
  7.     }  
  8.     else  
  9.     {  
  10.         return ( 1.0 - f );  
  11.     }  
  12. }  
2.基于Bell分布采樣的數學(xué)公式如下:


Bell分布采樣數學(xué)公式基于三次卷積計算實(shí)現。代碼實(shí)現如下:

  1. private double bellInterpolation( double x )  
  2. {  
  3.     double f = ( x / 2.0 ) * 1.5;  
  4.     if( f > -1.5 && f < -0.5 )  
  5.     {  
  6.         return0.5 * Math.pow(f + 1.52.0));  
  7.     }  
  8.     else if( f > -0.5 && f < 0.5 )  
  9.     {  
  10.         return 3.0 / 4.0 - ( f * f );  
  11.     }  
  12.     else if( ( f > 0.5 && f < 1.5 ) )  
  13.     {  
  14.         return0.5 * Math.pow(f - 1.52.0));  
  15.     }  
  16.     return 0.0;  
  17. }  
3.基于B樣條曲線(xiàn)采樣的數學(xué)公式如下:


是一種基于多項式的四次卷積的采樣計算,代碼如下:

  1. private double bspLineInterpolation( double f )  
  2. {  
  3.     if( f < 0.0 )  
  4.     {  
  5.         f = -f;  
  6.     }  
  7.   
  8.     if( f >= 0.0 && f <= 1.0 )  
  9.     {  
  10.         return ( 2.0 / 3.0 ) + ( 0.5 ) * ( f* f * f ) - (f*f);  
  11.     }  
  12.     else if( f > 1.0 && f <= 2.0 )  
  13.     {  
  14.         return 1.0 / 6.0 * Math.pow( ( 2.0 - f  ), 3.0 );  
  15.     }  
  16.     return 1.0;  
  17. }  
實(shí)現圖像雙立方插值的完整源代碼如下:
  1. package com.gloomyfish.zoom.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.awt.image.ColorModel;  
  5.   
  6. import com.gloomyfish.filter.study.AbstractBufferedImageOp;  
  7.   
  8. public class BicubicInterpolationFilter extends AbstractBufferedImageOp  {  
  9.     public final static int TRIANGLE__INTERPOLATION = 1;  
  10.     public final static int BELL__INTERPOLATION = 2;  
  11.     public final static int BSPLINE__INTERPOLATION = 4;  
  12.     public final static int CATMULLROOM__INTERPOLATION = 8;  
  13.     public final static double B = 0.0;  
  14.     public final static double C = 0.5// constant  
  15.     private int destH; // zoom height  
  16.     private int destW; // zoom width  
  17.     private int type;  
  18.     public BicubicInterpolationFilter()  
  19.     {  
  20.         this.type = BSPLINE__INTERPOLATION;  
  21.     }  
  22.     public void setType(int type) {  
  23.         this.type = type;  
  24.     }  
  25.     public void setDestHeight(int destH) {  
  26.         this.destH = destH;  
  27.     }  
  28.   
  29.     public void setDestWidth(int destW) {  
  30.         this.destW = destW;  
  31.     }  
  32.       
  33.     private double bellInterpolation( double x )  
  34.     {  
  35.         double f = ( x / 2.0 ) * 1.5;  
  36.         if( f > -1.5 && f < -0.5 )  
  37.         {  
  38.             return0.5 * Math.pow(f + 1.52.0));  
  39.         }  
  40.         else if( f > -0.5 && f < 0.5 )  
  41.         {  
  42.             return 3.0 / 4.0 - ( f * f );  
  43.         }  
  44.         else if( ( f > 0.5 && f < 1.5 ) )  
  45.         {  
  46.             return0.5 * Math.pow(f - 1.52.0));  
  47.         }  
  48.         return 0.0;  
  49.     }  
  50.       
  51.     private double bspLineInterpolation( double f )  
  52.     {  
  53.         if( f < 0.0 )  
  54.         {  
  55.             f = -f;  
  56.         }  
  57.   
  58.         if( f >= 0.0 && f <= 1.0 )  
  59.         {  
  60.             return ( 2.0 / 3.0 ) + ( 0.5 ) * ( f* f * f ) - (f*f);  
  61.         }  
  62.         else if( f > 1.0 && f <= 2.0 )  
  63.         {  
  64.             return 1.0 / 6.0 * Math.pow( ( 2.0 - f  ), 3.0 );  
  65.         }  
  66.         return 1.0;  
  67.     }  
  68.       
  69.     private double triangleInterpolation( double f )  
  70.     {  
  71.         f = f / 2.0;  
  72.         if( f < 0.0 )  
  73.         {  
  74.             return ( f + 1.0 );  
  75.         }  
  76.         else  
  77.         {  
  78.             return ( 1.0 - f );  
  79.         }  
  80.     }  
  81.       
  82.     private double CatMullRomInterpolation( double f )  
  83.     {  
  84.         if( f < 0.0 )  
  85.         {  
  86.             f = Math.abs(f);  
  87.         }  
  88.         if( f < 1.0 )  
  89.         {  
  90.             return ( ( 12 - 9 * B - 6 * C ) * ( f * f * f ) +  
  91.                 ( -18 + 12 * B + 6 *C ) * ( f * f ) +  
  92.                 ( 6 - 2 * B ) ) / 6.0;  
  93.         }  
  94.         else if( f >= 1.0 && f < 2.0 )  
  95.         {  
  96.             return ( ( -B - 6 * C ) * ( f * f * f )  
  97.                 + ( 6 * B + 30 * C ) * ( f *f ) +  
  98.                 ( - ( 12 * B ) - 48 * C  ) * f +  
  99.                 8 * B + 24 * C)/ 6.0;  
  100.         }  
  101.         else  
  102.         {  
  103.             return 0.0;  
  104.         }  
  105.     }   
  106.   
  107.     @Override  
  108.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  109.         int width = src.getWidth();  
  110.         int height = src.getHeight();  
  111.   
  112.         if (dest == null)  
  113.             dest = createCompatibleDestImage(src, null);  
  114.   
  115.         int[] inPixels = new int[width * height];  
  116.         int[] outPixels = new int[destH * destW];  
  117.         getRGB(src, 00, width, height, inPixels);  
  118.         float rowRatio = ((float) height) / ((float) destH);  
  119.         float colRatio = ((float) width) / ((float) destW);  
  120.         int index = 0;  
  121.         for (int row = 0; row < destH; row++) {  
  122.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  123.             double srcRow = ((float) row) * rowRatio;  
  124.             // 獲取整數部分坐標 row Index  
  125.             double j = Math.floor(srcRow);  
  126.             // 獲取行的小數部分坐標  
  127.             double t = srcRow - j;  
  128.             for (int col = 0; col < destW; col++) {  
  129.                 double srcCol = ((float) col) * colRatio;  
  130.                 // 獲取整數部分坐標 column Index  
  131.                 double k = Math.floor(srcCol);  
  132.                 // 獲取列的小數部分坐標  
  133.                 double u = srcCol - k;  
  134.                 double[] rgbData = new double[3];  
  135.                 double rgbCoffeData = 0.0;  
  136.                 for(int m=-1; m<3; m++)  
  137.                 {  
  138.                     for(int n=-1; n<3; n++)  
  139.                     {  
  140.                         int[] rgb = getPixel(j+m, k+n, width, height, inPixels);  
  141.                         double f1 = 0.0d;  
  142.                         double f2 = 0.0d;  
  143.                         if(type == TRIANGLE__INTERPOLATION)  
  144.                         {  
  145.                             f1  = triangleInterpolation( ((double) m ) - t );  
  146.                             f2 = triangleInterpolation ( -(( (double) n ) - u ) );    
  147.                         }  
  148.                         else if(type == BELL__INTERPOLATION)  
  149.                         {  
  150.                             f1  = bellInterpolation( ((double) m ) - t );  
  151.                             f2 = bellInterpolation ( -(( (double) n ) - u ) );    
  152.                         }  
  153.                         else if(type == BSPLINE__INTERPOLATION)  
  154.                         {  
  155.                             f1  = bspLineInterpolation( ((double) m ) - t );  
  156.                             f2 = bspLineInterpolation ( -(( (double) n ) - u ) );     
  157.                         }  
  158.                         else  
  159.                         {  
  160.                             f1  = CatMullRomInterpolation( ((double) m ) - t );  
  161.                             f2 = CatMullRomInterpolation ( -(( (double) n ) - u ) );                              
  162.                         }  
  163.                         // sum of weight  
  164.                         rgbCoffeData += f2*f1;  
  165.                         // sum of the RGB values  
  166.                         rgbData[0] += rgb[0] * f2 * f1;  
  167.                         rgbData[1] += rgb[1] * f2 * f1;  
  168.                         rgbData[2] += rgb[2] * f2 * f1;  
  169.                     }  
  170.                 }  
  171.                 ta = 255;  
  172.                 // get Red/green/blue value for sample pixel  
  173.                 tr = (int) (rgbData[0]/rgbCoffeData);  
  174.                 tg = (int) (rgbData[1]/rgbCoffeData);  
  175.                 tb = (int) (rgbData[2]/rgbCoffeData);  
  176.                 index = row * destW + col;  
  177.                 outPixels[index] = (ta << 24) | (clamp(tr) << 16)  
  178.                         | (clamp(tg) << 8) | clamp(tb);  
  179.             }  
  180.         }  
  181.         setRGB(dest, 00, destW, destH, outPixels);  
  182.         return dest;  
  183.     }  
  184.       
  185.     public int clamp(int value) {  
  186.         return value > 255 ? 255 :  
  187.             (value < 0 ? 0 : value);  
  188.     }  
  189.       
  190.     private int[] getPixel(double j, double k, int width, int height,  
  191.             int[] inPixels) {  
  192.         int row = (int) j;  
  193.         int col = (int) k;  
  194.         if (row >= height) {  
  195.             row = height - 1;  
  196.         }  
  197.         if (row < 0) {  
  198.             row = 0;  
  199.         }  
  200.         if (col < 0) {  
  201.             col = 0;  
  202.         }  
  203.         if (col >= width) {  
  204.             col = width - 1;  
  205.         }  
  206.         int index = row * width + col;  
  207.         int[] rgb = new int[3];  
  208.         rgb[0] = (inPixels[index] >> 16) & 0xff;  
  209.         rgb[1] = (inPixels[index] >> 8) & 0xff;  
  210.         rgb[2] = inPixels[index] & 0xff;  
  211.         return rgb;  
  212.     }  
  213.     public BufferedImage createCompatibleDestImage(  
  214.             BufferedImage src, ColorModel dstCM) {  
  215.         if ( dstCM == null )  
  216.             dstCM = src.getColorModel();  
  217.         return new BufferedImage(dstCM,   
  218.                 dstCM.createCompatibleWritableRaster(destW, destH),   
  219.                 dstCM.isAlphaPremultiplied(), null);  
  220.     }  
  221. }  
運行效果:原圖

雙立方插值放大以后:


總結:

基于這里三種方法實(shí)現的雙立方插值以后圖片跟原圖像相比,都有一定模糊

這里時(shí)候可以通過(guò)后續處理實(shí)現圖像銳化與對比度提升即可得到Sharpen版本

當然也可以通過(guò)尋找更加合適的R(x)函數來(lái)實(shí)現雙立方卷積插值過(guò)程時(shí)保留

圖像邊緣與對比度。


圖像放縮之雙立方插值數學(xué)原理

如果已知一個(gè)函數f(x)以及它在x=0,x=1處的導數,那么函數可以在[0,1]之間插值,當函數

表達為三次多項式時(shí)我們稱(chēng)之謂立方插值。一個(gè)三次多項式及其導數:

        f(x) =ax^3 +bx^2 + cx + d

         f’(x)=3ax^2 + 2bx +c

多項式在x=0, x=1處值及其導數值為:

         f(0)= d;

         f(1)= a + b + c + d;

         f’(0)=c

         f’(1)=3a + 2b + c

上述的四個(gè)等式可以等價(jià)的變換為:

         a= 2f(0) – 2f(1) + f’(0) + f’(1)

         b= -3f(0) + 3f(1) – 2f’(0) – f’(1)

         c= f’(0)

         d= f’(1)

假設你有四個(gè)點(diǎn)值p0, p1, p2, p3分別在x=-1, x=0, x=1, x=2, 把值分別指定到f(0), f(1), f’(0),

f’(1)中為:

         f(0)= p1

         f(1)= p2

         f’(0)= (p2 – p0)/2

         f’(1)= (p3-p1)/2

這個(gè)我們的立方插值公式變成:

f(p0,p1,p2,p3, x) = (-1/2p0 + 3/2p1 -3/2p2+ 1/2p3)x^3 + (p0-5/2p1 + 2p2 -1/2d)x^2 + (-1/2p0 +

1/2p2)x + p1

雙立方插值是立方插值在二維空間的表達, 插值公式可以表述為:

G(x, y) = f (f (p00, p01, p02, p03, y), f(p10,p11, p12, p13, y), f(p20, p21, p22, p23, y), f(p30, p31, p32, p33, y), x)

解出其中的16個(gè)參數,即可得帶G(x, y)目標插值點(diǎn)的值。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
常見(jiàn)的Double型數據處理功能擴展
B樣條插值算法
四則運算實(shí)現1(C語(yǔ)言)
203,查找-插值查找
Java:int、char、double與byte相互轉換的程序
C#利用最小二乘法擬合任意次函數曲線(xiàn)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久