|
14 | 14 |
|
15 | 15 | import cdl.algorithms.image as alg |
16 | 16 | import cdl.computation.image as cpi |
| 17 | +import cdl.obj |
| 18 | +import cdl.param |
17 | 19 | import cdl.tests.data as ctd |
18 | 20 | from cdl.env import execenv |
19 | 21 | from cdl.utils.tests import check_array_result, check_scalar_result |
@@ -44,6 +46,74 @@ def test_image_fft_interactive(): |
44 | 46 | view_images_side_by_side(images, titles, rows=2, title="2D FFT/iFFT") |
45 | 47 |
|
46 | 48 |
|
| 49 | +@pytest.mark.validation |
| 50 | +def test_image_zero_padding() -> None: |
| 51 | + """2D FFT zero padding validation test.""" |
| 52 | + ima1 = ctd.create_checkerboard() |
| 53 | + rows, cols = 2, 2 |
| 54 | + param = cdl.param.ZeroPadding2DParam.create(rows=rows, cols=cols) |
| 55 | + assert param.strategy == "custom", ( |
| 56 | + f"Wrong default strategy: {param.strategy} (expected 'custom')" |
| 57 | + ) |
| 58 | + |
| 59 | + # Validate the zero padding with bottom-right position |
| 60 | + param.position = "bottom-right" |
| 61 | + ima2 = cpi.compute_zero_padding(ima1, param) |
| 62 | + sh1, sh2 = ima1.data.shape, ima2.data.shape |
| 63 | + exp_sh2 = (sh1[0] + rows, sh1[1] + cols) |
| 64 | + execenv.print("Validating zero padding for bottom-right position...", end=" ") |
| 65 | + assert sh2 == exp_sh2, f"Wrong shape: {sh2} (expected {exp_sh2})" |
| 66 | + assert np.all(ima2.data[0 : sh1[0], 0 : sh1[1]] == ima1.data), ( |
| 67 | + "Altered data in original image area" |
| 68 | + ) |
| 69 | + assert np.all(ima2.data[sh1[0] : sh2[0], sh1[1] : sh2[1]] == 0), ( |
| 70 | + "Altered data in padded area" |
| 71 | + ) |
| 72 | + execenv.print("OK") |
| 73 | + |
| 74 | + # Validate the zero padding with center position |
| 75 | + param.position = "center" |
| 76 | + ima3 = cpi.compute_zero_padding(ima1, param) |
| 77 | + sh3 = ima3.data.shape |
| 78 | + exp_sh3 = (sh1[0] + rows, sh1[1] + cols) |
| 79 | + execenv.print("Validating zero padding for center position...", end=" ") |
| 80 | + assert sh3 == exp_sh3, f"Wrong shape: {sh3} (expected {exp_sh3})" |
| 81 | + assert np.all( |
| 82 | + ima3.data[rows // 2 : sh1[0] + rows // 2, cols // 2 : sh1[1] + cols // 2] |
| 83 | + == ima1.data |
| 84 | + ), "Altered data in original image area" |
| 85 | + assert np.all(ima3.data[0 : rows // 2, :] == 0), "Altered data in padded area (top)" |
| 86 | + assert np.all(ima3.data[sh1[0] + rows // 2 :, :] == 0), ( |
| 87 | + "Altered data in padded area (bottom)" |
| 88 | + ) |
| 89 | + assert np.all(ima3.data[:, 0 : cols // 2] == 0), ( |
| 90 | + "Altered data in padded area (left)" |
| 91 | + ) |
| 92 | + assert np.all(ima3.data[:, sh1[1] + cols // 2 :] == 0), ( |
| 93 | + "Altered data in padded area (right)" |
| 94 | + ) |
| 95 | + execenv.print("OK") |
| 96 | + |
| 97 | + # Validate zero padding with strategies other than custom size |
| 98 | + # Image size is (200, 300) and the next power of 2 is (256, 512) |
| 99 | + # The multiple of 64 is (256, 320) |
| 100 | + ima4 = cdl.obj.create_image("", np.zeros((200, 300))) |
| 101 | + for strategy, (exp_rows, exp_cols) in ( |
| 102 | + ("next_pow2", (56, 212)), |
| 103 | + ("multiple_of_64", (56, 20)), |
| 104 | + ): |
| 105 | + param = cdl.param.ZeroPadding2DParam.create(strategy=strategy) |
| 106 | + param.update_from_image(ima4) |
| 107 | + assert param.rows == exp_rows, ( |
| 108 | + f"Wrong row number for '{param.strategy}' strategy: {param.rows}" |
| 109 | + f" (expected {exp_rows})" |
| 110 | + ) |
| 111 | + assert param.cols == exp_cols, ( |
| 112 | + f"Wrong column number for '{param.strategy}' strategy: {param.cols}" |
| 113 | + f" (expected {exp_cols})" |
| 114 | + ) |
| 115 | + |
| 116 | + |
47 | 117 | @pytest.mark.validation |
48 | 118 | def test_image_fft() -> None: |
49 | 119 | """2D FFT validation test.""" |
@@ -105,6 +175,7 @@ def test_image_psd() -> None: |
105 | 175 |
|
106 | 176 | if __name__ == "__main__": |
107 | 177 | test_image_fft_interactive() |
| 178 | + test_image_zero_padding() |
108 | 179 | test_image_fft() |
109 | 180 | test_image_magnitude_spectrum() |
110 | 181 | test_image_phase_spectrum() |
|
0 commit comments