Skip to content

Commit 24baa2b

Browse files
authored
Update test_digital_image_processing.py
1 parent 6e7bd44 commit 24baa2b

1 file changed

Lines changed: 44 additions & 57 deletions

File tree

digital_image_processing/test_digital_image_processing.py

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
PyTest's for Digital Image Processing
33
"""
44

5+
import os
56
import numpy as np
67
from cv2 import COLOR_BGR2GRAY, cvtColor, imread
78
from numpy import array, uint8
@@ -23,112 +24,98 @@
2324
gray = cvtColor(img, COLOR_BGR2GRAY)
2425

2526

26-
# Test: convert_to_negative()
2727
def test_convert_to_negative():
28+
"""Test negative image conversion."""
2829
negative_img = cn.convert_to_negative(img)
29-
# assert negative_img array for at least one True
30+
# Verify output contains at least one non-zero value
3031
assert negative_img.any()
3132

3233

33-
# Test: change_contrast()
3434
def test_change_contrast():
35-
with Image.open("digital_image_processing/image_data/lena_small.jpg") as img:
36-
# Work around assertion for response
37-
assert str(cc.change_contrast(img, 110)).startswith(
35+
"""Test contrast adjustment functionality."""
36+
with Image.open("digital_image_processing/image_data/lena_small.jpg") as img_pil:
37+
# Verify returns a PIL Image object
38+
assert str(cc.change_contrast(img_pil, 110)).startswith(
3839
"<PIL.Image.Image image mode=RGB size=100x100 at"
3940
)
4041

4142

42-
# canny.gen_gaussian_kernel()
4343
def test_gen_gaussian_kernel():
44-
resp = canny.gen_gaussian_kernel(9, sigma=1.4)
45-
# Assert ambiguous array
46-
assert resp.all()
44+
"""Test Gaussian kernel generation."""
45+
kernel = canny.gen_gaussian_kernel(9, sigma=1.4)
46+
# Verify kernel contains valid values
47+
assert kernel.all()
4748

4849

49-
# canny.py
5050
def test_canny():
51+
"""Test Canny edge detection."""
5152
canny_img = imread("digital_image_processing/image_data/lena_small.jpg", 0)
52-
# assert ambiguous array for all == True
53-
assert canny_img.all()
54-
canny_array = canny.canny(canny_img)
55-
# assert canny array for at least one True
56-
assert canny_array.any()
53+
assert canny_img.all() # Verify input image loaded correctly
54+
edges = canny.canny(canny_img)
55+
assert edges.any() # Verify edge detection produced output
5756

5857

59-
# filters/gaussian_filter.py
6058
def test_gen_gaussian_kernel_filter():
59+
"""Test Gaussian filter application."""
6160
assert gg.gaussian_filter(gray, 5, sigma=0.9).all()
6261

63-
6462
def test_convolve_filter():
65-
# laplace diagonals
63+
"""Test image convolution operation."""
64+
# Laplace kernel for edge detection
6665
laplace = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]])
67-
res = conv.img_convolve(gray, laplace).astype(uint8)
68-
assert res.any()
66+
result = conv.img_convolve(gray, laplace).astype(uint8)
67+
assert result.any() # Verify convolution output
6968

7069

7170
def test_median_filter():
71+
"""Test median noise reduction filter."""
7272
assert med.median_filter(gray, 3).any()
7373

7474

7575
def test_sobel_filter():
76-
grad, theta = sob.sobel_filter(gray)
77-
assert grad.any()
78-
assert theta.any()
76+
"""Test Sobel edge detection."""
77+
gradient, direction = sob.sobel_filter(gray)
78+
assert gradient.any() # Verify gradient magnitude
79+
assert direction.any() # Verify gradient direction
7980

8081

8182
def test_sepia():
82-
sepia = sp.make_sepia(img, 20)
83-
assert sepia.all()
83+
"""Test sepia tone filter."""
84+
sepia_img = sp.make_sepia(img, 20)
85+
assert sepia_img.all()
8486

8587

86-
def test_burkes(file_path: str = "digital_image_processing/image_data/lena_small.jpg"):
87-
burkes = bs.Burkes(imread(file_path, 1), 120)
88+
def test_burkes():
89+
"""Test Burkes dithering algorithm."""
90+
burkes = bs.Burkes(imread("digital_image_processing/image_data/lena_small.jpg", 1), 120)
8891
burkes.process()
8992
assert burkes.output_img.any()
9093

9194

92-
def test_nearest_neighbour(
93-
file_path: str = "digital_image_processing/image_data/lena_small.jpg",
94-
):
95-
nn = rs.NearestNeighbour(imread(file_path, 1), 400, 200)
95+
def test_nearest_neighbour():
96+
"""Test nearest-neighbor resizing."""
97+
nn = rs.NearestNeighbour(imread("digital_image_processing/image_data/lena_small.jpg", 1), 400, 200)
9698
nn.process()
9799
assert nn.output.any()
98100

99101

100102
def test_local_binary_pattern():
101-
# pull request 10161 before:
102-
# "digital_image_processing/image_data/lena.jpg"
103-
# after: "digital_image_processing/image_data/lena_small.jpg"
104-
105-
from os import getenv # Speed up our Continuous Integration tests
106-
107-
file_name = "lena_small.jpg" if getenv("CI") else "lena.jpg"
103+
"""Test Local Binary Pattern feature extraction."""
104+
# Use smaller image in CI environments for faster tests
105+
file_name = "lena_small.jpg" if os.getenv("CI") else "lena.jpg"
108106
file_path = f"digital_image_processing/image_data/{file_name}"
109107

110-
# Reading the image and converting it to grayscale
108+
# Load image in grayscale
111109
image = imread(file_path, 0)
112110

113-
# Test for get_neighbors_pixel function() return not None
114-
x_coordinate = 0
115-
y_coordinate = 0
116-
center = image[x_coordinate][y_coordinate]
117-
118-
neighbors_pixels = lbp.get_neighbors_pixel(
119-
image, x_coordinate, y_coordinate, center
120-
)
111+
# Test neighbor pixel collection
112+
center = image[0][0]
113+
neighbors = lbp.get_neighbors_pixel(image, 0, 0, center)
114+
assert neighbors is not None
121115

122-
assert neighbors_pixels is not None
123-
124-
# Test for local_binary_pattern function()
125-
# Create a numpy array as the same height and width of read image
126-
lbp_image = np.zeros((image.shape[0], image.shape[1]))
127-
128-
# Iterating through the image and calculating the local binary pattern value
129-
# for each pixel.
116+
# Test LBP feature map generation
117+
lbp_image = np.zeros_like(image, dtype=np.float32)
130118
for i in range(image.shape[0]):
131119
for j in range(image.shape[1]):
132120
lbp_image[i][j] = lbp.local_binary_value(image, i, j)
133-
134121
assert lbp_image.any()

0 commit comments

Comments
 (0)