Random horizontal lines when displaying image


I get a strange behavior when displaying an image in vtk.
At random the image displays horizontal lines when being displayed.
Has anybody encountered this bug before.
The code (slightly stripped) looks like this:

// CWndVTK* m_pWndVTK; //Initialized elswhere

vtkSmartPointer table = vtkSmartPointer::New();
table->SetRange(0, 255); // image intensity range
table->SetValueRange(0.0, 1.0); // from black to white
table->SetSaturationRange(0.0, 0.0); // no color saturation
table->SetRampToLinear();
table->Build();

vtkSmartPointer m_imgActor_0 = vtkSmartPointer::New();
vtkSmartPointer imgImport_0 = vtkSmartPointer::New();
vtkSmartPointer color_0 = vtkSmartPointer::New();

Size imageSize = Size(m_image_width, m_image_height);
void* imageBuffer_0 = malloc(m_image_width * m_image_height * sizeof(unsigned short));
imgImport_0->SetDataScalarTypeToUnsignedShort();
image_data_size = m_image_width * m_image_height * sizeof(unsigned short);

imgImport_0->CopyImportVoidPointer( imageBuffer_0, image_data_size );
imgImport_0->SetWholeExtent(0, m_image_width - 1, 0, m_image_height - 1, 0, 0);
imgImport_0->SetDataExtentToWholeExtent();
imgImport_0->UpdateWholeExtent();
imgImport_0->Update();

color_0->SetLookupTable(table);
color_0->SetInputConnection (imgImport_0->GetOutputPort());
color_0->Update();

m_imgActor_0->GetMapper()->SetInputConnection(imgImport_0->GetOutputPort());
m_imgActor_0->GetMapper()->SetInputConnection(color_0->GetOutputPort());

// Get data to display transform
CoordinateSystem^ cs = m_pWndVTK->displayTransform->ConvertFrom(dsImgMap->CoordinateSystem);
double* p = &(ForwardAffineMatrix);
// Move plane one hundredth of a pixel back, such that 3d objects in the same plane are not clipped by the image
p[11] -= Math::Abs(p[0]) / 100;

// Apply transform
vtkSmartPointer imageGridToDisplayTransform = vtkSmartPointer::New();
imageGridToDisplayTransform->SetMatrix(p);
m_imgActor_0->SetUserTransform(imageGridToDisplayTransform);
m_imgActor_0->SetInterpolate(m_info->useInterpolation);
m_imgActor_0->Update();
m_imgActor_0->Render(m_pWndVTK->GetRenderWindow()->GetRenderers()->GetFirstRenderer());

vtkSmartPointer colorTransferFunction = vtkSmartPointer::New();

for (int i = 0; i < paletteColors->Length; i++)
{
rgba[0] = paletteColors[i].R / 255.0f;
rgba[1] = paletteColors[i].G / 255.0f;
rgba[2] = paletteColors[i].B / 255.0f;

colorTransferFunction->AddRGBPoint(lookupHandler->GetLookupTableValueValue(i), rgba[0], rgba[1], rgba[2]);

}
color_0->SetLookupTable(colorTransferFunction);

m_pWndVTK->GetRenderWindow()->Render();

Please use GitLab Flavored Markdown (GLFM) | GitLab for code, otherwise it isn’t displayed properly (and some parts aren’t displayed at all, such as template parameter lists).

2 Likes

I see that you use malloc, which indicates that some error-prone C-style manual memory management is being used. Memory corruption (you don’t properly allocate or free memory areas) can lead to random lines to appear in images, so overall it is a quite likely root cause.

A slightly stripped code is somewhat informative but to be able to help effectively, we would need a complete example that reproduces the issue. Python is preferred because we can then simply copy-paste the example into a Python console and we don’t need to spend time with setting up a build.

Hi, Jesper,

Why do you seem to call m_imgActor_0->GetMapper()->SetInputConnection() twice in tandem passing two different objects?

Also, please, do as suggested above and put your code after a ```cpp and before a ``` to make it more readabale and with increased probability of getting help.

regards,

Paulo