Commit da12ed62e15fed6284e2f6a44e3b5b608d0bc2a3
1 parent
298c9f95
Exists in
master
Converting RGB dicom images to grayscale
Showing
3 changed files
with
15 additions
and
3 deletions
Show diff stats
invesalius/control.py
@@ -508,6 +508,7 @@ class Controller(): | @@ -508,6 +508,7 @@ class Controller(): | ||
508 | self.ImportGroup(group, use_gui) | 508 | self.ImportGroup(group, use_gui) |
509 | 509 | ||
510 | def ImportGroup(self, group, gui=True): | 510 | def ImportGroup(self, group, gui=True): |
511 | + | ||
511 | matrix, matrix_filename, dicom = self.OpenDicomGroup(group, 0, [0, 0], gui=gui) | 512 | matrix, matrix_filename, dicom = self.OpenDicomGroup(group, 0, [0, 0], gui=gui) |
512 | self.CreateDicomProject(dicom, matrix, matrix_filename) | 513 | self.CreateDicomProject(dicom, matrix, matrix_filename) |
513 | 514 | ||
@@ -878,6 +879,13 @@ class Controller(): | @@ -878,6 +879,13 @@ class Controller(): | ||
878 | 879 | ||
879 | 880 | ||
880 | def OnOpenDicomGroup(self, group, interval, file_range): | 881 | def OnOpenDicomGroup(self, group, interval, file_range): |
882 | + dicom = group.GetDicomSample() | ||
883 | + samples_per_pixel = dicom.image.samples_per_pixel | ||
884 | + print(f"\n\n\n\n{samples_per_pixel =}\n\n\n") | ||
885 | + if samples_per_pixel == 3: | ||
886 | + dlg = wx.MessageDialog(wx.GetApp().GetTopWindow(), _("this is a rgb image, it's necessary to convert to grayscale to open on invesalius.\ndo you want to convert it to grayscale?"), _("Confirm"), wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION) | ||
887 | + if dlg.ShowModal() != wx.ID_YES: | ||
888 | + return | ||
881 | matrix, matrix_filename, dicom = self.OpenDicomGroup(group, interval, file_range, gui=True) | 889 | matrix, matrix_filename, dicom = self.OpenDicomGroup(group, interval, file_range, gui=True) |
882 | self.CreateDicomProject(dicom, matrix, matrix_filename) | 890 | self.CreateDicomProject(dicom, matrix, matrix_filename) |
883 | self.LoadProject() | 891 | self.LoadProject() |
@@ -918,9 +926,9 @@ class Controller(): | @@ -918,9 +926,9 @@ class Controller(): | ||
918 | xyspacing = dicom.image.spacing | 926 | xyspacing = dicom.image.spacing |
919 | orientation = dicom.image.orientation_label | 927 | orientation = dicom.image.orientation_label |
920 | samples_per_pixel = dicom.image.samples_per_pixel | 928 | samples_per_pixel = dicom.image.samples_per_pixel |
921 | - | ||
922 | print(f"\n\n\n\n{samples_per_pixel =}\n\n\n") | 929 | print(f"\n\n\n\n{samples_per_pixel =}\n\n\n") |
923 | 930 | ||
931 | + | ||
924 | wl = float(dicom.image.level) | 932 | wl = float(dicom.image.level) |
925 | ww = float(dicom.image.window) | 933 | ww = float(dicom.image.window) |
926 | 934 |
invesalius/data/imagedata_utils.py
@@ -39,6 +39,7 @@ import invesalius.reader.bitmap_reader as bitmap_reader | @@ -39,6 +39,7 @@ import invesalius.reader.bitmap_reader as bitmap_reader | ||
39 | import invesalius.utils as utils | 39 | import invesalius.utils as utils |
40 | from invesalius import inv_paths | 40 | from invesalius import inv_paths |
41 | from invesalius.data import vtk_utils as vtk_utils | 41 | from invesalius.data import vtk_utils as vtk_utils |
42 | +from skimage.color import rgb2gray | ||
42 | 43 | ||
43 | if sys.platform == "win32": | 44 | if sys.platform == "win32": |
44 | try: | 45 | try: |
@@ -483,10 +484,13 @@ def dcmmf2memmap(dcm_file, orientation): | @@ -483,10 +484,13 @@ def dcmmf2memmap(dcm_file, orientation): | ||
483 | image = reader.GetImage() | 484 | image = reader.GetImage() |
484 | xs, ys, zs = image.GetSpacing() | 485 | xs, ys, zs = image.GetSpacing() |
485 | pf = image.GetPixelFormat() | 486 | pf = image.GetPixelFormat() |
487 | + samples_per_pixel = pf.GetSamplesPerPixel() | ||
486 | np_image = converters.gdcm_to_numpy(image, pf.GetSamplesPerPixel() == 1) | 488 | np_image = converters.gdcm_to_numpy(image, pf.GetSamplesPerPixel() == 1) |
489 | + if samples_per_pixel == 3: | ||
490 | + np_image = image_normalize(rgb2gray(np_image), 0, 255) | ||
491 | + print(f"\n\n\n{np_image.min() =} - {np_image.max() =}\n\n\n") | ||
487 | temp_file = tempfile.mktemp() | 492 | temp_file = tempfile.mktemp() |
488 | matrix = numpy.memmap(temp_file, mode="w+", dtype="int16", shape=np_image.shape) | 493 | matrix = numpy.memmap(temp_file, mode="w+", dtype="int16", shape=np_image.shape) |
489 | - print("Number of dimensions", np_image.shape) | ||
490 | z, y, x = np_image.shape | 494 | z, y, x = np_image.shape |
491 | if orientation == "CORONAL": | 495 | if orientation == "CORONAL": |
492 | spacing = xs, zs, ys | 496 | spacing = xs, zs, ys |
invesalius/reader/dicom.py