Commit 293b6402 authored by 袁通's avatar 袁通
Browse files

Finished bilinear resize

parent a8923b62
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
"""
LAB2 Task 1_2

Use bilinear interpolation interpolation to interpolate a grey scale image
Enlarge: [461, 461]
Shrink: [205, 205]
"""

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


def linear(x, y1, y2):
    if y2 > y1:
        return y1 + x * (y2-y1)
    else:
        return y2 + (1-x)*(y1-y2)

def small_map(x, range):
    ratio = x/range
    return 0.01 + ratio*(range-0.02)

def bilinear2_11810818(input_file, dim):
    # Load image
    in_image = io.imread(input_file)
    print(in_image)
    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)
    # out_image = np.zeros(dim)

    # Perform Exchange
    for col in range(out_width):
        for row in range(out_height):
            x = small_map(col*((in_width-1)/(out_width-1)), out_width)
            y = small_map(row*((in_height-1)/(out_height-1)), out_height)
            left=linear(y-math.floor(y), in_image[math.floor(x), math.floor(y)], in_image[math.floor(x), math.floor(y)+1])
            right=linear(y-math.floor(y), in_image[math.floor(x)+1, math.floor(y)], in_image[math.floor(x)+1, math.floor(y)+1])
            out_image[col, row] = round(linear(x-math.floor(x), right, left))

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


if __name__ == '__main__':
    bilinear2_11810818("rice.tif", [461, 461])
 No newline at end of file
+208 KiB

File added.

No diff preview for this file type.

+22 −8
Original line number Diff line number Diff line
@@ -8,15 +8,26 @@ Shrink: [205, 205]

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


def linear(x, y1, y2):
    if y2 > y1:
        return y1 + x * (y2-y1)
    else:
        return y2 + (1-x)*(y1-y2)


def bilinear_11810818(input_file, dim):
    # Load image
    in_image = io.imread(input_file)
    print(in_image)
    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)
    # out_image = np.zeros(dim)

    # Perform Exchange
    for col in range(out_width):
@@ -24,16 +35,19 @@ def bilinear_11810818(input_file, dim):
            x = col*((in_width-1)/(out_width-1))
            y = row*((in_height-1)/(out_height-1))
            if x == round(x):
                if y == round(y):
                    out_image[col, row] = in_image[x, y]
                if y == round(y):  # 在点上
                    out_image[col, row] = in_image[round(x), round(y)]
                else:  # 在纵轴上
                    out_image[col, row] = round(linear(y-math.floor(y), in_image[round(x), math.floor(y)], in_image[round(x), math.floor(y)+1]))
            elif y == round(y):  # 在横轴上
                out_image[col, row] = round(linear(x-math.floor(x), in_image[math.floor(x), round(y)], in_image[math.floor(x)+1, round(y)]))
            else:
                    out_image[col, row] =

            elif y == round(y):

            else:

                left=linear(y-math.floor(y), in_image[math.floor(x), math.floor(y)], in_image[math.floor(x), math.floor(y)+1])
                right=linear(y-math.floor(y), in_image[math.floor(x)+1, math.floor(y)], in_image[math.floor(x)+1, math.floor(y)+1])
                out_image[col, row] = round(linear(x-math.floor(x), right, left))
                # out_image[col, row] = 1

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

+208 KiB

File added.

No diff preview for this file type.