# How to tell if a vtkImageData object contains any pixel/voxel data

**URL:** https://discourse.vtk.org/t/how-to-tell-if-a-vtkimagedata-object-contains-any-pixel-voxel-data/2765
**Category:** Support
**Created:** [March 5, 2020, 8:54pm UTC](https://discourse.vtk.org/t/how-to-tell-if-a-vtkimagedata-object-contains-any-pixel-voxel-data/2765 "2020-03-05T20:54:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![MichelleKline](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michellekline/32/2155_2.png) [@MichelleKline](https://discourse.vtk.org/u/MichelleKline)
#### Post date: [March 5, 2020, 8:54pm UTC](https://discourse.vtk.org/t/how-to-tell-if-a-vtkimagedata-object-contains-any-pixel-voxel-data/2765/1 "2020-03-05T20:54:38Z")

</div>

Hello,  
What is the most straight-forward way to determine whether a vtkImageData object is “empty”. In other words, whether or not it contains any pixel/voxel data?  
Thank you,  
Michelle K.

---

<div class="post-metadata">

### Author: ![Ron84](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ron84/32/630_2.png) [@Ron84](https://discourse.vtk.org/u/Ron84)
#### Post date: [March 5, 2020, 9:31pm UTC](https://discourse.vtk.org/t/how-to-tell-if-a-vtkimagedata-object-contains-any-pixel-voxel-data/2765/2 "2020-03-05T21:31:18Z")

</div>

What’s with [GetNumberOfCells](https://vtk.org/doc/nightly/html/classvtkImageData.html#a9c9073b9aa7f5fa0329dc41d81cb3d95)?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [March 5, 2020, 11:36pm UTC](https://discourse.vtk.org/t/how-to-tell-if-a-vtkimagedata-object-contains-any-pixel-voxel-data/2765/3 "2020-03-05T23:36:02Z")

</div>

In 3D Slicer, we usually check `(extent[0] <= extent[1] && extent[2] <= extent[3] && extent[4] <= extent[5]) `. This is what vtkImageData::GetNumberOfCells() computes, too.

Even if the extent is non-empty, it is still possible that voxel data is not allocated yet, so then we also need to check `(image->GetPointData() != nullptr) && (imageDataScalar->GetPointData()->GetScalars() != nullptr)`. This is particularly important, because at many places in VTK, `GetPointData()->GetScalars()` is called without checking if `GetPointData() != nullptr`, leading to crashes if you set an empty image as input.

---

<div class="post-metadata">

### Author: ![MichelleKline](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michellekline/32/2155_2.png) [@MichelleKline](https://discourse.vtk.org/u/MichelleKline)
#### Post date: [March 5, 2020, 11:38pm UTC](https://discourse.vtk.org/t/how-to-tell-if-a-vtkimagedata-object-contains-any-pixel-voxel-data/2765/4 "2020-03-05T23:38:11Z")

</div>

This is what I was looking for. Thanks Andras!

---

<div class="post-metadata">

### Author: ![MichelleKline](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michellekline/32/2155_2.png) [@MichelleKline](https://discourse.vtk.org/u/MichelleKline)
#### Post date: [March 5, 2020, 11:42pm UTC](https://discourse.vtk.org/t/how-to-tell-if-a-vtkimagedata-object-contains-any-pixel-voxel-data/2765/5 "2020-03-05T23:42:18Z")

</div>

Thanks, Ronald.
