zoukankan      html  css  js  c++  java
  • 插值法(最邻近,双线性,双三次),图像放缩 python实现

    插值法的第一次都是相同的,计算新图的坐标点对应原图中哪个坐标点来填充,计算公式为:
    srcX = dstX* (srcWidth/dstWidth)
    srcY = dstY * (srcHeight/dstHeight)
    srcWidth/dstWidth和srcHeight/dstHeight分别表示宽和高的放缩比。
    那么问题来了,通过这个公式算出来的srcX,scrY有可能是小数,但是坐标点是不存在小数的,都是整数,得想办法把它转换成整数才行。
    不同的插值法的区别就体现在srcX,scrY是小数时,怎么变成整数去取原图片中的像素值。
    最邻近:看名字就很直白,四舍五入选取最接近的整数。这样的做法就会导致像素的变化不连续,在图像中的体现就是会有锯齿。
    双线性插值:双线性就是利用与坐标轴平行的两条直线去把小数坐标分解到相邻的四个整数坐标点的和,权重为距离。

    例如P点是小数坐标,Q是相邻的四个整数坐标
    双三次插值:与双线性插值类似,只不过用了相邻的16个点。但是需要注意的是,前面两种方法能回保证两个方向的坐标权重和为1,但是双三次插值不能保证这点,所以又可能去出现像素值越界的情况,需要截断。
    直接看代码,清晰明了。实验结果图:



    from PIL import Image
    import matplotlib.pyplot as plt
    import numpy as np 
    import math
    
    def NN_interpolation(img,dstH,dstW):
        scrH,scrW,_=img.shape
        retimg=np.zeros((dstH,dstW,3),dtype=np.uint8)
        for i in range(dstH):
            for j in range(dstW):
                scrx=round((i+1)*(scrH/dstH))
                scry=round((j+1)*(scrW/dstW))
                retimg[i,j]=img[scrx-1,scry-1]
        return retimg
    
    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):
            for j in range(dstW):
                scrx=(i+1)*(scrH/dstH)-1
                scry=(j+1)*(scrW/dstW)-1
                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
    
    def BiBubic(x):
        x=abs(x)
        if x<=1:
            return 1-2*(x**2)+(x**3)
        elif x<2:
            return 4-8*x+5*(x**2)-(x**3)
        else:
            return 0
    
    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+ii<0 or y+jj<0 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='C:/Users/11358/Pictures/Camera Roll/1.png'
    image=np.array(Image.open(im_path))
    
    image1=NN_interpolation(image,image.shape[0]*2,image.shape[1]*2)
    image1=Image.fromarray(image1.astype('uint8')).convert('RGB')
    image1.save('C:/Users/11358/Pictures/Camera Roll/2.png')
    
    image2=BiLinear_interpolation(image,image.shape[0]*2,image.shape[1]*2)
    image2=Image.fromarray(image2.astype('uint8')).convert('RGB')
    image2.save('C:/Users/11358/Pictures/Camera Roll/3.png')
    
    image3=BiCubic_interpolation(image,image.shape[0]*2,image.shape[1]*2)
    image3=Image.fromarray(image3.astype('uint8')).convert('RGB')
    image3.save('C:/Users/11358/Pictures/Camera Roll/4.png')
    
  • 相关阅读:
    周末之个人杂想(十三)
    PowerTip of the DaySorting Multiple Properties
    PowerTip of the DayCreate Remoting Solutions
    PowerTip of the DayAdd Help to Your Functions
    PowerTip of the DayAcessing Function Parameters by Type
    PowerTip of the DayReplace Text in Files
    PowerTip of the DayAdding Extra Information
    PowerTip of the DayPrinting Results
    Win7下IIS 7.5配置SSAS(2008)远程访问
    PowerTip of the DayOpening Current Folder in Explorer
  • 原文地址:https://www.cnblogs.com/super-JJboom/p/9993431.html
Copyright © 2011-2022 走看看