# Determine percentage of visible background

**URL:** https://discourse.vtk.org/t/determine-percentage-of-visible-background/3749
**Category:** Support
**Created:** [July 10, 2020, 7:52pm UTC](https://discourse.vtk.org/t/determine-percentage-of-visible-background/3749 "2020-07-10T19:52:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![woodscn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/woodscn/32/1738_2.png) [@woodscn](https://discourse.vtk.org/u/woodscn)
#### Post date: [July 10, 2020, 7:52pm UTC](https://discourse.vtk.org/t/determine-percentage-of-visible-background/3749/1 "2020-07-10T19:52:12Z")

</div>

I need to determine what percentage of a given rendered image is background. Is there a simple way I can query that?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [July 11, 2020, 2:37pm UTC](https://discourse.vtk.org/t/determine-percentage-of-visible-background/3749/2 "2020-07-11T14:37:48Z")

</div>

Hello, Nathan,

Do you need an accurate number or a fair estimation +/- some error?

If the latter is your case, you can simply traverse the viewport every, say, 10 columns/rows, and do a picking test. If the pick returns an actor, something is visible there. Doing this over the entire render area can give you a good enough proportion of background/non-background. Of course, you could do picking on every pixel, but that may hinder performance.

cheers,

Paulo

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [July 11, 2020, 2:46pm UTC](https://discourse.vtk.org/t/determine-percentage-of-visible-background/3749/3 "2020-07-11T14:46:27Z")

</div>

Also, if you know the background color beforehand, you can capture the viewport’s pixmap/image and test the pixels. If you have a, for example, black pixel, then it is background.

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [July 13, 2020, 7:02am UTC](https://discourse.vtk.org/t/determine-percentage-of-visible-background/3749/4 "2020-07-13T07:02:31Z")

</div>

You can also render to a vtkImageData with alpha support and count how many pixels have alpha=0.  
See `vtkWindowToImageFilter::SetInputBufferTypeToRGBA()`

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [July 13, 2020, 3:35pm UTC](https://discourse.vtk.org/t/determine-percentage-of-visible-background/3749/5 "2020-07-13T15:35:12Z")

</div>

There isn’t anything in VTK that I am aware of that will compute this for you automatically.

---

<div class="post-metadata">

### Author: ![woodscn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/woodscn/32/1738_2.png) [@woodscn](https://discourse.vtk.org/u/woodscn)
#### Post date: [July 28, 2020, 8:26pm UTC](https://discourse.vtk.org/t/determine-percentage-of-visible-background/3749/6 "2020-07-28T20:26:30Z")

</div>

Thank you all for the help. It took some doing, but I was able to do it by comparing with the background color.
