Spaces:
Running
Running
File size: 7,803 Bytes
eab7c24 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | import tempfile
import unittest
from pathlib import Path
import cv2
import numpy as np
import tifffile
from skimage.util import img_as_ubyte
from cleaning import (
INPAINT_RADIUS,
_as_display_rgb_uint8,
_as_rgb_uint8,
clean_display_image_and_mask,
correct_vignetting,
create_debris_mask,
create_tissue_mask,
read_image_rgb_and_preview,
write_image_rgb,
)
class PreviewConversionTests(unittest.TestCase):
def test_uint16_preview_uses_full_dtype_range(self):
image = np.array([[0, 32768, 65535]], dtype=np.uint16)
preview = _as_display_rgb_uint8(image)
expected = np.array([0, 128, 255], dtype=np.uint8)
np.testing.assert_array_equal(preview[:, :, 0], expected[None, :])
np.testing.assert_array_equal(preview[:, :, 1], expected[None, :])
np.testing.assert_array_equal(preview[:, :, 2], expected[None, :])
def test_uint8_preview_is_unchanged(self):
image = np.array([[[10, 120, 240]]], dtype=np.uint8)
preview = _as_display_rgb_uint8(image)
np.testing.assert_array_equal(preview, image)
def test_reader_returns_separate_processing_and_display_views(self):
image = np.full((10, 10, 3), 32768, dtype=np.uint16)
image[0, 0] = 0
image[-1, -1] = 65535
with tempfile.TemporaryDirectory() as directory:
image_path = Path(directory) / "image.tiff"
tifffile.imwrite(image_path, image, photometric="rgb")
processing, display = read_image_rgb_and_preview(image_path)
np.testing.assert_array_equal(processing, _as_rgb_uint8(image))
np.testing.assert_array_equal(display, img_as_ubyte(image))
def test_cleaned_output_uses_display_brightness_and_processing_mask(self):
raw = np.full((32, 32, 3), 32768, dtype=np.uint16)
raw[12:20, 12:20] = 0
processing = _as_rgb_uint8(raw)
display = _as_display_rgb_uint8(raw)
cleaned, debris_mask = clean_display_image_and_mask(
processing,
display,
)
expected_mask = create_debris_mask(processing)
expected_cleaned = cv2.inpaint(
display,
expected_mask,
INPAINT_RADIUS,
cv2.INPAINT_TELEA,
)
np.testing.assert_array_equal(debris_mask, expected_mask)
np.testing.assert_array_equal(cleaned, expected_cleaned)
def test_vignetting_correction_flattens_detected_edge_falloff(self):
size = 160
axis = np.linspace(-1, 1, size, dtype=np.float32)
x, y = np.meshgrid(axis, axis)
illumination = 1 - 0.18 * (x * x + y * y)
base_color = np.array([230, 220, 210], dtype=np.float32)
image = np.clip(
illumination[..., None] * base_color,
0,
255,
).astype(np.uint8)
corrected, was_corrected = correct_vignetting(image)
center = np.s_[60:100, 60:100]
border = np.zeros((size, size), dtype=bool)
border[:20] = True
border[-20:] = True
border[:, :20] = True
border[:, -20:] = True
before_gap = image[center].mean() - image[border].mean()
after_gap = corrected[center].mean() - corrected[border].mean()
self.assertTrue(was_corrected)
self.assertLess(abs(after_gap), abs(before_gap) * 0.25)
def test_uniform_image_skips_vignetting_correction(self):
image = np.full((128, 128, 3), 220, dtype=np.uint8)
corrected, was_corrected = correct_vignetting(image)
self.assertFalse(was_corrected)
np.testing.assert_array_equal(corrected, image)
def test_vignetting_correction_handles_steep_one_sided_falloff(self):
height, width = 180, 240
x = np.linspace(0, 1, width, dtype=np.float32)
illumination = 1 - 0.35 * np.clip((x - 0.72) / 0.28, 0, 1)
base_color = np.array([245, 240, 235], dtype=np.float32)
image = np.clip(
illumination[None, :, None] * base_color,
0,
255,
).astype(np.uint8)
image = np.repeat(image, height, axis=0)
corrected, was_corrected = correct_vignetting(image)
center_level = image[:, 80:140].mean()
edge_level = image[:, -12:].mean()
corrected_center_level = corrected[:, 80:140].mean()
corrected_edge_level = corrected[:, -12:].mean()
before_gap = center_level - edge_level
after_gap = corrected_center_level - corrected_edge_level
self.assertTrue(was_corrected)
self.assertLess(after_gap, before_gap * 0.15)
def test_vignetting_correction_preserves_stained_tissue(self):
height, width = 180, 240
x = np.linspace(0, 1, width, dtype=np.float32)
illumination = 1 - 0.30 * np.clip((x - 0.65) / 0.35, 0, 1)
background_color = np.array([245, 240, 235], dtype=np.float32)
image = np.clip(
illumination[None, :, None] * background_color,
0,
255,
).astype(np.uint8)
image = np.repeat(image, height, axis=0)
tissue_mask = np.zeros((height, width), dtype=np.uint8)
cv2.circle(tissue_mask, (190, 90), 35, 255, cv2.FILLED)
tissue_color = np.array([155, 90, 110], dtype=np.uint8)
image[tissue_mask > 0] = tissue_color
corrected, was_corrected = correct_vignetting(image)
self.assertTrue(was_corrected)
np.testing.assert_array_equal(
corrected[tissue_mask > 0],
image[tissue_mask > 0],
)
self.assertGreater(
corrected[:, -12:][tissue_mask[:, -12:] == 0].mean(),
image[:, -12:][tissue_mask[:, -12:] == 0].mean() + 40,
)
def test_tissue_mask_fills_and_preserves_pale_spheroid_interior(self):
height, width = 240, 320
x = np.linspace(0, 1, width, dtype=np.float32)
illumination = 1 - 0.30 * np.clip((x - 0.65) / 0.35, 0, 1)
background_color = np.array([245, 240, 235], dtype=np.float32)
image = np.clip(
illumination[None, :, None] * background_color,
0,
255,
).astype(np.uint8)
image = np.repeat(image, height, axis=0)
spheroid = np.zeros((height, width), dtype=np.uint8)
cv2.circle(spheroid, (250, 120), 55, 255, cv2.FILLED)
image[spheroid > 0] = [220, 205, 205]
cv2.circle(image, (250, 120), 55, (150, 85, 105), 10)
image[110:130, 240:260] = 0
tissue_mask = create_tissue_mask(image)
corrected, was_corrected = correct_vignetting(image)
cleaned, debris_mask = clean_display_image_and_mask(image, image)
self.assertTrue(was_corrected)
self.assertEqual(tissue_mask[120, 250], 255)
np.testing.assert_array_equal(
corrected[spheroid > 0],
image[spheroid > 0],
)
self.assertTrue(np.any(debris_mask[110:130, 240:260]))
self.assertFalse(
np.array_equal(
cleaned[110:130, 240:260],
image[110:130, 240:260],
)
)
def test_tiff_output_uses_lossless_lzw_compression(self):
image = np.full((128, 128, 3), 127, dtype=np.uint8)
with tempfile.TemporaryDirectory() as directory:
image_path = Path(directory) / "cleaned.tiff"
write_image_rgb(image_path, image)
with tifffile.TiffFile(image_path) as tiff:
page = tiff.pages[0]
self.assertEqual(page.compression.name, "LZW")
self.assertEqual(page.tags["Predictor"].value, 2)
np.testing.assert_array_equal(page.asarray(), image)
self.assertLess(image_path.stat().st_size, image.nbytes)
if __name__ == "__main__":
unittest.main()
|