Dr. Cho’s Website
Course Materials

Zooming, shrinking, and grayscaling of images

Dr. Huidae Cho
Institute for Environmental and Spatial Analysis...University of North Georgia

1   Zooming

1.1   Nearest neighbor interpolation

nearest_neighbor_interpolate_mycalc.py (Study this code!)

nearest-neighbor-interpolation

nearest-neighbor-interpolation-done

1.2   Nearest neighbor interpolation algorithm

for i in range(0, new_size):
    for j in range(0, new_size):
        row = int(i / scale)
        col = int(j / scale)
        new_img[i,j] = img[row,col]

1.3   Pixel replication

Special case of nearest neighbor interpolation

pixel-replication

pixel-replication-done

1.4   Bilinear interpolation

bilinear-interpolation

bilinear_interpolate_mycalc.py

Algorithm

One/two/four neighbor pixels depending on the location

Interpolate green first and blue next

1.5   Bilinear interpolation algorithm

for i in range(0, new_size):
    for j in range(0, new_size):
        x = j / float(scale)
        y = i / float(scale)
        if ((x <= 0.5 and (y <= 0.5 or y >= size-1.5)) or
            (x >= size-1.5 and (y <= 0.5 or y >= size-1.5))):
            # corner: one neighbor
            new_img[i,j] = get_corner_neighbor(img, x, y)
        elif x <= 0.5 or x >= size-1.5:
            # vertical borders: two neighbors
            new_img[i,j] = get_vertical_border_neighbor(img, x, y)
        elif y <= 0.5 or y >= size-1.5:
            # horizontal borders: two neighbors
            new_img[i,j] = get_horizontal_border_neighbor(img, x, y)
        else:
            # inner pixels: four neighbors
            new_img[i,j] = get_inner_neighbor(img, x, y)

1.6   Bilinear interpolation get_corner_neighbor

def get_corner_neighbor(img, x, y):
    return img[int(y),int(x)]

1.7   Bilinear interpolation get_vertical_border_neighbor

def get_vertical_border_neighbor(img, x, y):
    x1 = x2 = int(x)
    y1 = int(y)
    if y-y1 < 0.5:
        y2 = y1 - 1
    else:
        y2 = y1 + 1

    return img[y1,x1] + (img[y2,x2]-img[y1,x1]) / (y2-y1) * (y-y1)

1.8   Bilinear interpolation get_horizontal_border_neighbor

def get_horizontal_border_neighbor(img, x, y):
    x1 = int(x)
    if x-x1 < 0.5:
        x2 = x1 - 1
    else:
        x2 = x1 + 1
    y1 = y2 = int(y)

    return img[y1,x1] + (img[y2,x2]-img[y1,x1]) / (x2-x1) * (x-x1)

1.9   Bilinear interpolation get_inner_neighbor

def get_inner_neighbor(img, x, y):
    x1 = int(x)
    if x-x1 < 0.5:
        x2 = x1 - 1
    else:
        x2 = x1 + 1
    y1 = int(y)
    if y-y1 < 0.5:
        y2 = y1 - 1
    else:
        y2 = y1 + 1
    f11 = img[y1,x1]
    f12 = img[y1,x2]
    f21 = img[y2,x1]
    f22 = img[y2,x2]

    a1 = (f21-f11) / (x2-x1)
    b1 = (f11*x2-f21*x1) / (x2-x1)
    a2 = (f22-f12) / (x2-x1)
    b2 = (f12*x2-f22*x1) / (x2-x1)
    a = (a1*y2-a2*y1) / (y2-y1)
    b = (b2-b1) / (y2-y1)
    c = (a2-a1) / (y2-y1)
    d = (b1*y2-b2*y1) / (y2-y1)

    return a*x+b*y+c*x*y+d

1.10   Compare nearest neighbor and bilinear interpolation methods

Which one is more pixelated?

Which one is brighter?

Why?

2   Shrinking

2.1   Row-column deletion

Half spatial resolution: Sample every other row and column.

shrink_delete_mycalc.py

shrink_delete_scale_mycalc.py

2.2   Row-column deletion algorithm

for i in range(0, new_size):
    for j in range(0, new_size):
        new_img[i,j] = img[2*i,2*j]

2.3   Mean

Half spatial resolution: Take the mean of four pixels.

shrink_mean_mycalc.py

compare_delete_mean_mycalc.py

2.4   Mean algorithm

for i in range(0, new_size):
    for j in range(0, new_size):
        new_img[i,j] = np.mean(img[2*i:2*i+2, 2*j:2*j+2])

3   Grayscaling

$k=8 \rightarrow k=1$ ($L=256 \rightarrow L=2$)

grayscale_mycalc.py

3.1   Grayscaling algorithm

for c in range(0, new_L):
    new_img[(img >= c*L/new_L) & (img < (c+1)*L/new_L)] = c

4   Homework: Bilinear interpolation

Convert the following 2-by-2 image (thick black grids) to a 3-by-3 image (red grids) using the bilinear interpolation method. Perform manual calculations and show me your final image in a matrix form.

bilinear-interpolation-homework

Show your work for full credits!