From b87f001ad549bf0043395f1c4566e4439658d7be Mon Sep 17 00:00:00 2001 From: Vilens Jumutcs Date: Wed, 14 Jul 2021 10:40:38 +0200 Subject: [PATCH 1/3] resolving grayscale image handling --- miscnn/data_loading/interfaces/image_io.py | 9 +++-- tests/test_iointerfaces.py | 41 ++++++++++++++-------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/miscnn/data_loading/interfaces/image_io.py b/miscnn/data_loading/interfaces/image_io.py index d6b7b8e..4248dad 100644 --- a/miscnn/data_loading/interfaces/image_io.py +++ b/miscnn/data_loading/interfaces/image_io.py @@ -96,14 +96,17 @@ def load_image(self, index): img_raw = Image.open(os.path.join(img_path, "imaging" + "." + \ self.img_format)) # Convert image to rgb or grayscale - if self.img_type == "grayscale": + if self.img_type == "grayscale" and img_raw.mode != "L": img_pil = img_raw.convert("LA") - elif self.img_type == "rgb": + elif self.img_type == "rgb" and img_raw.mode != "RGB": img_pil = img_raw.convert("RGB") + else: + img_pil = img_raw # Convert Pillow image to numpy matrix img = np.array(img_pil) # Keep only intensity for grayscale images - if self.img_type =="grayscale" : img = img[:,:,0] + if self.img_type == "grayscale" and len(img.shape) > 2: + img = img[:, :, 0] # Return image return img, {"type": "image"} diff --git a/tests/test_iointerfaces.py b/tests/test_iointerfaces.py index 671ee2c..4949d60 100644 --- a/tests/test_iointerfaces.py +++ b/tests/test_iointerfaces.py @@ -43,18 +43,24 @@ def setUpClass(self): # Create image and segmentation np.random.seed(1234) img = np.random.rand(16, 16, 16) * 255 - self.img = img.astype(int) + self.img = img.astype(np.uint8) seg = np.random.rand(16, 16, 16) * 3 - self.seg = seg.astype(int) + self.seg = seg.astype(np.uint8) # Initialize temporary directory self.tmp_data = tempfile.TemporaryDirectory(prefix="tmp.miscnn.", suffix=".data") # Write PNG sample with image and segmentation - path_sample_image = opj(self.tmp_data.name, "image") + path_sample_image = opj(self.tmp_data.name, "image_01") os.mkdir(path_sample_image) - img_pillow = Image.fromarray(img[:,:,0].astype(np.uint8)) + img_pillow = Image.fromarray(self.img[:,:,0]) img_pillow.save(opj(path_sample_image, "imaging.png")) - seg_pillow = Image.fromarray(seg[:,:,0].astype(np.uint8)) + seg_pillow = Image.fromarray(self.seg[:,:,0]) + seg_pillow.save(opj(path_sample_image, "segmentation.png")) + path_sample_image = opj(self.tmp_data.name, "image_02") + os.mkdir(path_sample_image) + img_pillow = Image.fromarray(self.img[:, :, :3]) + img_pillow.save(opj(path_sample_image, "imaging.png")) + seg_pillow = Image.fromarray(self.seg[:, :, 0]) seg_pillow.save(opj(path_sample_image, "segmentation.png")) # Write NIfTI sample with image and segmentation path_sample_nifti = opj(self.tmp_data.name, "nifti") @@ -76,23 +82,28 @@ def test_IOI_IMAGE_creation(self): interface = Image_interface() # Initialization def test_IOI_IMAGE_initialize(self): - interface = Image_interface(pattern="image") + interface = Image_interface(pattern="image_\\d+") sample_list = interface.initialize(self.tmp_data.name) - self.assertEqual(len(sample_list), 1) - self.assertEqual(sample_list[0], "image") + self.assertEqual(len(sample_list), 2) + self.assertTrue("image_01" in sample_list) + self.assertTrue("image_02" in sample_list) # Loading Images and Segmentations def test_IOI_IMAGE_loading(self): - interface = Image_interface(pattern="image") + interface = Image_interface(pattern="image_\\d+") sample_list = interface.initialize(self.tmp_data.name) - img = interface.load_image(sample_list[0]) - seg = interface.load_segmentation(sample_list[0]) - self.assertTrue(np.array_equal(img[0], self.img[:,:,0])) - self.assertTrue(np.array_equal(seg, self.seg[:,:,0])) + img_01 = interface.load_image(sample_list[0]) + seg_01 = interface.load_segmentation(sample_list[0]) + true_img_01 = np.array(Image.fromarray(self.img[:, :, :3]).convert('LA')) + self.assertTrue(np.array_equal(img_01[0], true_img_01[:,:,0])) + self.assertTrue(np.array_equal(seg_01, self.seg[:,:,0])) + img_02 = interface.load_image(sample_list[1]) + seg_02 = interface.load_segmentation(sample_list[1]) + self.assertTrue(np.array_equal(img_02[0], self.img[:, :, 0])) + self.assertTrue(np.array_equal(seg_02, self.seg[:, :, 0])) # NIFTI_interface - Loading and Storage of Predictions def test_IOI_IMAGE_predictionhandling(self): interface = Image_interface(pattern="image") - sample_list = interface.initialize(self.tmp_data.name) - sample = MIScnn_sample.Sample("pred.image", self.img[:,:,0], 1, 2); + sample = MIScnn_sample.Sample("pred.image", self.img[:,:,0], 1, 2) sample.add_prediction(self.seg[:,:,0]); interface.save_prediction(sample, self.tmp_data.name) pred = interface.load_prediction("pred.image", self.tmp_data.name) From 5af9dc49c47263fd859208a824e1b407519cbd99 Mon Sep 17 00:00:00 2001 From: Vilens Jumutcs Date: Thu, 15 Jul 2021 10:11:31 +0200 Subject: [PATCH 2/3] resolving grayscale image handling (fixes) --- miscnn/data_loading/interfaces/image_io.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/miscnn/data_loading/interfaces/image_io.py b/miscnn/data_loading/interfaces/image_io.py index 4248dad..e61b10c 100644 --- a/miscnn/data_loading/interfaces/image_io.py +++ b/miscnn/data_loading/interfaces/image_io.py @@ -95,8 +95,8 @@ def load_image(self, index): # Load image from file img_raw = Image.open(os.path.join(img_path, "imaging" + "." + \ self.img_format)) - # Convert image to rgb or grayscale - if self.img_type == "grayscale" and img_raw.mode != "L": + # Convert image to rgb or grayscale if needed + if self.img_type == "grayscale" and len(img_raw.getbands()) > 1: img_pil = img_raw.convert("LA") elif self.img_type == "rgb" and img_raw.mode != "RGB": img_pil = img_raw.convert("RGB") @@ -104,7 +104,7 @@ def load_image(self, index): img_pil = img_raw # Convert Pillow image to numpy matrix img = np.array(img_pil) - # Keep only intensity for grayscale images + # Keep only intensity for grayscale images if needed if self.img_type == "grayscale" and len(img.shape) > 2: img = img[:, :, 0] # Return image From c35101da5f7da6604f2630da792fd1beb096c56d Mon Sep 17 00:00:00 2001 From: vilen Date: Wed, 21 Jul 2021 17:38:26 +0200 Subject: [PATCH 3/3] resolving grayscale image handling (fixes) --- tests/test_iointerfaces.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_iointerfaces.py b/tests/test_iointerfaces.py index 4949d60..235a5b9 100644 --- a/tests/test_iointerfaces.py +++ b/tests/test_iointerfaces.py @@ -90,15 +90,15 @@ def test_IOI_IMAGE_initialize(self): # Loading Images and Segmentations def test_IOI_IMAGE_loading(self): interface = Image_interface(pattern="image_\\d+") - sample_list = interface.initialize(self.tmp_data.name) + sample_list = sorted(interface.initialize(self.tmp_data.name)) img_01 = interface.load_image(sample_list[0]) seg_01 = interface.load_segmentation(sample_list[0]) - true_img_01 = np.array(Image.fromarray(self.img[:, :, :3]).convert('LA')) - self.assertTrue(np.array_equal(img_01[0], true_img_01[:,:,0])) + self.assertTrue(np.array_equal(img_01[0], self.img[:, :, 0])) self.assertTrue(np.array_equal(seg_01, self.seg[:,:,0])) img_02 = interface.load_image(sample_list[1]) seg_02 = interface.load_segmentation(sample_list[1]) - self.assertTrue(np.array_equal(img_02[0], self.img[:, :, 0])) + true_img_02 = np.array(Image.fromarray(self.img[:, :, :3]).convert('LA')) + self.assertTrue(np.array_equal(img_02[0], true_img_02[:,:,0])) self.assertTrue(np.array_equal(seg_02, self.seg[:, :, 0])) # NIFTI_interface - Loading and Storage of Predictions def test_IOI_IMAGE_predictionhandling(self):