Commit 732780f9 authored by 袁通's avatar 袁通
Browse files

Update files

parent d7121366
Loading
Loading
Loading
Loading
+215 KiB

File added.

No diff preview for this file type.

+73 −0
Original line number Diff line number Diff line
"""
LAB2 Task2

Use Python function “interp2” from packet “scipy” or your own written algorithm to interpolate a grey scale image by using bicubic interpolation.
Enlarge: [461, 461]
Shrink: [205, 205]
"""

import numpy as np
from skimage import io, data
import math
from scipy import interpolate


def small_map2(x, range):
    ratio = (x-1)/(range)
    return 2.01 + ratio*(range-4.02)


def find_value(x, y, z, pix_x, pix_y):
    f = interpolate.interp2d(x, y, z, kind='cubic')
    out = f(pix_x, pix_y)[0]
    print(out)
    print(z)

    return out


def bicubic2_11810818(input_file, dim):
    # Load image
    in_image = io.imread(input_file)
    out_width = dim[0]
    out_height = dim[1]
    in_width = in_image.shape[0]
    in_height = in_image.shape[1]
    out_image = np.zeros(dim, dtype=np.uint8)

    print(in_image)

    # Perform Exchange
    for col in range(out_width):
        for row in range(out_height):
            pix_x = small_map2(col*((in_width-1)/(out_width-1)), out_width)
            pix_y = small_map2(row*((in_height-1)/(out_height-1)), out_height)
            space = np.zeros([4, 4])
            x_range = [math.floor(pix_x) - 1, math.floor(pix_x), math.floor(pix_x) + 1, math.floor(pix_x) + 2]
            y_range = [math.floor(pix_y) - 1, math.floor(pix_y), math.floor(pix_y) + 1, math.floor(pix_y) + 2]

            # print("x: "+str(x_range))
            # print("y: "+str(y_range))
            # print(str(pix_x) + " " + str(pix_y))

            for i in range(4):
                for j in range(4):
                    space[i, j] = in_image[x_range[i], y_range[j]]

            out_image[col, row] = find_value(
                x_range,
                y_range,
                space,
                pix_x,
                pix_y
            )

    # Save Image
    print(out_image)
    io.imsave("enlarged_bicubic3_11810818.tif", out_image)


if __name__ == '__main__':
    # bicubic2_11810818("rice.tif", [205, 205])
    bicubic2_11810818("rice.tif", [461, 461])
+208 KiB

File added.

No diff preview for this file type.

+26 −0
Original line number Diff line number Diff line
import numpy as np
from skimage import io, data
import math
from scipy import interpolate

def interp_test():

    x = [1, 2, 3, 4]
    y = x;
    z = [[106, 110, 154, 188],
         [103,  99, 12, 157],
         [100,  97, 101, 109],
         [102, 104,  96, 105]]


    f1_1 = interpolate.interp1d(x, z[0], kind='cubic')
    f1_2 = interpolate.interp1d(x, z[1], kind='cubic')
    z1 = [f1_1(2.5), f1_2(2.5), f1_1(2.5), f1_2(2.5)]
    f1_3 = interpolate.interp1d(x, z1, kind='cubic')
    print("interpolate.interp1d get: " + str(f1_3(2.5)))

    f2 = interpolate.interp2d(x, y, z, kind='cubic')
    print("interpolate.interp2d get: " + str(f2(2.5, 2.5)[0]))

if __name__ == '__main__':
    interp_test()
 No newline at end of file
+41.3 KiB

File added.

No diff preview for this file type.

Loading