# Compute Gradient

**URL:** <https://discourse.slicer.org/t/compute-gradient/10642>\
**Category:** Support\
**Created:** [March 11, 2020, 1:37pm UTC](https://discourse.slicer.org/t/compute-gradient/10642 "2020-03-11T13:37:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)\
**Post date:** [March 11, 2020, 1:37pm UTC](https://discourse.slicer.org/t/compute-gradient/10642/1 "2020-03-11T13:37:46Z")

</div>

Hi;

I want to implement a new 3d reconstrcution method, in which I need to know gradient vector of each (i,j,k) voxel. The idea is to convert the binary label map to closed surface. The idea is similar to marching cube.

I know that I must apply vtkImageGradiant followed by VTKProbeFilter to get the gradint vector on each (i,j,k) voxel. But, It is wrong if I apply the vtkGradiantFilter on the LabelMap ( represented by 0 and 1 values). My question how can I recover the really scalar value of each voxel ( gray value for exemple of the segmented region) in order to apply the gradient filter on it .

I need some ideas, I implement the method in c++

---

<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:** [March 11, 2020, 1:44pm UTC](https://discourse.slicer.org/t/compute-gradient/10642/2 "2020-03-11T13:44:41Z")

</div>

If you need the gradient to estimate surface normal and you have a binary image then you need to first apply Gaussian smoothing filter on the labelmap.

Make sure that the smoothed results can be represented using the current scalar type and range of label values. For example, if you use int value as scalar type and labelmap values are 0 = background and 1 = foreground then you will get unusable results. You need to have label values of 0 and at least 100 (or use a float scalar type).

---

<div class="post-metadata">

**Author:** ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)\
**Post date:** [March 11, 2020, 1:49pm UTC](https://discourse.slicer.org/t/compute-gradient/10642/3 "2020-03-11T13:49:57Z")

</div>

I have applied gaussian smoothing filter on the labelMap. Then I have applied the gradient filter on the smoothed image, but the majority of normal vectors are null (0,0,0). I know that is wrong. But I don’t understand how can I make sure that the smoothed results can be represented using the current scalar type and range of labelMap? and how can I get the label values between 0 and 100 for exemple. are there some tests or expressions that i must use

---

<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:** [March 11, 2020, 1:52pm UTC](https://discourse.slicer.org/t/compute-gradient/10642/4 "2020-03-11T13:52:39Z")

</div>

You can use VTK filters for either rescaling intensity values or casting the image to a different scalar type.

---

<div class="post-metadata">

**Author:** ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)\
**Post date:** [March 11, 2020, 1:55pm UTC](https://discourse.slicer.org/t/compute-gradient/10642/5 "2020-03-11T13:55:58Z")

</div>

Thank you very much for your help, I can understand now why I have got wrong results

---

<div class="post-metadata">

**Author:** ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)\
**Post date:** [March 13, 2020, 10:20am UTC](https://discourse.slicer.org/t/compute-gradient/10642/6 "2020-03-13T10:20:48Z")

</div>

Hi Mr;  
I have got good results when I applied vtkImageCast on the labelMap but with SetInputConnection but with SetInputData , I had wrong model . However, SetInputConnection(labelMap) causes an error because in the argument I must add Something like data-\>GetOutPourPort() which I can not do with vtkimagedata (labelMap)

the probleme is that setInputdata(labelmap) does not work.  
with setInputConnection (labelMap) this is what is displayed (good results)

 ![imgg](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/2/62feb3256231a8dfb1006f92a99f2ca71a7c074b.jpeg)

but with setInputdata I have wrong model.

---

<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:** [March 14, 2020, 3:57pm UTC](https://discourse.slicer.org/t/compute-gradient/10642/7 "2020-03-14T15:57:48Z")

</div>

You have two options for setting input for a VTK filter:

- use `SetInputData` with a `vtkImageData` object as input (may be obtained by calling `Update` then `GetOutput` on a filter)
- use `SetInputConnection` with an algorithm output (typically obtained by calling `GetOutputPort` on a filter)

---

<div class="post-metadata">

**Author:** ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)\
**Post date:** [March 14, 2020, 4:11pm UTC](https://discourse.slicer.org/t/compute-gradient/10642/8 "2020-03-14T16:11:56Z")

</div>

Is exactly what i am doing, I have used SetInputData with vtkImageData but I have not got not good results. however when I have used SetInputConnection with vtkImage Data, I have got like in the Screenshot result. with an error because setInputconnection must not be used with vtkImageData. Is there a solution to convert vtkImageData to an algorithm output without affect data? for exemple I found vtkTrivialProducer, or vtkPassThroughFilter?
