{
  "id": 207726,
  "title": "VTK: Loading Images Faster",
  "url": "/competitions/vinbigdata-chest-xray-abnormalities-detection/discussion/207726",
  "author_name": "Pythonian",
  "post_date": "2020-12-31T02:46:01.959000",
  "votes": 1,
  "comment_count": 0,
  "views": 0,
  "content": "<p>I don't want anybody to lose a lot of time loading their data. So I think that it would be nice for the community to use VTK, a library written in C++ for loading files (including dicom). Comparing the function <a href=\"https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\" target=\"_blank\">read_xray  </a> to VTK, I found that the times were 38.9 s vs 5.5 s for loading 100 images. However, VTK is notoriously undocumented from what I've seen, and the function I threw together with VTK still loads images inconsistently. If you guys end up finding the fix, please share it in a kernel or over here. <br>\nThis is what I have now :<br>\nimport vtk<br>\nfrom vtk.util import numpy_support<br>\ndef get_dicom(PathDicom):<br>\n    reader = vtk.vtkDICOMImageReader()<br>\n    reader.SetFileName(PathDicom)<br>\n    # reader.SetDirectoryName(PathDicom)<br>\n    reader.Update()<br>\n    # Load dimensions using <code>GetDataExtent</code><br>\n    _extent = reader.GetDataExtent()<br>\n    ConstPixelDims = [_extent[1]-_extent[0]+1, _extent[3]-_extent[2]+1, _extent[5]-_extent[4]+1][:2]</p>\n<pre><code># Load spacing values\nConstPixelSpacing = reader.GetPixelSpacing()\n\n# Get the 'vtkImageData' object from the reader\nimageData = reader.GetOutput()\n# Get the 'vtkPointData' object from the 'vtkImageData' object\npointData = imageData.GetPointData()\n# Ensure that only one array exists within the 'vtkPointData' object\n# assert (pointData.GetNumberOfArrays()==1)\n# Get the `vtkArray` (or whatever derived type) which is needed for the `numpy_support.vtk_to_numpy` function\narrayData = pointData.GetArray(0)\n\n# Convert the `vtkArray` to a NumPy array\nArrayDicom = numpy_support.vtk_to_numpy(arrayData)\n# Reshape the NumPy array using 'ConstPixelDims' as a 'shape'\nArrayDicom = ArrayDicom.reshape(ConstPixelDims, order='F')\nreturn ArrayDicom \n</code></pre>",
  "messages": [
    {
      "id": 1133199,
      "postDate": "2020-12-31T02:46:01.960Z",
      "content": "<p>I don't want anybody to lose a lot of time loading their data. So I think that it would be nice for the community to use VTK, a library written in C++ for loading files (including dicom). Comparing the function <a href=\"https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\" target=\"_blank\">read_xray  </a> to VTK, I found that the times were 38.9 s vs 5.5 s for loading 100 images. However, VTK is notoriously undocumented from what I've seen, and the function I threw together with VTK still loads images inconsistently. If you guys end up finding the fix, please share it in a kernel or over here. <br>\nThis is what I have now :<br>\nimport vtk<br>\nfrom vtk.util import numpy_support<br>\ndef get_dicom(PathDicom):<br>\n    reader = vtk.vtkDICOMImageReader()<br>\n    reader.SetFileName(PathDicom)<br>\n    # reader.SetDirectoryName(PathDicom)<br>\n    reader.Update()<br>\n    # Load dimensions using <code>GetDataExtent</code><br>\n    _extent = reader.GetDataExtent()<br>\n    ConstPixelDims = [_extent[1]-_extent[0]+1, _extent[3]-_extent[2]+1, _extent[5]-_extent[4]+1][:2]</p>\n<pre><code># Load spacing values\nConstPixelSpacing = reader.GetPixelSpacing()\n\n# Get the 'vtkImageData' object from the reader\nimageData = reader.GetOutput()\n# Get the 'vtkPointData' object from the 'vtkImageData' object\npointData = imageData.GetPointData()\n# Ensure that only one array exists within the 'vtkPointData' object\n# assert (pointData.GetNumberOfArrays()==1)\n# Get the `vtkArray` (or whatever derived type) which is needed for the `numpy_support.vtk_to_numpy` function\narrayData = pointData.GetArray(0)\n\n# Convert the `vtkArray` to a NumPy array\nArrayDicom = numpy_support.vtk_to_numpy(arrayData)\n# Reshape the NumPy array using 'ConstPixelDims' as a 'shape'\nArrayDicom = ArrayDicom.reshape(ConstPixelDims, order='F')\nreturn ArrayDicom \n</code></pre>",
      "rawMarkdown": "I don't want anybody to lose a lot of time loading their data. So I think that it would be nice for the community to use VTK, a library written in C++ for loading files (including dicom). Comparing the function [read_xray  ](https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way) to VTK, I found that the times were 38.9 s vs 5.5 s for loading 100 images. However, VTK is notoriously undocumented from what I've seen, and the function I threw together with VTK still loads images inconsistently. If you guys end up finding the fix, please share it in a kernel or over here. \nThis is what I have now :\nimport vtk\nfrom vtk.util import numpy_support\ndef get_dicom(PathDicom):\n    reader = vtk.vtkDICOMImageReader()\n    reader.SetFileName(PathDicom)\n    # reader.SetDirectoryName(PathDicom)\n    reader.Update()\n    # Load dimensions using `GetDataExtent`\n    _extent = reader.GetDataExtent()\n    ConstPixelDims = [_extent[1]-_extent[0]+1, _extent[3]-_extent[2]+1, _extent[5]-_extent[4]+1][:2]\n\n    # Load spacing values\n    ConstPixelSpacing = reader.GetPixelSpacing()\n\n    # Get the 'vtkImageData' object from the reader\n    imageData = reader.GetOutput()\n    # Get the 'vtkPointData' object from the 'vtkImageData' object\n    pointData = imageData.GetPointData()\n    # Ensure that only one array exists within the 'vtkPointData' object\n    # assert (pointData.GetNumberOfArrays()==1)\n    # Get the `vtkArray` (or whatever derived type) which is needed for the `numpy_support.vtk_to_numpy` function\n    arrayData = pointData.GetArray(0)\n\n    # Convert the `vtkArray` to a NumPy array\n    ArrayDicom = numpy_support.vtk_to_numpy(arrayData)\n    # Reshape the NumPy array using 'ConstPixelDims' as a 'shape'\n    ArrayDicom = ArrayDicom.reshape(ConstPixelDims, order='F')\n    return ArrayDicom ",
      "votes": 1
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1133199": "I don't want anybody to lose a lot of time loading their data. So I think that it would be nice for the community to use VTK, a library written in C++ for loading files (including dicom). Comparing the function [read_xray  ](https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way) to VTK, I found that the times were 38.9 s vs 5.5 s for loading 100 images. However, VTK is notoriously undocumented from what I've seen, and the function I threw together with VTK still loads images inconsistently. If you guys end up finding the fix, please share it in a kernel or over here. \nThis is what I have now :\nimport vtk\nfrom vtk.util import numpy_support\ndef get_dicom(PathDicom):\n    reader = vtk.vtkDICOMImageReader()\n    reader.SetFileName(PathDicom)\n    # reader.SetDirectoryName(PathDicom)\n    reader.Update()\n    # Load dimensions using `GetDataExtent`\n    _extent = reader.GetDataExtent()\n    ConstPixelDims = [_extent[1]-_extent[0]+1, _extent[3]-_extent[2]+1, _extent[5]-_extent[4]+1][:2]\n\n    # Load spacing values\n    ConstPixelSpacing = reader.GetPixelSpacing()\n\n    # Get the 'vtkImageData' object from the reader\n    imageData = reader.GetOutput()\n    # Get the 'vtkPointData' object from the 'vtkImageData' object\n    pointData = imageData.GetPointData()\n    # Ensure that only one array exists within the 'vtkPointData' object\n    # assert (pointData.GetNumberOfArrays()==1)\n    # Get the `vtkArray` (or whatever derived type) which is needed for the `numpy_support.vtk_to_numpy` function\n    arrayData = pointData.GetArray(0)\n\n    # Convert the `vtkArray` to a NumPy array\n    ArrayDicom = numpy_support.vtk_to_numpy(arrayData)\n    # Reshape the NumPy array using 'ConstPixelDims' as a 'shape'\n    ArrayDicom = ArrayDicom.reshape(ConstPixelDims, order='F')\n    return ArrayDicom "
  }
}