# vtkImageData: No scalar field has been specified-assuming 1 component

**URL:** <https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166>\
**Category:** Support\
**Created:** [July 11, 2021, 3:25am UTC](https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166 "2021-07-11T03:25:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)\
**Post date:** [July 11, 2021, 3:25am UTC](https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166/1 "2021-07-11T03:25:16Z")

</div>

I am using vmtk to obtain cpr image. But `vtkImageData` reported a bug.

My code is:

```auto

curvedMPRImageFilter = vtkvmtk.vtkvmtkCurvedMPRImageFilter()
curvedMPRImageFilter.SetInputData(vtkImg)
curvedMPRImageFilter.SetCenterline(centerline)
curvedMPRImageFilter.SetParallelTransportNormalsArrayName('ParallelTransportNormals')
curvedMPRImageFilter.SetFrenetTangentArrayName('FSTangents')
curvedMPRImageFilter.SetInplaneOutputSpacing(1, 1)
curvedMPRImageFilter.SetInplaneOutputSize(300, 300)
curvedMPRImageFilter.SetReslicingBackgroundLevel(0)
curvedMPRImageFilter.Update()

```

A bug is reported for `curvedMPRImageFilter.Update()`:

```auto
vtkImageData: No Scalar Field has been specified - assuming 1 component!

```

The `vtkImg` is converted from `numpy` data:

```auto
def numpyToVTK(data, multi_component=False, type='float'):

    flat_data_array = data.transpose(2,1,0).flatten()
    vtk_data = numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=vtk.VTK_FLOAT)
    shape = data.shape

    img = vtk.vtkImageData()
    img.GetPointData().SetScalars(vtk_data)
    img.SetDimensions(shape[0], shape[1], shape[2])
    return img

```

I can not find a solution for this bug. Any suggestion is appreciated~~

---

<div class="post-metadata">

**Author:** ![Kenichiro-Yoshimi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@Kenichiro-Yoshimi](https://discourse.vtk.org/u/Kenichiro-Yoshimi)\
**Post date:** [July 11, 2021, 5:56am UTC](https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166/2 "2021-07-11T05:56:55Z")

</div>

Hello,

What is displayed when you run the following?

```python
print(vtkImg.GetPointData())

```

---

<div class="post-metadata">

**Author:** ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)\
**Post date:** [July 12, 2021, 12:38am UTC](https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166/3 "2021-07-12T00:38:15Z")

</div>

The displayed information is:

```auto

Connected to pydev debugger (build 193.6911.25)
vtkPointData (0000026EFE343780)
  Debug: Off
  Modified Time: 2290
  Reference Count: 2
  Registered Events: 
    Registered Observers:
      vtkObserver (0000026E80B403E0)
        Event: 33
        EventName: ModifiedEvent
        Command: 0000026E80BC2CB0
        Priority: 0
        Tag: 1
  Number Of Arrays: 1
  Array 0 name = nullptr
  Number Of Components: 1
  Number Of Tuples: 52953088
  Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 )
  Interpolate Flags: ( 1 1 1 1 1 0 0 1 )
  Pass Through Flags: ( 1 1 1 1 1 1 1 1 )
  Scalars: 
    Debug: Off
    Modified Time: 2278
    Reference Count: 1
    Registered Events: (none)
    Name: (none)
    Data type: float
    Size: 52953088
    MaxId: 52953087
    NumberOfComponents: 1
    Information: 0000000000000000
    Name: (none)
    Number Of Components: 1
    Number Of Tuples: 52953088
    Size: 52953088
    MaxId: 52953087
    LookupTable: (none)
  Vectors: (none)
  Normals: (none)
  TCoords: (none)
  Tensors: (none)
  GlobalIds: (none)
  PedigreeIds: (none)
  EdgeFlag: (none)

```

---

<div class="post-metadata">

**Author:** ![Kenichiro-Yoshimi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@Kenichiro-Yoshimi](https://discourse.vtk.org/u/Kenichiro-Yoshimi)\
**Post date:** [July 12, 2021, 4:22am UTC](https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166/4 "2021-07-12T04:22:20Z")

</div>

Hello,

If you use the **SetActiveScalars** function in the **numpyToVTK** to set the active scalars, will it still solve the problem?

```python
def numpyToVTK(data, multi_component=False, type='float'):

    flat_data_array = data.transpose(2,1,0).flatten()
    vtk_data = numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=vtk.VTK_FLOAT)
    vtk_data.SetName('foo')
    shape = data.shape

    img = vtk.vtkImageData()
    img.GetPointData().SetScalars(vtk_data)
    img.GetPointData().SetActiveScalars('foo')
    img.SetDimensions(shape[0], shape[1], shape[2])
    return img

```

---

<div class="post-metadata">

**Author:** ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)\
**Post date:** [July 12, 2021, 5:06am UTC](https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166/5 "2021-07-12T05:06:49Z")

</div>

Sorry, but the bug is still reported. The complete code to reproduce this bug is:

```auto
import numpy as np
from vmtk import vtkvmtk
import vtk
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk

def numpyToVTKpoint(line):

    centerline = vtk.vtkPolyData()
    points = vtk.vtkPoints()
    lineCell = vtk.vtkCellArray()

    centerline.SetPoints(points)
    centerline.SetLines(lineCell)

    # centerline.GetPoints().SetNumberOfPoints(2)
    # centerline.GetLines().Reset()
    # ptIds = [0, 1]
    # centerline.GetLines().InsertNextCell(2, ptIds)

    ptIds = np.arange(line.shape[0]).tolist()
    centerline.GetPoints().SetNumberOfPoints(line.shape[0])
    centerline.GetLines().Reset()
    centerline.GetLines().InsertNextCell(len(ptIds), ptIds)
    for i in range(line.shape[0]):
        centerline.GetPoints().SetPoint(i, line[i, :])

    fsTangents = vtk.vtkDoubleArray()
    fsTangents.SetName('FSTangents')
    fsTangents.SetNumberOfTuples(1)
    fsTangents.InsertNextValue(1.0)
    fsTangents.InsertNextValue(1.0)
    fsTangents.InsertNextValue(1.0)
    centerline.GetPointData().AddArray(fsTangents)
    centerline.Modified()

    parallelTransportNormals = vtk.vtkDoubleArray()
    parallelTransportNormals.SetName("ParallelTransportNormals")
    parallelTransportNormals.SetNumberOfTuples(1)
    parallelTransportNormals.InsertNextValue(1.0)
    parallelTransportNormals.InsertNextValue(1.0)
    parallelTransportNormals.InsertNextValue(1.0)
    centerline.GetPointData().AddArray(parallelTransportNormals)
    centerline.Modified()

    return centerline

def numpyToVTK(data):

    flat_data_array = data.transpose(2,1,0).flatten()
    vtk_data = numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=vtk.VTK_FLOAT)
    vtk_data.SetName('foo')
    shape = data.shape

    img = vtk.vtkImageData()
    img.GetPointData().SetScalars(vtk_data)
    img.GetPointData().SetActiveScalars('foo')
    img.SetDimensions(shape[0], shape[1], shape[2])
    return img

def vtkToNumpy(data):
    temp = vtk_to_numpy(data.GetPointData().GetScalars())
    dims = data.GetDimensions()

    numpy_data = temp.reshape(dims[2], dims[1], dims[0])
    numpy_data = numpy_data.transpose(2,1,0)

    return numpy_data

image = np.zeros([256, 256, 256])
image[64:192, 64:192, :] = 1

x = np.ones(shape=[128])*128
y = np.ones(shape=[128])*128
z = np.arange(0, 128)+64
point = np.array([x, y, z]).T

centerline = numpyToVTKpoint(point)
vtkImg = numpyToVTK(image)

curvedMPRImageFilter = vtkvmtk.vtkvmtkCurvedMPRImageFilter()
curvedMPRImageFilter.SetInputData(vtkImg)
curvedMPRImageFilter.SetCenterline(centerline)
curvedMPRImageFilter.SetParallelTransportNormalsArrayName('ParallelTransportNormals')
curvedMPRImageFilter.SetFrenetTangentArrayName('FSTangents')
curvedMPRImageFilter.SetInplaneOutputSpacing(1, 1)
curvedMPRImageFilter.SetInplaneOutputSize(300, 300)
curvedMPRImageFilter.SetReslicingBackgroundLevel(0)
curvedMPRImageFilter.Update()
cprImg = curvedMPRImageFilter.GetOutput()
cprImg = vtkToNumpy(cprImg)

```

---

<div class="post-metadata">

**Author:** ![Kenichiro-Yoshimi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@Kenichiro-Yoshimi](https://discourse.vtk.org/u/Kenichiro-Yoshimi)\
**Post date:** [July 12, 2021, 8:57am UTC](https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166/6 "2021-07-12T08:57:57Z")

</div>

Hello,

This appears to be a bug in vmtk that was fixed on November 13, 2018.

> <https://github.com/vmtk/vmtk/commit/43fab892721ed540df36f088f25765c3ef4df3c2>

However, it is not reflected in the latest release version 1.4.0 (March 20, 2018).

Are you using version 1.4.0?

---

<div class="post-metadata">

**Author:** ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)\
**Post date:** [July 12, 2021, 9:18am UTC](https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166/7 "2021-07-12T09:18:03Z")

</div>

Yes, my vmtk version is 1.4.0. Because I can only download 1.4.0 vmtk. There is on other version unless I compile the vmtk from the source code.

---

<div class="post-metadata">

**Author:** ![yang\_liu](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yang_liu/32/8190_2.png) [@yang\_liu](https://discourse.vtk.org/u/yang_liu)\
**Post date:** [December 28, 2023, 5:40am UTC](https://discourse.vtk.org/t/vtkimagedata-no-scalar-field-has-been-specified-assuming-1-component/6166/8 "2023-12-28T05:40:28Z")

</div>

Hello, may I ask if your problem has been resolved?

“ERROR: In …/Common/DataModel/vtkImageData.cxx, line 1324  
vtkImageData (0x2110c60): No Scalar Field has been specified - assuming 1 component!”

I also encountered this error when performing surface reconstruction on coronary arteries, which has been bothering me for a long time. Looking forward to your reply. Thank you very much
