# ctk::vtkImageDataToQImage generates weird results

**URL:** <https://discourse.slicer.org/t/ctk-vtkimagedatatoqimage-generates-weird-results/25881>\
**Category:** Development\
**Created:** [October 25, 2022, 3:08am UTC](https://discourse.slicer.org/t/ctk-vtkimagedatatoqimage-generates-weird-results/25881 "2022-10-25T03:08:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nnzzll](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/nnzzll/32/16350_2.png) [@nnzzll](https://discourse.slicer.org/u/nnzzll)\
**Post date:** [October 25, 2022, 3:08am UTC](https://discourse.slicer.org/t/ctk-vtkimagedatatoqimage-generates-weird-results/25881/1 "2022-10-25T03:08:38Z")

</div>

I’m trying to convert vtkImageData to a QImage and display it.

Image below is the original image data displayed in sliceWidget.

 ![Screenshot from 2022-10-25 11-01-14](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/c/2caf4892a1d629298875031914a0c0754739b9c1.jpeg)

And this is what I got by using `ctk::vtkImageDataToQImage`

 ![Screenshot from 2022-10-25 11-03-23](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/d/bdcfb1731986c223e681a995bf873d7e9f36c7a4.png)

It confused me since in most cases, `ctk::vtkImageDataToQImage` generates the right result, I don’t know why this kind of case happened

---

<div class="post-metadata">

**Author:** ![nnzzll](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/nnzzll/32/16350_2.png) [@nnzzll](https://discourse.slicer.org/u/nnzzll)\
**Post date:** [October 25, 2022, 7:42am UTC](https://discourse.slicer.org/t/ctk-vtkimagedatatoqimage-generates-weird-results/25881/2 "2022-10-25T07:42:14Z")

</div>

The problem is due to `QImage()` constructor.  
In most cases, `QImage(int width, int height,QImage::QFormat format)` works fine, however, sometimes the `bytesPerLine` need to be specified, otherwise this kind of tilted issue happens.  
This issue can be solved by the code below:

```auto
QImage VTKImageDataToQImage(vtkImageData* imageData)
{
  if (!imageData || !imageData->GetPointData() || !imageData->GetPointData()->GetScalars() ||
      imageData->GetScalarType() != VTK_UNSIGNED_CHAR)
  {
    return QImage();
  }

  int width = imageData->GetDimensions()[0];
  int height = imageData->GetDimensions()[1];
  vtkIdType numberOfScalarComponents = imageData->GetNumberOfScalarComponents();
  QImage image;
  QImage::Format format;
  if (numberOfScalarComponents == 3)
  {
    format = QImage::Format_RGB888;
  }
  else if (numberOfScalarComponents == 4)
  {
    format = QImage::Format_RGBA8888;
  }
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
  else if (numberOfScalarComponents == 1)
  {
    format = QImage::Format_Grayscale8;
  }
#endif
  else
  {
    // unsupported pixel format
    return QImage();
  }
  image = QImage(static_cast<uint8_t*>(imageData->GetScalarPointer(0, 0, 0)), width, height,
                 width * numberOfScalarComponents, format);
  return image.mirrored();
}

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 7, 2023, 12:41am UTC](https://discourse.slicer.org/t/ctk-vtkimagedatatoqimage-generates-weird-results/25881/3 "2023-01-07T00:41:32Z")

</div>

Thank you, your fix was integrated into CTK at

> <https://github.com/commontk/CTK/commit/dec834fccffebdc3b0896c157d39e3c0031c4a0a>
>
> Bytes per line must be specified to make sure the image is always converted corr…ectly.
> 
> The problem was reported and solution was proposed here:
> https://discourse.slicer.org/t/ctk-vtkimagedatatoqimage-generates-weird-results/25881/2
