~/image/e4 で演習
NEAREST ... 最近隣内挿法
BILINEAR ... 共一次内挿法
BICUBIC ... 3次畳み込み内挿法
が用意されている。
# e401.py ... expand image
import Image
img = Image.open('s401.ppm')
width, height = img.size
width = width * 5
height = height * 5
imgx = img.resize((width,height), Image.NEAREST)
imgx.save('s401-1.ppm')
BILINEAR -> e401-2.ppm
BICUBIC -> e401-3.ppm
ANTIALIAS -> e401-4.ppm
にそれぞれ画像を保存して比較してみよう

u = ax + by + c v = dx + ey + f
(x, y) = ( 10, 50), (380, 100), (130, 280) (u, v) = ( 0, 0), (400, 100), (100, 300)
|u1 v1| |x1 y1 1| |a d| |u2 v2| = |x2 y2 1| |b e| |u3 v3| |x3 y3 1| |c f|
# e403.py ... affine coefficients
from Matrix import *
from LinearAlgebra import *
U = Matrix([[ 0, 0],
[400, 100],
[100, 300]])
X = Matrix([[10, 50, 1],
[380, 100, 1],
[130, 280, 1]])
C = solve_linear_equations(X, U)
print 'a =', C[0][0]
print 'b =', C[1][0]
print 'c =', C[2][0]
print 'd =', C[0][1]
print 'e =', C[1][1]
print 'f =', C[2][1]
# e404.py ... test affine transform
import Image
img = Image.open('s404.ppm')
imgx = img.transform((400,300), Image.AFFINE,
(a, b, c, d, e, f),
Image.NEAREST)
imgx.save('s404-1.ppm')# e405.py
from math import sqrt
import Gnuplot
def linreg(X, Y):
assert len(X) == len(Y)
N = len(X)
Sx = Sy = Sxx = Syy = Sxy = 0.0
for x, y in zip(X, Y):
Sx = Sx + x
Sy = Sy + y
Sxx = Sxx + x*x
Syy = Syy + y*y
Sxy = Sxy + x*y
det = Sxx * N - Sx * Sx
a = (Sxy * N - Sy * Sx)/det
b = (Sxx * Sy - Sx * Sxy)/det
return a, b
def plot(X, Y, a, b):
g = Gnuplot.Gnuplot()
P = Gnuplot.Data(zip(X, Y))
L = Gnuplot.Func('(%f)*x + (%f)' % (a, b), with='line')
g.plot(P, L)
raw_input('Hit return to terminate.')
if __name__=='__main__':
#testing
X=[1,2,3,4,5]
Y=[357.14,53.57,48.78,10.48,-168.8]
a, b = linreg(X, Y)
plot(X, Y, a, b)