Erictoo6
2022-02-20 12:41
采纳率: 66.7%
浏览 42

vtk读取dicom文件提取像素矩阵


使用vtk读取dicom文件后,想将像素值信息提取出来用OpenCV处理,但是卡在了不知道怎么提取像素值信息。尝试了下述代码

#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkImageCast.h>
#include <vtkDICOMImageReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkAutoInit.h>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <typeinfo>
#include <iostream>
VTK_MODULE_INIT(vtkRenderingOpenGL2)

using namespace cv;
using namespace std;

void dicomread(string inputFilename, Mat& img, vtkSmartPointer<vtkDICOMImageReader>& reader);

int main(int argc, char* argv[])
{
    // Verify input arguments
    //if (argc != 2)
    //{
    //  std::cout << "Usage: " << argv[0]
    //      << " Filename(.img)" << std::endl;
    //  return EXIT_FAILURE;
    //}

    std::string inputFilename = "C:\\Users\\26327\\Desktop\\dll4sw\\data\\SE3\\IM0.dcm";
    Mat img;
    // Read all the DICOM files in the specified directory.
    vtkSmartPointer<vtkDICOMImageReader> reader =
        vtkSmartPointer<vtkDICOMImageReader>::New();
    reader->SetFileName(inputFilename.c_str());
    reader->Update();
    dicomread(inputFilename, img, reader);
    // Visualize
    vtkSmartPointer<vtkImageViewer2> imageViewer =
        vtkSmartPointer<vtkImageViewer2>::New();
    imageViewer->SetInputConnection(reader->GetOutputPort());
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    imageViewer->SetupInteractor(renderWindowInteractor);
    imageViewer->Render();
    imageViewer->GetRenderer()->ResetCamera();
    imageViewer->Render();

    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

void dicomread(string inputFilename, Mat& img, vtkSmartPointer<vtkDICOMImageReader>& reader)
{
    img.create(512, 512, CV_32SC1);

    vtkSmartPointer<vtkImageCast> imageCast =
        vtkSmartPointer<vtkImageCast>::New();

    reader->SetFileName(inputFilename.c_str());
    vtkImageData* p;
    reader->Update();
    cout << typeid(reader->GetOutput()).name() << endl;

    imageCast->SetInputConnection(reader->GetOutputPort());
    imageCast->SetOutputScalarTypeToInt();
    imageCast->Update();

    int dims[3];
    reader->GetOutput()->GetDimensions(dims);

    for (int k = 0; k < dims[2]; k++)
    {
        for (int j = 0; j < dims[1]; j++)
        {
            for (int i = 0; i < dims[0]; i++)
            {
                int* pixel =
                    (int*)(imageCast->GetOutput()->GetScalarPointer(i, j, k)); 
                img.at<int>(j, i) = int(*pixel); 
            }
        }
    }
}

但是这两行都报错,报错信息都是“pointer to incomplete class type "vtkImageData" is not allowed"。请问如何解决


    reader->GetOutput()->GetDimensions(dims);

    int* pixel = (int*)(imageCast->GetOutput()->GetScalarPointer(i, j, k)); 

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题