interp1d type =" linear"给出不精确的结果

时间:2014-01-21 17:14:15

标签: python scipy scaling interpolation

我注意到interp1d可能会给出错误的结果。 至少结果与Gimp和Photoshop中的类比过程的结果不同。 Gimp和Photoshop的结果几乎相同。 也许它与scipy代码中的一些四舍五入有关。

你知道如何消除差异吗?我有理由怀疑图形软件的结果更接近完美。

在以下所有情况下,我只使用1行像素并执行水平缩放。为清晰起见,图像包含更多行

这是非常奇特的原始图像(在#0000FF背景上均匀分开的#000000像素):

未缩放的图片

original image

以下是scipy缩放结果以及gimp和Photoshop的结果。我在上面提到的软件中尝试了许多不同的缩放选项,以及python插值。实际上在这种相当激进的情况下,不同类型的缩放(立方,线性)在scipy中给出了相同的结果。

(您可能需要在浏览器中放大图像。我不想执行额外的缩放以不影响原始结果)

gimp / scipy type =“linear”

gimp-scipy comparison

scipy linear / photoshop / scipy cubic photoshop-scipy comparison

简档 (我裁掉了无意义的纯蓝色长假)

profile plot 1 profile plot 2

在剖面图中,问题不太明显 - 特别是在gimp-scipy的情况下。在位图上,差异非常明显。中间线完全重叠 - 但是我们越往左侧越远,我们就越能看到距离的差异。如何获得更合适的“gimp-like”结果? 对我来说问题不是像素的值,而是暗像素之间的距离。我对线性插值感到满意,不需要立方体,但我把它包含在科学(和比较)的问题中。

我的代码用于插值

import numpy as np
from scipy.interpolate import interp1d
import pygame

def splineDecodeColors2(oldchain,newchain):
   L0,L1 = len(oldchain[0]),len(newchain[0])
   chain0 = np.array(oldchain)
   x0 = np.linspace(0, L0-1, L0)
   chain1 = np.array(newchain)


   for color in xrange(3):
     try:
        f = interp1d(x0, chain0[color], kind= 'linear')
        x  = np.linspace(0, L0-1, L1)
        chain1[color] = f(x).round()
     except ValueError, error:
        print error # possibly len(chain)==1
        chain1[color]=chain0[color]


   return chain1.T



def saveToPNG(img,name):
    name=str(name)
    img = np.array([img,img,img])
    img  = np.rot90(img,3)
    pygame.image.save(pygame.surfarray.make_surface(img),name+".png")



def main():
    a = np.array([[ 0., 0., 0.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 0.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 0.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 0.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 0.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 0.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 0.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 0.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.], [ 0., 0., 255.]]).T

    b= np.zeros([3,592])
    c = splineDecodeColors2(a,b)
    saveToPNG(c,"interlpolated")

if __name__ == '__main__':
    main()

0 个答案:

没有答案