# Extract pixel values from reformatted red slice

**URL:** https://discourse.slicer.org/t/extract-pixel-values-from-reformatted-red-slice/29004
**Category:** Support
**Created:** [April 19, 2023, 10:40am UTC](https://discourse.slicer.org/t/extract-pixel-values-from-reformatted-red-slice/29004 "2023-04-19T10:40:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)
#### Post date: [April 19, 2023, 10:40am UTC](https://discourse.slicer.org/t/extract-pixel-values-from-reformatted-red-slice/29004/1 "2023-04-19T10:40:18Z")

</div>

Hi,

We want to extract pixel values from reformatted red slice to obtain a perpendicular view. To do that, we use endoscopy module in the following way:

```auto
    # Get curve info 
    curveNode = slicer.util.getNode(curveName)
    pointsCurve = vtk.util.numpy_support.vtk_to_numpy(curveNode.GetCurve().GetPoints().GetData())
    modelNode = slicer.util.getNode("model_" + curveName)

    # Set endoscopy params
    pointsEndoscopy = slicer.util.arrayFromModelPoints(modelNode)    
    endoscopyGUI = slicer.modules.endoscopy.widgetRepresentation().self()
    endoscopyGUI.flyTo(0)
   
    # Storage vectors
    volumeNodes_slices = []
    voxels_slices = []
    curvePoints = []

    for index in range(0, len(pointsEndoscopy)-1): 
        
        sliceNodeID = "vtkMRMLSliceNodeRed"
        
        # Get image data from slice view
        sliceNode = slicer.mrmlScene.GetNodeByID(sliceNodeID)
        appLogic = slicer.app.applicationLogic()
        sliceLogic = appLogic.GetSliceLogic(sliceNode)
        sliceLayerLogic = sliceLogic.GetBackgroundLayer()
        reslice = sliceLayerLogic.GetReslice()
        reslicedImage = vtk.vtkImageData()
        reslicedImage.DeepCopy(reslice.GetOutput())
        
        # Create new volume node using resliced image
        volumeNodeResliced = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode")
        volumeNodeResliced.SetIJKToRASMatrix(sliceNode.GetXYToRAS())
        volumeNodeResliced.SetAndObserveImageData(reslicedImage)
        volumeNodeResliced.CreateDefaultDisplayNodes()
        volumeNodeResliced.CreateDefaultStorageNode()
        
        volumeNodes_slices.append(volumeNodeResliced)
        
        # Get voxels as a numpy array
        voxels = slicer.util.arrayFromVolume(volumeNodeResliced)
        voxels_slices.append(voxels)

        # Save voxels as .npy

```

The problem is that pixel values in .npy file do not match with the values in slicer.  
For example:

 ![imagen](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/d/4d166f90fdb14f4d149d6549a66be4e2be92a841.png) ![imagen](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/1/d13e230bec44c7fe52c09f9af139c1ba3315da29.png)

Left: .npy file → HU = 470 in vessel center  
Right: red slice → HU = 419 in vessel center

---

<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: [April 20, 2023, 4:31am UTC](https://discourse.slicer.org/t/extract-pixel-values-from-reformatted-red-slice/29004/2 "2023-04-20T04:31:29Z")

</div>

If you computed the coordinates correctly then probably the difference is due to interpolation. You can use nearest neighbor instead of interpolation if you want to see exactly matching values.

However, if you want to extract orthogonal slices along a curve then you don’t need to implement any new computations. Instead, you can use Curved Planar Reformat module in the Sandbox extension. It gives you all the reformatted slices conveniently in a 3D array.

---

<div class="post-metadata">

### Author: ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)
#### Post date: [April 20, 2023, 7:07am UTC](https://discourse.slicer.org/t/extract-pixel-values-from-reformatted-red-slice/29004/3 "2023-04-20T07:07:33Z")

</div>

Yes, it was due to interpolation.

However, we have observed another strange behaviour. The images saved as .npy with name “index” correspond to a flyTo(index+1) of the endoscopy module and we don’t know why there is no index correspondence (see code in the previous post).

Thanks

---

<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: [April 20, 2023, 11:45am UTC](https://discourse.slicer.org/t/extract-pixel-values-from-reformatted-red-slice/29004/4 "2023-04-20T11:45:48Z")

</div>

Rendering pipeline update is limited by the screen refresh rate, while data may be modified thousands of times per second. If you capture data from the rendering pipeline then make sure you call `slicer.util.forceRenderAllViews()` before. The rendering pipeline is not intended for acquiring data for quantitative analysis, as it is optimized for visualization. Currently, there are not significant limitations (other than lower refresh rate and image resolution and field of view limited to display resolution at later stages of the pipeline, and your data potentially impacted by user’s visualization choices such as interpolation/nearest neighbor), but in the future we may implement features like dynamically lowering of rendering resolution when visualizing very large data sets, etc.

Reslicing/resampling the original image is a more robust way of extracting pixel values from reformatted slices. The non-linear transform based method that [Curved Planar Reformat](https://github.com/PerkLab/SlicerSandbox/blob/master/CurvedPlanarReformat/CurvedPlanarReformat.py) module uses is particularly powerful because it allows transforming any nodes between the world coordinate system and the straightened coordinate system.
