Commit da12ed62e15fed6284e2f6a44e3b5b608d0bc2a3

Authored by Thiago Franco de Moraes
1 parent 298c9f95
Exists in master

Converting RGB dicom images to grayscale

invesalius/control.py
... ... @@ -508,6 +508,7 @@ class Controller():
508 508 self.ImportGroup(group, use_gui)
509 509  
510 510 def ImportGroup(self, group, gui=True):
  511 +
511 512 matrix, matrix_filename, dicom = self.OpenDicomGroup(group, 0, [0, 0], gui=gui)
512 513 self.CreateDicomProject(dicom, matrix, matrix_filename)
513 514  
... ... @@ -878,6 +879,13 @@ class Controller():
878 879  
879 880  
880 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 889 matrix, matrix_filename, dicom = self.OpenDicomGroup(group, interval, file_range, gui=True)
882 890 self.CreateDicomProject(dicom, matrix, matrix_filename)
883 891 self.LoadProject()
... ... @@ -918,9 +926,9 @@ class Controller():
918 926 xyspacing = dicom.image.spacing
919 927 orientation = dicom.image.orientation_label
920 928 samples_per_pixel = dicom.image.samples_per_pixel
921   -
922 929 print(f"\n\n\n\n{samples_per_pixel =}\n\n\n")
923 930  
  931 +
924 932 wl = float(dicom.image.level)
925 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 39 import invesalius.utils as utils
40 40 from invesalius import inv_paths
41 41 from invesalius.data import vtk_utils as vtk_utils
  42 +from skimage.color import rgb2gray
42 43  
43 44 if sys.platform == "win32":
44 45 try:
... ... @@ -483,10 +484,13 @@ def dcmmf2memmap(dcm_file, orientation):
483 484 image = reader.GetImage()
484 485 xs, ys, zs = image.GetSpacing()
485 486 pf = image.GetPixelFormat()
  487 + samples_per_pixel = pf.GetSamplesPerPixel()
486 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 492 temp_file = tempfile.mktemp()
488 493 matrix = numpy.memmap(temp_file, mode="w+", dtype="int16", shape=np_image.shape)
489   - print("Number of dimensions", np_image.shape)
490 494 z, y, x = np_image.shape
491 495 if orientation == "CORONAL":
492 496 spacing = xs, zs, ys
... ...
invesalius/reader/dicom.py
... ... @@ -962,7 +962,7 @@ class Parser():
962 962  
963 963 if not data:
964 964 return 1
965   - return data
  965 + return int(data)
966 966  
967 967 def GetPhotometricInterpretation(self):
968 968 """
... ...