{
  "id": 444313,
  "title": "Totalsegmentator output empty",
  "url": "/competitions/rsna-2023-abdominal-trauma-detection/discussion/444313",
  "author_name": "Gyula Maloveczky4",
  "post_date": "2023-10-01T10:57:12.930000",
  "votes": 0,
  "comment_count": 0,
  "views": 0,
  "content": "<p>If I convert the images to 8 bit Totalsegmentator outputs empty nifti files. Any ideas why this could be?</p>\n<p>Here is the code for the conversion (I have checked the images and they are not empty):</p>\n<pre><code>\n root, _,   os.walk(input_folder):\n       :\n         .endswith():\n            input_path = os.path.join(root, )\n            output_path = os.path.join(output_folder, )\n\n            \n            dcm = pydicom.dcmread(input_path)\n\n            \n            transfer_syntax_uid = dcm.()\n             transfer_syntax_uid  transfer_syntax_uid.name.startswith():\n                \n                pixel_data = pydicom.encaps.decode_data_sequence(dcm.PixelData)\n            :\n                \n                pixel_data = dcm.pixel_array\n\n            \n            pixel_data = (pixel_data - np.(pixel_data)) *  / (np.(pixel_data) - np.(pixel_data))\n            pixel_data_8_bit = pixel_data.astype(np.uint8)\n\n            \n            image = Image.fromarray(pixel_data_8_bit)\n            image = image.resize(target_size)\n\n            \n            dcm.PixelData = np.asarray(image).tobytes()\n            dcm.BitsStored = \n            dcm.BitsAllocated = \n            dcm.HighBit = \n            dcm.PixelRepresentation = \n            dcm.Rows, dcm.Columns = target_size  \n\n            \n            dcm.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRLittleEndian\n\n            \n            dcm.save_as(output_path)\n</code></pre>\n<p>`</p>",
  "messages": [
    {
      "id": 2463469,
      "postDate": "2023-10-01T10:57:12.930Z",
      "content": "<p>If I convert the images to 8 bit Totalsegmentator outputs empty nifti files. Any ideas why this could be?</p>\n<p>Here is the code for the conversion (I have checked the images and they are not empty):</p>\n<pre><code>\n root, _,   os.walk(input_folder):\n       :\n         .endswith():\n            input_path = os.path.join(root, )\n            output_path = os.path.join(output_folder, )\n\n            \n            dcm = pydicom.dcmread(input_path)\n\n            \n            transfer_syntax_uid = dcm.()\n             transfer_syntax_uid  transfer_syntax_uid.name.startswith():\n                \n                pixel_data = pydicom.encaps.decode_data_sequence(dcm.PixelData)\n            :\n                \n                pixel_data = dcm.pixel_array\n\n            \n            pixel_data = (pixel_data - np.(pixel_data)) *  / (np.(pixel_data) - np.(pixel_data))\n            pixel_data_8_bit = pixel_data.astype(np.uint8)\n\n            \n            image = Image.fromarray(pixel_data_8_bit)\n            image = image.resize(target_size)\n\n            \n            dcm.PixelData = np.asarray(image).tobytes()\n            dcm.BitsStored = \n            dcm.BitsAllocated = \n            dcm.HighBit = \n            dcm.PixelRepresentation = \n            dcm.Rows, dcm.Columns = target_size  \n\n            \n            dcm.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRLittleEndian\n\n            \n            dcm.save_as(output_path)\n</code></pre>\n<p>`</p>",
      "rawMarkdown": "If I convert the images to 8 bit Totalsegmentator outputs empty nifti files. Any ideas why this could be?\n\nHere is the code for the conversion (I have checked the images and they are not empty):\n\n\n    # Loop through the input DICOM files\n    for root, _, files in os.walk(input_folder):\n        for file in files:\n            if file.endswith(\".dcm\"):\n                input_path = os.path.join(root, file)\n                output_path = os.path.join(output_folder, file)\n\n                # Read the DICOM file\n                dcm = pydicom.dcmread(input_path)\n\n                # Check if the DICOM file is RLE-compressed\n                transfer_syntax_uid = dcm.get(\"TransferSyntaxUID\")\n                if transfer_syntax_uid and transfer_syntax_uid.name.startswith(\"RLE\"):\n                    # Decompress the pixel data using the pydicom library\n                    pixel_data = pydicom.encaps.decode_data_sequence(dcm.PixelData)\n                else:\n                    # No compression or unsupported compression type, use the original pixel data\n                    pixel_data = dcm.pixel_array\n\n                # Apply scaling to fit the dynamic range into 8 bits\n                pixel_data = (pixel_data - np.min(pixel_data)) * 255.0 / (np.max(pixel_data) - np.min(pixel_data))\n                pixel_data_8_bit = pixel_data.astype(np.uint8)\n\n                # Resize the image to the target size (e.g., 256x256)\n                image = Image.fromarray(pixel_data_8_bit)\n                image = image.resize(target_size)\n\n                # Update DICOM metadata\n                dcm.PixelData = np.asarray(image).tobytes()\n                dcm.BitsStored = 8\n                dcm.BitsAllocated = 8\n                dcm.HighBit = 7\n                dcm.PixelRepresentation = 0\n                dcm.Rows, dcm.Columns = target_size  # Update image dimensions\n\n                # Set TransferSyntaxUID to Uncompressed Explicit VR Little Endian\n                dcm.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRLittleEndian\n\n                # Save as an 8-bit DICOM file without compression\n                dcm.save_as(output_path)\n\n`"
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "2463469": "If I convert the images to 8 bit Totalsegmentator outputs empty nifti files. Any ideas why this could be?\n\nHere is the code for the conversion (I have checked the images and they are not empty):\n\n\n    # Loop through the input DICOM files\n    for root, _, files in os.walk(input_folder):\n        for file in files:\n            if file.endswith(\".dcm\"):\n                input_path = os.path.join(root, file)\n                output_path = os.path.join(output_folder, file)\n\n                # Read the DICOM file\n                dcm = pydicom.dcmread(input_path)\n\n                # Check if the DICOM file is RLE-compressed\n                transfer_syntax_uid = dcm.get(\"TransferSyntaxUID\")\n                if transfer_syntax_uid and transfer_syntax_uid.name.startswith(\"RLE\"):\n                    # Decompress the pixel data using the pydicom library\n                    pixel_data = pydicom.encaps.decode_data_sequence(dcm.PixelData)\n                else:\n                    # No compression or unsupported compression type, use the original pixel data\n                    pixel_data = dcm.pixel_array\n\n                # Apply scaling to fit the dynamic range into 8 bits\n                pixel_data = (pixel_data - np.min(pixel_data)) * 255.0 / (np.max(pixel_data) - np.min(pixel_data))\n                pixel_data_8_bit = pixel_data.astype(np.uint8)\n\n                # Resize the image to the target size (e.g., 256x256)\n                image = Image.fromarray(pixel_data_8_bit)\n                image = image.resize(target_size)\n\n                # Update DICOM metadata\n                dcm.PixelData = np.asarray(image).tobytes()\n                dcm.BitsStored = 8\n                dcm.BitsAllocated = 8\n                dcm.HighBit = 7\n                dcm.PixelRepresentation = 0\n                dcm.Rows, dcm.Columns = target_size  # Update image dimensions\n\n                # Set TransferSyntaxUID to Uncompressed Explicit VR Little Endian\n                dcm.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRLittleEndian\n\n                # Save as an 8-bit DICOM file without compression\n                dcm.save_as(output_path)\n\n`"
  }
}