图像处理之双线性插值原理和实现
雙線性?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):
測(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)題。
- 上一篇: 匀光匀色--直方图匹配算法实现与应用
- 下一篇: 图像处理之添加图像水印