Spline3D曲线拟合

时间:2015-05-11 08:18:42

标签: python-2.7 scipy curve-fitting spline

我写了一个程序参考pyeq2,用于40点3D样条拟合来创建768x480 Mask。 但是,将40点改为144点,768x480改为1920x1080,结果变得不顺畅。

如何修改代码或任何其他可以执行此操作的工具。

asciiDataInColumns_3D = '''
60  60  246.09
60  180 245.07
60  300 243.63
60  420 242.64
60  540 240.39
60  660 241.09
60  780 243.78
60  900 244.35
60  1020    249.19
180 60  242.50
180 180 241.37
180 300 242.78
180 420 240.25
180 540 238.87
180 660 249.34
180 780 243.21
180 900 244.21
180 1020    247.70
300 60  242.50
300 180 241.37
300 300 241.37
300 420 240.39
300 540 240.81
300 660 243.92
300 780 240.39
300 900 251.91
300 1020    255.00
420 60  244.64
420 180 244.64
420 300 243.63
420 420 243.49
420 540 242.78
420 660 242.35
420 780 243.78
420 900 243.78
420 1020    246.23
540 60  245.80
540 180 246.23
540 300 245.22
540 420 245.36
540 540 243.78
540 660 242.78
540 780 242.35
540 900 242.50
540 1020    243.06
660 60  245.65
660 180 246.53
660 300 246.38
660 420 245.51
660 540 243.92
660 660 241.93
660 780 240.11
660 900 238.46
660 1020    240.67
780 60  245.51
780 180 247.55
780 300 247.55
780 420 248.00
780 540 244.93
780 660 241.93
780 780 240.39
780 900 238.32
780 1020    238.87
900 60  245.80
900 180 247.70
900 300 246.67
900 420 245.80
900 540 244.64
900 660 243.06
900 780 239.15
900 900 236.69
900 1020    236.15
1020    60  247.70
1020    180 248.00
1020    300 247.11
1020    420 245.51
1020    540 243.21
1020    660 240.67
1020    780 238.05
1020    900 235.62
1020    1020    235.22
1140    60  247.55
1140    180 248.15
1140    300 246.96
1140    420 245.36
1140    540 242.78
1140    660 239.28
1140    780 237.37
1140    900 234.29
1140    1020    234.55
1260    60  247.26
1260    180 246.53
1260    300 245.22
1260    420 242.78
1260    540 241.23
1260    660 237.50
1260    780 236.69
1260    900 234.16
1260    1020    234.95
1380    60  246.53
1380    180 245.07
1380    300 243.06
1380    420 240.67
1380    540 237.91
1380    660 235.35
1380    780 234.69
1380    900 233.89
1380    1020    234.55
1500    60  245.51
1500    180 243.06
1500    300 241.23
1500    420 239.01
1500    540 235.75
1500    660 234.16
1500    780 233.24
1500    900 232.84
1500    1020    233.76
1620    60  244.49
1620    180 242.21
1620    300 239.28
1620    420 236.02
1620    540 233.76
1620    660 232.98
1620    780 231.81
1620    900 232.98
1620    1020    234.16
1740    60  245.94
1740    180 242.92
1740    300 239.42
1740    420 236.29
1740    540 234.03
1740    660 232.58
1740    780 230.52
1740    900 230.14
1740    1020    234.69
1860    60  252.83
1860    180 249.19
1860    300 244.35
1860    420 240.53
1860    540 236.96
1860    660 237.37
1860    780 235.22
1860    900 235.48
1860    1020    236.56
'''

Dimension = 3.0

inSmoothingFactor = 1.0
XOrder = 4
YOrder = 4

#bbox = [0,768,0,480]
bbox = [0,1920,0,1080]

数据转换:

rawData = StringIO.StringIO(asciiDataInColumns_3D).readlines()
dataLists = [[], [], []]

for line in rawData:
    tokenlist = line.split()

    if len(tokenlist) < Dimension:
        continue
    try:
        a = float(tokenlist[0])
        b = float(tokenlist[1])
        c = float(tokenlist[2])
    except:
        continue
    if a > 1.0E300 or a < -1.0E300:
        continue
    if b > 1.0E300 or b < -1.0E300:
        continue
    if c > 1.0E300 or c < -1.0E300:
        continue
    dataLists[0].append(a)
    dataLists[1].append(b)
    dataLists[2].append(c)

arrayLists = numpy.array(dataLists)

Data ARGsort:

indices = numpy.argsort(arrayLists[2])
DependentData = numpy.array(arrayLists[2][indices])
IndependentData = numpy.array([arrayLists[0][indices], arrayLists[1][indices]])
scipy Spline:

mSpline = scipy.interpolate.fitpack2.SmoothBivariateSpline(IndependentData[0], IndependentData[1], DependentData, s=inSmoothingFactor, kx=XOrder, ky=YOrder, bbox=bbox)

tx = mSpline.tck[0]
ty = mSpline.tck[1]
coeff = mSpline.tck[2]

公式:

def mfitting(x_in, y_in):
    global tx, ty, coeff
    nx = len(tx)
    ny = len(ty)
    kx = 4
    ky = 4

    h = [0.0] * 25
    hh = [0.0] * 25
    w_x = [0.0] * 25
    w_y = [0.0] * 25

    kx1 = kx+1
    nkx1 = nx-kx1
    l = kx1
    l1 = l+1

    while x_in >= tx[l1-1] and l != nkx1:
        l = l1
        l1 = l+1

    h[0] = 1.0
    for j in range(1, kx+1):
        for i in range(j):
            hh[i] = h[i]
        h[0] = 0.0
        for i in range(j):
            li = l+i
            lj = li-j
            if tx[li] != tx[lj]:
                f = hh[i] / (tx[li] - tx[lj])
                h[i] = h[i] + f * (tx[li] - x_in)
                h[i+1] = f * (x_in - tx[lj])
            else:
                h[i+1-1] = 0.0

    lx = l-kx1
    for j in range(kx1):
        w_x[j] = h[j]

    ky1 = ky+1
    nky1 = ny-ky1
    l = ky1
    l1 = l+1

    while y_in >= ty[l1-1] and l != nky1:
        l = l1
        l1 = l+1

    h[0] = 1.0
    for j in range(1, ky+1):
        for i in range(j):
            hh[i] = h[i]
        h[0] = 0.0
        for i in range(j):
            li = l+i
            lj = li-j
            if ty[li] != ty[lj]:
                f = hh[i] / (ty[li] - ty[lj])
                h[i] = h[i] + f * (ty[li] - y_in)
                h[i+1] = f * (y_in - ty[lj])
            else:
                h[i+1-1] = 0.0

    ly = l-ky1
    for j in range(ky1):
        w_y[j] = h[j]

    l = lx*nky1
    for i1 in range(kx1):
        h[i1] = w_x[i1]

    l1 = l+ly
    temp = 0.0
    for i1 in range(kx1):
        l2 = l1
        for j1 in range(ky1):
            l2 = l2+1
            temp = temp + coeff[l2-1] * h[i1] * w_y[j1]
        l1 = l1+nky1

    return temp

主:

print "Creating MASK ...."

#im_RGB = Image.new('RGB',(768,480))
im_RGB = Image.new('RGB',(1920,1080))

'''
for i in range(768):
        for j in range(480):
                value = int(Spline_model(i,j))
                im_RGB.putpixel((i,j), (value,value,value))
'''
for i in range(1920):
        for j in range(1080):
                value = int(mfitting(i,j))
                im_RGB.putpixel((i,j), (value,value,value))

im_RGB.save("MASK.png")

print "Creating MASK Done"

0 个答案:

没有答案