Skip to content

Commit

Permalink
added tests and image scoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Shengtan Mao authored and Shengtan Mao committed Dec 20, 2020
1 parent 090e33d commit 93888c9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
27 changes: 25 additions & 2 deletions antialiasing/src/image_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def imp(file):
:return: a three dimesional numpy array representing the image
:rtype: ndarray
"""
return np.asarray(im.open(file), dtype=np.intc)
return np.asarray(im.open(file), dtype=np.int16)


def exp(array, file):
Expand All @@ -26,4 +26,27 @@ def exp(array, file):
:param file: file path
:type file: string
"""
im.fromarray(array).save(file)
im.fromarray(array.astype(np.uint8)).save(file)


def score(old, new):
"""
Scores two images of same dimensions on similarity from 0 to 255.
The smaller the score the more similar they are.
:param old: a three dimesional numpy array representing the image
:type array: ndarray
:param new: a three dimesional numpy array representing the image
:type array: ndarray
:return: score from 0 to 255
:rtype: double
"""
assert(old.shape[0] == new.shape[0] and old.shape[1] == new.shape[1])
sum = 0
for i in range(new.shape[0]):
for j in range(new.shape[1]):
for k in range(3):
sum += abs(new[i][j][k] - old[i][j][k])
return sum/(new.shape[0]*new.shape[1]*3)
8 changes: 4 additions & 4 deletions antialiasing/src/supersampling/bicubic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def bicub_val(m):
"""
computation for bicubic interpolation
Compututes derivatives to calculate coefficients.
:m: 4 by 4 matrix
:type file: ndarray
Expand Down Expand Up @@ -38,7 +38,7 @@ def bicub_val(m):

def compute_coeff(col):
"""
computes coefficients for bicubic interpolation
Computes coefficients for bicubic interpolation.
:col: a numpy array representing a single color
:type file: ndarray
Expand All @@ -58,7 +58,7 @@ def compute_coeff(col):

def bicub_inter(array, factor):
"""
Scales an image by a factor using bicubic interpolation
Scales an image by a factor using bicubic interpolation.
:param array: a numpy array representing the image
:type file: ndarray
Expand All @@ -69,7 +69,7 @@ def bicub_inter(array, factor):
old_shape = array.shape
new_arr = np.zeros(
(round(old_shape[0] * factor), round(old_shape[1] * factor), 3),
np.uint8)
np.int16)
new_shape = new_arr.shape
coeffs = []
coeffs.append(compute_coeff(array[:, :, 0]))
Expand Down
2 changes: 1 addition & 1 deletion antialiasing/src/supersampling/uniform_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def unif_grid(array, factor):
"""
old_shape = array.shape
new_arr = np.zeros(
(int(old_shape[0] * factor), int(old_shape[1] * factor), 3), np.uint8
(int(old_shape[0] * factor), int(old_shape[1] * factor), 3), np.int16
)
new_shape = new_arr.shape
for i in range(new_shape[0]):
Expand Down
2 changes: 1 addition & 1 deletion antialiasing/src/supersampling/uniform_grid_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def unif_grid_2(array, factor):
old_shape = array.shape
new_arr = np.zeros(
(round(old_shape[0] * factor), round(old_shape[1] * factor), 3),
np.uint8)
np.int16)
new_shape = new_arr.shape
for i in range(new_shape[0]):
for j in range(new_shape[1]):
Expand Down
9 changes: 9 additions & 0 deletions antialiasing/tests/unit-tests/test_image_handling.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from src.image_handling import imp
from src.image_handling import score
from src.supersampling.uniform_grid import unif_grid
import pathlib as pl
import numpy as np

Expand Down Expand Up @@ -28,3 +30,10 @@ def test_valid_val():
for k in range(shape[2]):
test_val = array[i][j][k]
assert test_val < 256 and test_val >= 0


def test_valid_score():
small = unif_grid(array, 0.5)
modified = unif_grid(small, 2)
s = score(array, modified)
assert s >= 0 and s <= 255
32 changes: 29 additions & 3 deletions antialiasing/tests/unit-tests/test_supersampling.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from src.image_handling import imp
from src.supersampling.uniform_grid import unif_grid
from src.supersampling.uniform_grid_2 import unif_grid_2
from src.supersampling.bicubic import bicub_inter
import pathlib as pl
import numpy as np

images_path = pl.Path("./antialiasing/tests/images")
array = imp(images_path / "triangle.jpeg")
shape = array.shape


def test_unif_grid_shape():
factor = 1.5
new_arr = unif_grid(array, factor)
Expand All @@ -22,7 +22,7 @@ def test_unif_grid_shape():

def test_unit_grid_type():
new_arr = unif_grid(array, 1.5)
assert new_arr.dtype == np.uint8
assert new_arr.dtype == np.int16


def test_unit_grid_val():
Expand All @@ -48,12 +48,38 @@ def test_unif_grid_2_shape():

def test_unit_grid_2_type():
new_arr = unif_grid_2(array, 1.5)
assert new_arr.dtype == np.uint8
assert new_arr.dtype == np.int16


def test_unit_grid_2_val():
new_arr = unif_grid_2(array, 1.5)
new_shape = new_arr.shape
for i in range(new_shape[0]):
for j in range(new_shape[1]):
for k in range(shape[2]):
test_val = new_arr[i][j][k]
assert test_val < 256 and test_val >= 0


def test_bicubic_shape():
factor = 1.5
new_arr = bicub_inter(array, factor)
new_shape = new_arr.shape
assert (
int(shape[0] * factor) == new_shape[0]
and int(shape[1] * factor) == new_shape[1]
and new_shape[2] == 3
)


def test_bicubic_type():
new_arr = bicub_inter(array, 1.5)
assert new_arr.dtype == np.int16


def test_bicubic_type():
new_arr = bicub_inter(array, 1.5)
new_shape = new_arr.shape
for i in range(new_shape[0]):
for j in range(new_shape[1]):
for k in range(shape[2]):
Expand Down

0 comments on commit 93888c9

Please sign in to comment.