|
2 | 2 | PyTest's for Digital Image Processing |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import os |
5 | 6 | import numpy as np |
6 | 7 | from cv2 import COLOR_BGR2GRAY, cvtColor, imread |
7 | 8 | from numpy import array, uint8 |
|
23 | 24 | gray = cvtColor(img, COLOR_BGR2GRAY) |
24 | 25 |
|
25 | 26 |
|
26 | | -# Test: convert_to_negative() |
27 | 27 | def test_convert_to_negative(): |
| 28 | + """Test negative image conversion.""" |
28 | 29 | 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 |
30 | 31 | assert negative_img.any() |
31 | 32 |
|
32 | 33 |
|
33 | | -# Test: change_contrast() |
34 | 34 | 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( |
38 | 39 | "<PIL.Image.Image image mode=RGB size=100x100 at" |
39 | 40 | ) |
40 | 41 |
|
41 | 42 |
|
42 | | -# canny.gen_gaussian_kernel() |
43 | 43 | 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() |
47 | 48 |
|
48 | 49 |
|
49 | | -# canny.py |
50 | 50 | def test_canny(): |
| 51 | + """Test Canny edge detection.""" |
51 | 52 | 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 |
57 | 56 |
|
58 | 57 |
|
59 | | -# filters/gaussian_filter.py |
60 | 58 | def test_gen_gaussian_kernel_filter(): |
| 59 | + """Test Gaussian filter application.""" |
61 | 60 | assert gg.gaussian_filter(gray, 5, sigma=0.9).all() |
62 | 61 |
|
63 | | - |
64 | 62 | def test_convolve_filter(): |
65 | | - # laplace diagonals |
| 63 | + """Test image convolution operation.""" |
| 64 | + # Laplace kernel for edge detection |
66 | 65 | 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 |
69 | 68 |
|
70 | 69 |
|
71 | 70 | def test_median_filter(): |
| 71 | + """Test median noise reduction filter.""" |
72 | 72 | assert med.median_filter(gray, 3).any() |
73 | 73 |
|
74 | 74 |
|
75 | 75 | 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 |
79 | 80 |
|
80 | 81 |
|
81 | 82 | 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() |
84 | 86 |
|
85 | 87 |
|
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) |
88 | 91 | burkes.process() |
89 | 92 | assert burkes.output_img.any() |
90 | 93 |
|
91 | 94 |
|
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) |
96 | 98 | nn.process() |
97 | 99 | assert nn.output.any() |
98 | 100 |
|
99 | 101 |
|
100 | 102 | 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" |
108 | 106 | file_path = f"digital_image_processing/image_data/{file_name}" |
109 | 107 |
|
110 | | - # Reading the image and converting it to grayscale |
| 108 | + # Load image in grayscale |
111 | 109 | image = imread(file_path, 0) |
112 | 110 |
|
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 |
121 | 115 |
|
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) |
130 | 118 | for i in range(image.shape[0]): |
131 | 119 | for j in range(image.shape[1]): |
132 | 120 | lbp_image[i][j] = lbp.local_binary_value(image, i, j) |
133 | | - |
134 | 121 | assert lbp_image.any() |
0 commit comments