Skip to content

Commit

Permalink
added new algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
shengtanmao committed Nov 6, 2020
1 parent bc7d2f1 commit 5d3b3f2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
53 changes: 53 additions & 0 deletions antialiasing/src/supersampling/uniform_grid_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# scale image with the uniform algorithm

import numpy as np
import math as m

# significantly worse for non-integer factor


def unif_grid_2(array, factor):
"""
Scales an image by a factor using uniform using the grid algorithm in
uniform distribution.
:param array: a numpy array representing the image
:type file: ndarray
:return: a numpy array representing the scaled image
:rtype: ndarray
"""
old_shape = array.shape
new_arr = np.zeros(
(round(old_shape[0] * factor), round(old_shape[1] * factor), 3),
np.uint8)
new_shape = new_arr.shape
for i in range(new_shape[0]):
for j in range(new_shape[1]):
offset = 2 ** (-10)
x = i / factor
x_f = m.floor(x)
x_c = min(m.ceil(x), old_shape[0] - 1)
x_f_d = x_f_d = (x - x_f) ** 2 + offset
x_c_d = (x - x_c) ** 2 + offset

y = j / factor
y_f = m.floor(y)
y_c = min(m.ceil(y), old_shape[1] - 1)
y_f_d = (y - y_f) ** 2 + offset
y_c_d = (y - y_c) ** 2 + offset
weights = [
1 / (m.sqrt(x_f_d + y_f_d)),
1 / (m.sqrt(x_c_d + y_c_d)),
1 / (m.sqrt(x_c_d + y_f_d)),
1 / (m.sqrt(x_f_d + y_c_d)),
]
for k in range(3):

new_arr[i][j][k] = (
array[x_f][y_f][k] * weights[0]
+ array[x_c][y_c][k] * weights[1]
+ array[x_c][y_f][k] * weights[2]
+ array[x_f][y_c][k] * weights[3]
) / sum(weights)
return new_arr
Binary file added antialiasing/tests/images/triangle2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions antialiasing/tests/unit-tests/test_supersampling.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from src.image_handling import imp
from src.supersampling.uniform_grid import unif_grid
from src.supersampling.uniform_grid_2 import unif_grid_2
import pathlib as pl
import numpy as np

Expand Down Expand Up @@ -32,3 +33,29 @@ def test_unit_grid_val():
for k in range(shape[2]):
test_val = new_arr[i][j][k]
assert test_val < 256 and test_val >= 0


def test_unif_grid_2_shape():
factor = 1.5
new_arr = unif_grid_2(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_unit_grid_type():
new_arr = unif_grid_2(array, 1.5)
assert new_arr.dtype == np.uint8


def test_unit_grid_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

0 comments on commit 5d3b3f2

Please sign in to comment.