日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

图像处理之双线性插值原理和实现

發(fā)布時(shí)間:2024/1/23 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图像处理之双线性插值原理和实现 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

雙線性?xún)?nèi)插原理:

由于圖像數(shù)據(jù)組成為等間距橫向和縱向分布的像素點(diǎn)構(gòu)成,點(diǎn)與點(diǎn)之間間距為一個(gè)像素,那么對(duì)圖像的內(nèi)插,就可以當(dāng)做在每個(gè)像素內(nèi)橫向和縱向均勻增加點(diǎn),每個(gè)點(diǎn)的像素值則由周?chē)膫€(gè)點(diǎn)確定。

假設(shè)當(dāng)點(diǎn)P(x,y)位于P1(x1,y1)、P2(x2,y2)、P3(x3,y3)和P4(x4,y4)四個(gè)點(diǎn)構(gòu)成的矩陣內(nèi),如圖所示:

設(shè)定四個(gè)點(diǎn)像素值為p1、p2、p3、p4,中間P點(diǎn)像素為p,P點(diǎn)像素值通過(guò)P1-P4四個(gè)點(diǎn)像素進(jìn)行擬合。這里采用距離加權(quán)的方法對(duì)內(nèi)插點(diǎn)像素進(jìn)行賦值,即通過(guò)計(jì)算P點(diǎn)坐標(biāo)與其他幾個(gè)點(diǎn)距離來(lái)確定每個(gè)值所占權(quán)重,假設(shè)P點(diǎn)剛好位于矩陣中心,那么p剛好等于p1-p4的均值。

問(wèn)題的核心在于如何計(jì)算P點(diǎn)與周?chē)膫€(gè)點(diǎn)距離,由于P點(diǎn)在矩陣內(nèi),假設(shè)一個(gè)矩陣邊長(zhǎng)為1,x和y與周?chē)c(diǎn)坐標(biāo)的差值都小于等于1,那么可以通過(guò)如下公式來(lái)計(jì)算p點(diǎn)像素值:

p=p1 * [(1 - |x - x1|) * (1 - |y - y1|)] + p2 * [(1 - |x - x2|) * (1 - |y - y2|)] + p3 * [(1 - |x - x3|) * (1 - |y-y3|)] + p4 * [(1 -|x - x4|) * (1 - |y-y4|)]

通過(guò)以上公式,當(dāng)P點(diǎn)位于矩陣中心時(shí),x-x1=0.5,y-y1=0.5,P1點(diǎn)權(quán)重為0.5*0.5=0.25,P2到P4點(diǎn)權(quán)重也一樣是0.25,如果P點(diǎn)剛好位于P1-P4點(diǎn)任意一點(diǎn)上,通過(guò)公式計(jì)算結(jié)果也是p1-p4。

代碼:

double result = p1 * ((1 - Math.abs(x - x1)) * (1 - Math.abs(y - y1)))+ p2 * ((1 - Math.abs(x - x2)) * (1 - Math.abs(y - y2)))+ p3 * ((1 - Math.abs(x - x3)) * (1 - Math.abs(y - y3))) + p4 * ((1 - Math.abs(x - x4)) * (1 - Math.abs(y - y4)));

假設(shè)P1 = 100.25, p2= 120.23, p3 = 103.49, p4 = 105.66,P點(diǎn)坐標(biāo)為(0.2,0.3),計(jì)算結(jié)果p=112.217,四個(gè)點(diǎn)均值為107.4075,P點(diǎn)坐標(biāo)最接近P2點(diǎn),P2點(diǎn)最高,因此P點(diǎn)像素大于均值也更符合預(yù)期。

圖像雙線性?xún)?nèi)插的實(shí)現(xiàn):

  • 讀取原圖數(shù)據(jù),獲取原圖寬高XY,設(shè)定內(nèi)插數(shù)n;
  • 根據(jù)原圖寬高生成目標(biāo)圖像寬高,這里要注意的是,由于雙線性?xún)?nèi)插必須在原圖X和Y方向內(nèi)部都有值得地方進(jìn)行,那么原圖X方向邊緣像素和Y方向最邊緣像素?zé)o法進(jìn)行內(nèi)插,那么目標(biāo)圖像寬和高的計(jì)算應(yīng)該是(X-1)*n,(Y-1)*n;
  • 采用雙線性?xún)?nèi)插對(duì)目標(biāo)圖像像素進(jìn)行賦值,目標(biāo)圖像任意像素(x,y),當(dāng)x/n,y/n必定落在原圖四個(gè)像素組成的區(qū)間內(nèi),(x,y)值,就是原圖中四個(gè)像素值進(jìn)行雙線性?xún)?nèi)插計(jì)算結(jié)果。
  • 測(cè)試代碼:

    import java.awt.image.BufferedImage; import java.io.File;import javax.imageio.ImageIO;/*** 雙線性?xún)?nèi)插* * @author chen**/ public class BilinearInterpolation {public void interpolation(File inputFile, File destFile, int interpolation) throws Exception {BufferedImage inImage = ImageIO.read(inputFile);int width = inImage.getWidth();int height = inImage.getHeight();int destWidth = (width - 1) * interpolation;int destHeight = (height - 1) * interpolation;destFile.createNewFile();BufferedImage destImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);for (int i = 0; i < destWidth; i++) {for (int j = 0; j < destHeight; j++) {double x = (double) i / (double) interpolation;double y = (double) j / (double) interpolation;int x1 = (int) x;int x2 = x1;int x3 = x1 + 1;int x4 = x3;int y1 = (int) y + 1;int y2 = y1 - 1;int y3 = y2;int y4 = y1;int p1 = inImage.getRGB(x1, y1);int p2 = inImage.getRGB(x2, y2);int p3 = inImage.getRGB(x3, y3);int p4 = inImage.getRGB(x4, y4);int R = caculateValue(x, y, x1, x2, x3, x4, y1, y2, y3, y4, (p1 >> 16) & 0xff, (p2 >> 16) & 0xff,(p3 >> 16) & 0xff, (p4 >> 16) & 0xff);int G = caculateValue(x, y, x1, x2, x3, x4, y1, y2, y3, y4, (p1 >> 8) & 0xff, (p2 >> 8) & 0xff,(p3 >> 8) & 0xff, (p4 >> 8) & 0xff);int B = caculateValue(x, y, x1, x2, x3, x4, y1, y2, y3, y4, p1 & 0xff, p2 & 0xff, p3 & 0xff,p4 & 0xff);int rgb = (255 & 0xff) << 24 | (R & 0xff) << 16 | ( G & 0xff) << 8| ( B & 0xff);destImage.setRGB(i, j, rgb);}}ImageIO.write(destImage, "JPG", destFile);}private int caculateValue(double x, double y, int x1, int x2, int x3, int x4, int y1, int y2, int y3, int y4,double p1, double p2, double p3, double p4) {int result = (int) (p1 * ((1 - Math.abs(x - x1)) * (1 - Math.abs(y - y1)))+ p2 * ((1 - Math.abs(x - x2)) * (1 - Math.abs(y - y2)))+ p3 * ((1 - Math.abs(x - x3)) * (1 - Math.abs(y - y3)))+ p4 * ((1 - Math.abs(x - x4)) * (1 - Math.abs(y - y4))));return result;}public static void main(String[] args) throws Exception {new BilinearInterpolation().interpolation(new File("C:\\Users\\admin\\Desktop\\test\\1.png"),new File("C:\\Users\\admin\\Desktop\\test\\11.jpg"), 10);} }

    ?

    總結(jié)

    以上是生活随笔為你收集整理的图像处理之双线性插值原理和实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。