VTK GetScalarPointer() and GetScalarComponentAsFloat() not work

简介:

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 erros 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;
}

本文转自博客园Grandyang的博客,原文链接:VTK GetScalarPointer() and GetScalarComponentAsFloat() not work,如需转载请自行联系原博主。

相关文章
clion中cpp文件显示This file does not belong to any project ,code insight features might not work【解决方案】
clion中cpp文件显示This file does not belong to any project ,code insight features might not work【解决方案】
clion中cpp文件显示This file does not belong to any project ,code insight features might not work【解决方案】
|
4月前
|
机器学习/深度学习 人工智能 算法
OpenCV(Open Source Computer Vision Library
OpenCV(Open Source Computer Vision Library,开源计算机视觉库)是一个开源的计算机视觉和机器学习软件库,它包含了许多图像处理、视频分析和计算机视觉方面的功能。OpenCV的目的是为人工智能、机器视觉、图像处理等领域的研究人员和开发者提供一个通用且高效的平台。
43 1
|
6月前
|
iOS开发 芯片 MacOS
[Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture...
[Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture...
128 0
|
12月前
|
IDE 开发工具 数据库
STM32bug【 KEILMDK中出现The Project references devices, files or libraries that are not installed】
STM32bug【 KEILMDK中出现The Project references devices, files or libraries that are not installed】
154 0
|
计算机视觉
Qt 配置好OpenCV运行报错 The process was ended forcefully. exe Crashed.
Qt 配置好OpenCV运行报错 The process was ended forcefully. exe Crashed.
408 0
Qt 配置好OpenCV运行报错 The process was ended forcefully. exe Crashed.
Qt&Vtk-023-MultiView
Qt&Vtk-023-MultiView
126 0
Qt&Vtk-023-MultiView
|
C++ Windows
编译WINDOWS版SDL2:You should run hg revert SDL_config.h
编译WINDOWS版SDL2:You should run hg revert SDL_config.h
482 0
|
C语言
Qt Creator新安装后运行一个程序后,出现错误:Error while building/deploying project dict-qt (kit: Desktop Qt 5.10.0 MinGW 32bit) When executing step "qmake"
1、环境介绍:在windows10 Pro下,当前Qt Creator版本,如下图所示: 2、问题描述:当用Qt Creator新建一个工程后,按Ctrl + R 构建/部署时,出现问题,问题截图如下: 3、解决方案:这是由于Qt Creator打开的工程文件夹的绝对路径中存在中文字符,只需将工...
5651 0