2019-08-08 15:59:15 +00:00
|
|
|
"""
|
|
|
|
PyTest's for Digital Image Processing
|
|
|
|
"""
|
2022-08-24 04:48:54 +00:00
|
|
|
import numpy as np
|
2020-07-06 07:44:19 +00:00
|
|
|
from cv2 import COLOR_BGR2GRAY, cvtColor, imread
|
2019-08-08 15:59:15 +00:00
|
|
|
from numpy import array, uint8
|
|
|
|
from PIL import Image
|
|
|
|
|
2020-07-06 07:44:19 +00:00
|
|
|
from digital_image_processing import change_contrast as cc
|
|
|
|
from digital_image_processing import convert_to_negative as cn
|
|
|
|
from digital_image_processing import sepia as sp
|
|
|
|
from digital_image_processing.dithering import burkes as bs
|
|
|
|
from digital_image_processing.edge_detection import canny as canny
|
|
|
|
from digital_image_processing.filters import convolve as conv
|
|
|
|
from digital_image_processing.filters import gaussian_filter as gg
|
2022-08-24 04:48:54 +00:00
|
|
|
from digital_image_processing.filters import local_binary_pattern as lbp
|
2020-07-06 07:44:19 +00:00
|
|
|
from digital_image_processing.filters import median_filter as med
|
|
|
|
from digital_image_processing.filters import sobel_filter as sob
|
|
|
|
from digital_image_processing.resize import resize as rs
|
|
|
|
|
2019-09-13 11:40:14 +00:00
|
|
|
img = imread(r"digital_image_processing/image_data/lena_small.jpg")
|
2019-08-08 15:59:15 +00:00
|
|
|
gray = cvtColor(img, COLOR_BGR2GRAY)
|
|
|
|
|
2020-04-30 09:54:20 +00:00
|
|
|
|
2019-12-09 02:29:01 +00:00
|
|
|
# Test: convert_to_negative()
|
|
|
|
def test_convert_to_negative():
|
|
|
|
negative_img = cn.convert_to_negative(img)
|
|
|
|
# assert negative_img array for at least one True
|
|
|
|
assert negative_img.any()
|
|
|
|
|
|
|
|
|
2019-08-08 15:59:15 +00:00
|
|
|
# Test: change_contrast()
|
|
|
|
def test_change_contrast():
|
2019-09-13 11:40:14 +00:00
|
|
|
with Image.open("digital_image_processing/image_data/lena_small.jpg") as img:
|
2019-08-08 15:59:15 +00:00
|
|
|
# Work around assertion for response
|
|
|
|
assert str(cc.change_contrast(img, 110)).startswith(
|
2019-09-13 11:40:14 +00:00
|
|
|
"<PIL.Image.Image image mode=RGB size=100x100 at"
|
2019-08-08 15:59:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# canny.gen_gaussian_kernel()
|
|
|
|
def test_gen_gaussian_kernel():
|
|
|
|
resp = canny.gen_gaussian_kernel(9, sigma=1.4)
|
|
|
|
# Assert ambiguous array
|
|
|
|
assert resp.all()
|
|
|
|
|
|
|
|
|
|
|
|
# canny.py
|
|
|
|
def test_canny():
|
2019-09-13 11:40:14 +00:00
|
|
|
canny_img = imread("digital_image_processing/image_data/lena_small.jpg", 0)
|
2020-01-18 12:24:33 +00:00
|
|
|
# assert ambiguous array for all == True
|
2019-08-08 15:59:15 +00:00
|
|
|
assert canny_img.all()
|
|
|
|
canny_array = canny.canny(canny_img)
|
|
|
|
# assert canny array for at least one True
|
|
|
|
assert canny_array.any()
|
|
|
|
|
|
|
|
|
|
|
|
# filters/gaussian_filter.py
|
|
|
|
def test_gen_gaussian_kernel_filter():
|
|
|
|
assert gg.gaussian_filter(gray, 5, sigma=0.9).all()
|
|
|
|
|
|
|
|
|
|
|
|
def test_convolve_filter():
|
|
|
|
# laplace diagonals
|
2022-10-12 22:54:20 +00:00
|
|
|
laplace = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]])
|
|
|
|
res = conv.img_convolve(gray, laplace).astype(uint8)
|
2019-08-08 15:59:15 +00:00
|
|
|
assert res.any()
|
|
|
|
|
|
|
|
|
|
|
|
def test_median_filter():
|
|
|
|
assert med.median_filter(gray, 3).any()
|
|
|
|
|
|
|
|
|
|
|
|
def test_sobel_filter():
|
|
|
|
grad, theta = sob.sobel_filter(gray)
|
|
|
|
assert grad.any() and theta.any()
|
2020-04-26 09:59:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_sepia():
|
|
|
|
sepia = sp.make_sepia(img, 20)
|
|
|
|
assert sepia.all()
|
2020-04-30 09:54:20 +00:00
|
|
|
|
|
|
|
|
2020-04-30 20:47:11 +00:00
|
|
|
def test_burkes(file_path: str = "digital_image_processing/image_data/lena_small.jpg"):
|
2020-04-30 09:54:20 +00:00
|
|
|
burkes = bs.Burkes(imread(file_path, 1), 120)
|
|
|
|
burkes.process()
|
|
|
|
assert burkes.output_img.any()
|
2020-05-07 21:47:28 +00:00
|
|
|
|
2020-05-08 05:44:07 +00:00
|
|
|
|
2020-05-07 21:47:28 +00:00
|
|
|
def test_nearest_neighbour(
|
|
|
|
file_path: str = "digital_image_processing/image_data/lena_small.jpg",
|
|
|
|
):
|
|
|
|
nn = rs.NearestNeighbour(imread(file_path, 1), 400, 200)
|
|
|
|
nn.process()
|
|
|
|
assert nn.output.any()
|
2022-08-24 04:48:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_local_binary_pattern():
|
|
|
|
file_path: str = "digital_image_processing/image_data/lena.jpg"
|
|
|
|
|
|
|
|
# Reading the image and converting it to grayscale.
|
|
|
|
image = imread(file_path, 0)
|
|
|
|
|
|
|
|
# Test for get_neighbors_pixel function() return not None
|
|
|
|
x_coordinate = 0
|
|
|
|
y_coordinate = 0
|
|
|
|
center = image[x_coordinate][y_coordinate]
|
|
|
|
|
|
|
|
neighbors_pixels = lbp.get_neighbors_pixel(
|
|
|
|
image, x_coordinate, y_coordinate, center
|
|
|
|
)
|
|
|
|
|
|
|
|
assert neighbors_pixels is not None
|
|
|
|
|
|
|
|
# Test for local_binary_pattern function()
|
|
|
|
# Create a numpy array as the same height and width of read image
|
|
|
|
lbp_image = np.zeros((image.shape[0], image.shape[1]))
|
|
|
|
|
|
|
|
# Iterating through the image and calculating the local binary pattern value
|
|
|
|
# for each pixel.
|
|
|
|
for i in range(0, image.shape[0]):
|
|
|
|
for j in range(0, image.shape[1]):
|
|
|
|
lbp_image[i][j] = lbp.local_binary_value(image, i, j)
|
|
|
|
|
|
|
|
assert lbp_image.any()
|