I am using VTK 5.10.1 with VS 2010, and the following example does not work on my machine:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/ImageData/IterateImageData
The error said:
vtkImageData [009B92A8]: Bad component index 1302176011
I am not sure why this happened, and it should work since it is from the official document. However, the reality is that it does not work on my machine, neither GetScalarComponentAsFloat() or GetScalarComponentAsDouble() will work.
So if we want to get the pixel data from vtkImageData, we need to find another way to do it. And we can get the data by accessing vtkDataArray, please see the following example code:
#include <vtkVersion.h> #include <vtkSmartPointer.h> #include <vtkImageData.h> #include <vtkPointData.h> #include "vtkBMPReader.h" int main(int, char *[]) { char * filename = "img.bmp"; if( !filename || strlen(filename) == 0 ) { return -1; } vtkBMPReader * reader = vtkBMPReader::New(); reader->SetFileName(filename); reader->Update(); vtkSmartPointer<vtkImageData> image_data = reader->GetOutput(); int* dims = image_data->GetDimensions(); std::cout << "Dims: " << " x: " << dims[0] << " y: " << dims[1] << " z: " << dims[2] << std::endl; std::cout << "Number of points: " << image_data->GetNumberOfPoints() << std::endl; std::cout << "Number of cells: " << image_data->GetNumberOfCells() << std::endl; std::cout << "Number of scalar components: " << image_data->GetNumberOfScalarComponents() << std::endl; vtkDataArray *arr = image_data->GetPointData()->GetArray(0); // Retrieve the entries from the image data and print them to the screen for (int z = 0; z < dims[2]; z++) { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { /* Change this double* pixel = static_cast<double*>(imageData->GetScalarPointer(x,y,z)); // do something with v std::cout << pixel[0] << " "; */ double d[3]; arr->GetTuple(y * dims[0] + x, d); std::cout << d[0] << " "; } std::cout << std::endl; } std::cout << std::endl; } return EXIT_SUCCESS; }
Read More:
- [Solved] Error(22) WARNING: IPv4 forwarding is disabled. Networking will not work.
- Gitlab nginx wont start to work. [down: nginx: 1s, normally up, want up; run: log: (pid 20128)xxx]
- hint: Updates were rejected because the remote contains work that you do To XXX
- PCL 1.8.1 VTK 9.0 QT 5.14.9 [How to Solve]
- Keras’ print model error: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work.
- [Solved] Qt Vtk wglMakeCurrent failed in MakeCurrent(), error: The handle is invalid.
- How to Solve vtk error (vtkInteractorStyleSwitchBase & vtkWin32OpenGLRenderWin Error)
- AUC Error – ValueError: Data is not binary and pos_label is not specified
- Abnormal [System.InvalidOperationException: custom type mapping for ‘xxx’ is not specified or is not a solution
- [Solved] Mac M1 Debug Error: could not launch process: can not run under Rosetta
- [Solved] Error: Main class not found or could not be loaded com.jawasoft.
- Postgis Install Error: could not load library “C:\……“:The specified module could not be found.
- Rendering Problems The following classes could not be instantiated: xml layout file could not be loaded
- Go declares that the local variable does not use command line arguments. Main. Go: 4:6: a declared but not used
- Error using tensorflow GPU: could not create cudnn handle: cudnn_STATUS_NOT_INITIALIZED
- [Solved] Hikvision SDK: NET_DVR_GetDVRConfig failed Device does not support this function
- [Solved] Could not identify launch activity: Default Activity not found Error while Launching activity
- [Solved] Could not identify launch activity: Default Activity not found Error while Launching activity
- Could not identify launch activity: Default Activity not foundError while Launching activity
- Readiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to