宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

本文目录一览:

  • 1、如何通过python实现三次样条插值
  • 2、双线性插值法原理 python实现
  • 3、图像双三次插值算法原理及python实现
  • 4、求c语言写的双三次插值函数

如何通过python实现三次样条插值

spline函数可以实现三次样条插值 x = 0:10; y = sin(x); xx = 0:.25:10; yy = spline(x,y,xx); plot(x,y,’o’,xx,yy) 另外fnplt csapi这两个函数也是三次样条插值函数,具体你可以help一下!

双三次插值法代码python(三次插值函数)(含python实现)-风君子博客

双线性插值法原理 python实现

码字不易,如果此文对你有所帮助,请帮忙点赞,感谢!

一. 双线性插值法原理:

        ① 何为线性插值?

        插值就是在两个数之间插入一个数,线性插值原理图如下:

        ② 各种插值法:

        插值法的第一步都是相同的,计算目标图(dstImage)的坐标点对应原图(srcImage)中哪个坐标点来填充,计算公式为:

        srcX = dstX * (srcWidth/dstWidth)

        srcY = dstY * (srcHeight/dstHeight)

        (dstX,dstY)表示目标图像的某个坐标点,(srcX,srcY)表示与之对应的原图像的坐标点。srcWidth/dstWidth 和 srcHeight/dstHeight 分别表示宽和高的放缩比。

        那么问题来了,通过这个公式算出来的 srcX, scrY 有可能是小数,但是原图像坐标点是不存在小数的,都是整数,得想办法把它转换成整数才行。

        不同插值法的区别就体现在 srcX, scrY 是小数时,怎么将其变成整数去取原图像中的像素值。

        最近邻插值(Nearest-neighborInterpolation):看名字就很直白,四舍五入选取最接近的整数。这样的做法会导致像素变化不连续,在目标图像中产生锯齿边缘。

        双线性插值(Bilinear Interpolation):双线性就是利用与坐标轴平行的两条直线去把小数坐标分解到相邻的四个整数坐标点。权重与距离成反比。

        双三次插值(Bicubic Interpolation):与双线性插值类似,只不过用了相邻的16个点。但是需要注意的是,前面两种方法能保证两个方向的坐标权重和为1,但是双三次插值不能保证这点,所以可能出现像素值越界的情况,需要截断。

        ③ 双线性插值算法原理

        假如我们想得到未知函数 f 在点 P = (x, y) 的值,假设我们已知函数 f 在 Q11 = (x1, y1)、Q12 = (x1, y2), Q21 = (x2, y1) 以及 Q22 = (x2, y2) 四个点的值。最常见的情况,f就是一个像素点的像素值。首先在 x 方向进行线性插值,然后再在 y 方向上进行线性插值,最终得到双线性插值的结果。

    ④ 举例说明

二. python实现灰度图像双线性插值算法:

灰度图像双线性插值放大缩小

import numpy as np

import math

import cv2

def double_linear(input_signal, zoom_multiples):

    ”’

    双线性插值

    :param input_signal: 输入图像

    :param zoom_multiples: 放大倍数

    :return: 双线性插值后的图像

    ”’

    input_signal_cp = np.copy(input_signal)  # 输入图像的副本

    input_row, input_col = input_signal_cp.shape # 输入图像的尺寸(行、列)

    # 输出图像的尺寸

    output_row = int(input_row * zoom_multiples)

    output_col = int(input_col * zoom_multiples)

    output_signal = np.zeros((output_row, output_col)) # 输出图片

    for i in range(output_row):

        for j in range(output_col):

            # 输出图片中坐标 (i,j)对应至输入图片中的最近的四个点点(x1,y1)(x2, y2),(x3, y3),(x4,y4)的均值

            temp_x = i / output_row * input_row

            temp_y = j / output_col * input_col

            x1 = int(temp_x)

            y1 = int(temp_y)

            x2 = x1

            y2 = y1 + 1

            x3 = x1 + 1

            y3 = y1

            x4 = x1 + 1

            y4 = y1 + 1

            u = temp_x – x1

            v = temp_y – y1

            # 防止越界

            if x4 = input_row:

                x4 = input_row – 1

                x2 = x4

                x1 = x4 – 1

                x3 = x4 – 1

            if y4 = input_col:

                y4 = input_col – 1

                y3 = y4

                y1 = y4 – 1

                y2 = y4 – 1

            # 插值

            output_signal[i, j] = (1-u)*(1-v)*int(input_signal_cp[x1, y1]) + (1-u)*v*int(input_signal_cp[x2, y2]) + u*(1-v)*int(input_signal_cp[x3, y3]) + u*v*int(input_signal_cp[x4, y4])

    return output_signal

# Read image

img = cv2.imread(“../paojie_g.jpg”,0).astype(np.float)

out = double_linear(img,2).astype(np.uint8)

# Save result

cv2.imshow(“result”, out)

cv2.imwrite(“out.jpg”, out)

cv2.waitKey(0)

cv2.destroyAllWindows()

三. 灰度图像双线性插值实验结果:

四. 彩色图像双线性插值python实现

def BiLinear_interpolation(img,dstH,dstW):

    scrH,scrW,_=img.shape

    img=np.pad(img,((0,1),(0,1),(0,0)),’constant’)

    retimg=np.zeros((dstH,dstW,3),dtype=np.uint8)

    for i in range(dstH-1):

        for j in range(dstW-1):

            scrx=(i+1)*(scrH/dstH)

            scry=(j+1)*(scrW/dstW)

            x=math.floor(scrx)

            y=math.floor(scry)

            u=scrx-x

            v=scry-y

            retimg[i,j]=(1-u)*(1-v)*img[x,y]+u*(1-v)*img[x+1,y]+(1-u)*v*img[x,y+1]+u*v*img[x+1,y+1]

    return retimg

im_path=’../paojie.jpg’

image=np.array(Image.open(im_path))

image2=BiLinear_interpolation(image,image.shape[0]*2,image.shape[1]*2)

image2=Image.fromarray(image2.astype(‘uint8’)).convert(‘RGB’)

image2.save(‘3.png’)

五. 彩色图像双线性插值实验结果:

六. 最近邻插值算法和双三次插值算法可参考:

        ① 最近邻插值算法:

        

        ② 双三次插值算法:

        

七. 参考内容:

        

        

图像双三次插值算法原理及python实现

一. 图像双三次插值算法原理:

        假设源图像 A 大小为 m*n ,缩放后的目标图像 B 的大小为 M*N 。那么根据比例我们可以得到 B(X,Y) 在 A 上的对应坐标为 A(x,y) = A( X*(m/M), Y*(n/N) ) 。在双线性插值法中,我们选取 A(x,y) 的最近四个点。而在双立方插值法中,我们选取的是最近的16个像素点作为计算目标图像 B(X,Y) 处像素值的参数。如图所示:

        如图所示 P 点就是目标图像 B 在 (X,Y) 处对应于源图像中的位置,P 的坐标位置会出现小数部分,所以我们假设 P 的坐标为 P(x+u,y+v),其中 x,y 分别表示整数部分,u,v 分别表示小数部分。那么我们就可以得到如图所示的最近 16 个像素的位置,在这里用 a(i,j)(i,j=0,1,2,3) 来表示。 

        双立方插值的目的就是通过找到一种关系,或者说系数,可以把这 16 个像素对于 P 处像素值的影响因子找出来,从而根据这个影响因子来获得目标图像对应点的像素值,达到图像缩放的目的。 

        BiCubic基函数形式如下:

二. python实现双三次插值算法

from PIL import Image

import numpy as np

import math

# 产生16个像素点不同的权重

def BiBubic(x):

    x=abs(x)

    if x=1:

        return 1-2*(x**2)+(x**3)

    elif x2:

        return 4-8*x+5*(x**2)-(x**3)

    else:

        return 0

# 双三次插值算法

# dstH为目标图像的高,dstW为目标图像的宽

def BiCubic_interpolation(img,dstH,dstW):

    scrH,scrW,_=img.shape

    #img=np.pad(img,((1,3),(1,3),(0,0)),’constant’)

    retimg=np.zeros((dstH,dstW,3),dtype=np.uint8)

    for i in range(dstH):

        for j in range(dstW):

            scrx=i*(scrH/dstH)

            scry=j*(scrW/dstW)

            x=math.floor(scrx)

            y=math.floor(scry)

            u=scrx-x

            v=scry-y

            tmp=0

            for ii in range(-1,2):

                for jj in range(-1,2):

                    if x+ii0 or y+jj0 or x+ii=scrH or y+jj=scrW:

                        continue

                    tmp+=img[x+ii,y+jj]*BiBubic(ii-u)*BiBubic(jj-v)

            retimg[i,j]=np.clip(tmp,0,255)

    return retimg

im_path=’../paojie.jpg’

image=np.array(Image.open(im_path))

image2=BiCubic_interpolation(image,image.shape[0]*2,image.shape[1]*2)

image2=Image.fromarray(image2.astype(‘uint8’)).convert(‘RGB’)

image2.save(‘BiCubic_interpolation.jpg’)

三. 实验结果:

四. 参考内容:

        

        

求c语言写的双三次插值函数

void SPL(int n, double *x, double *y, int ni, double *xi, double *yi); 是你所要。

已知 n 个点 x,y; x 必须已按顺序排好。要插值 ni 点,横坐标 xi[], 输出 yi[]。

程序里用double 型,保证计算精度。

SPL调用现成的程序。

现成的程序很多。端点处理方法不同,结果会有不同。想同matlab比较,你需 尝试 调用 spline()函数 时,令 end1 为 1, 设 slope1 的值,令 end2 为 1 设 slope2 的值。

#include stdio.h

#include math.h

int spline (int n, int end1, int end2,

double slope1, double slope2,

double x[], double y[],

double b[], double c[], double d[],

int *iflag)

{

int nm1, ib, i, ascend;

double t;

nm1 = n – 1;

*iflag = 0;

if (n 2)

{ /* no possible interpolation */

*iflag = 1;

goto LeaveSpline;

}

ascend = 1;

for (i = 1; i n; ++i) if (x[i] = x[i-1]) ascend = 0;

if (!ascend)

{

*iflag = 2;

goto LeaveSpline;

}

if (n = 3)

{

d[0] = x[1] – x[0];

c[1] = (y[1] – y[0]) / d[0];

for (i = 1; i nm1; ++i)

{

d[i] = x[i+1] – x[i];

b[i] = 2.0 * (d[i-1] + d[i]);

c[i+1] = (y[i+1] – y[i]) / d[i];

c[i] = c[i+1] – c[i];

}

/* —- Default End conditions */

b[0] = -d[0];

b[nm1] = -d[n-2];

c[0] = 0.0;

c[nm1] = 0.0;

if (n != 3)

{

c[0] = c[2] / (x[3] – x[1]) – c[1] / (x[2] – x[0]);

c[nm1] = c[n-2] / (x[nm1] – x[n-3]) – c[n-3] / (x[n-2] – x[n-4]);

c[0] = c[0] * d[0] * d[0] / (x[3] – x[0]);

c[nm1] = -c[nm1] * d[n-2] * d[n-2] / (x[nm1] – x[n-4]);

}

/* Alternative end conditions — known slopes */

if (end1 == 1)

{

b[0] = 2.0 * (x[1] – x[0]);

c[0] = (y[1] – y[0]) / (x[1] – x[0]) – slope1;

}

if (end2 == 1)

{

b[nm1] = 2.0 * (x[nm1] – x[n-2]);

c[nm1] = slope2 – (y[nm1] – y[n-2]) / (x[nm1] – x[n-2]);

}

/* Forward elimination */

for (i = 1; i n; ++i)

{

t = d[i-1] / b[i-1];

b[i] = b[i] – t * d[i-1];

c[i] = c[i] – t * c[i-1];

}

/* Back substitution */

c[nm1] = c[nm1] / b[nm1];

for (ib = 0; ib nm1; ++ib)

{

i = n – ib – 2;

c[i] = (c[i] – d[i] * c[i+1]) / b[i];

}

b[nm1] = (y[nm1] – y[n-2]) / d[n-2] + d[n-2] * (c[n-2] + 2.0 * c[nm1]);

for (i = 0; i nm1; ++i)

{

b[i] = (y[i+1] – y[i]) / d[i] – d[i] * (c[i+1] + 2.0 * c[i]);

d[i] = (c[i+1] – c[i]) / d[i];

c[i] = 3.0 * c[i];

}

c[nm1] = 3.0 * c[nm1];

d[nm1] = d[n-2];

}

else

{

b[0] = (y[1] – y[0]) / (x[1] – x[0]);

c[0] = 0.0;

d[0] = 0.0;

b[1] = b[0];

c[1] = 0.0;

d[1] = 0.0;

}

LeaveSpline:

return 0;

}

double seval (int n, double u,

double x[], double y[],

double b[], double c[], double d[],

int *last)

{

int i, j, k;

double w;

i = *last;

if (i = n-1) i = 0;

if (i 0) i = 0;

if ((x[i] u) || (x[i+1] u))

{

i = 0;

j = n;

do

{

k = (i + j) / 2;

if (u x[k]) j = k;

if (u = x[k]) i = k;

}

while (j i+1);

}

*last = i;

w = u – x[i];

w = y[i] + w * (b[i] + w * (c[i] + w * d[i]));

return (w);

}

void SPL(int n, double *x, double *y, int ni, double *xi, double *yi)

{

double *b, *c, *d;

int iflag,last,i;

b = (double *) malloc(sizeof(double) * n);

c = (double *)malloc(sizeof(double) * n);

d = (double *)malloc(sizeof(double) * n);

if (!d) { printf(“no enough memory for b,c,dn”);}

else {

spline (n,0,0,0,0,x,y,b,c,d,iflag);

if (iflag==0) printf(“I got coef b,c,d nown”); else printf(“x not in order or other errorn”);

for (i=0;ini;i++) yi[i] = seval(ni,xi[i],x,y,b,c,d,last);

free(b);free(c);free(d);

};

}

main(){

double x[6]={0.,1.,2.,3.,4.,5};

double y[6]={0.,0.5,2.0,1.6,0.5,0.0};

double u[8]={0.5,1,1.5,2,2.5,3,3.5,4};

double s[8];

int i;

SPL(6, x,y, 8, u, s);

for (i=0;i8;i++) printf(“%lf %lf n”,u[i],s[i]);

return 0;

}