VTK实现单层图片的提取方法有很多,可以使用的filter有很多。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main(int, char*[])
{
string fileName = "test.nii.gz";
vtkSmartPointer<vtkImageData> input;
vtkSmartPointer <vtkNIFTIImageReader> niftireader = vtkSmartPointer<vtkNIFTIImageReader>::New();
niftireader->SetFileName(fileName.c_str());
niftireader->Update();
input = niftireader->GetOutput();
vtkMatrix4x4* qMatrix = niftireader->GetQFormMatrix();
qMatrix->Print(std::cout);
int inputDims[3] = { 0 };
int extent[6] = {0};
double origin[3] = { 0 };
double spacing[3] = { 0 };
vtkNew<vtkMatrix3x3> mat;
input->GetDimensions(inputDims);
input->GetExtent(extent);
input->GetOrigin(origin);
input->GetSpacing(spacing);
vtkSmartPointer<vtkImageReslice> pReslice = vtkSmartPointer<vtkImageReslice>::New();
//pReslice->SetOutputOrigin(origin);
double center[3];
center[0] = origin[0] /*+ spacing[0] * 0.5 * (extent[0] + extent[1])*/;
center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);
center[2] = origin[2] /*+ spacing[2] * 0.5 * (extent[4] + extent[5])*/;
static double axialElements[16] = {
0, 1, 0, 0,
0, 0, 1, 0,
1, 0, 0, 0,
0, 0, 0, 1
};
vtkSmartPointer<vtkMatrix4x4> resliceAxes =
vtkSmartPointer<vtkMatrix4x4>::New();
resliceAxes->DeepCopy(axialElements);
resliceAxes->SetElement(0, 3, center[0]);
resliceAxes->SetElement(1, 3, center[1]);
resliceAxes->SetElement(2, 3, center[2]);
//pReslice->SetOutputExtent(ext);
pReslice->SetOutputDimensionality(2);
pReslice->SetResliceAxes(resliceAxes);
pReslice->SetAutoCropOutput(1);//!!!一定要加这句话
pReslice->SetInputData(input);
pReslice->Update();
vtkImageData* extracted = pReslice->GetOutput();
//extracted->SetOrigin(origin);
int* extractedDims = extracted->GetDimensions();
std::cout << "Dims: "
<< " x: " << extractedDims[0] << " y: " << extractedDims[1]
<< " z: " << extractedDims[2] << std::endl;
std::cout << "Number of points: " << extracted->GetNumberOfPoints()
<< std::endl;
std::cout << "Number of cells: " << extracted->GetNumberOfCells()
<< std::endl;
string dstfileName = "resliced.nii.gz";
vtkSmartPointer<vtkNIFTIImageWriter> envImageWriter = vtkSmartPointer<vtkNIFTIImageWriter>::New();
envImageWriter->SetInputData(extracted);
//envImageWriter->SetQFormMatrix(qMatrix);
envImageWriter->SetFileName(dstfileName.c_str());
envImageWriter->Write();
return EXIT_SUCCESS;
}
double center[3];
center[0] = origin[0] /*+ spacing[0] * 0.5 * (extent[0] + extent[1])*/;
center[1] = origin[1] /*+ spacing[1] * 0.5 * (extent[2] + extent[3])*/;
center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);
static double axialElements[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
提取中心层片,x轴切片
double center[3];
center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);
center[1] = origin[1] /*+ spacing[1] * 0.5 * (extent[2] + extent[3])*/;
center[2] = origin[2] /*+ spacing[2] * 0.5 * (extent[4] + extent[5])*/;
static double axialElements[16] = {
0, 0, 1, 0,
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 1
};
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main(int, char*[])
{
string fileName = "D:/Dataset/Hip_Femur_Detection/Detect/BJ_02006.nii.gz";
vtkSmartPointer<vtkImageData> input;
vtkSmartPointer <vtkNIFTIImageReader> niftireader = vtkSmartPointer<vtkNIFTIImageReader>::New();
niftireader->SetFileName(fileName.c_str());
niftireader->Update();
input = niftireader->GetOutput();
vtkMatrix4x4* qMatrix = niftireader->GetQFormMatrix();
qMatrix->Print(std::cout);
int inputDims[3] = { 0 };
int extent[6] = {0};
double origin[3] = { 0 };
double spacing[3] = { 0 };
vtkNew<vtkMatrix3x3> mat;
input->GetDimensions(inputDims);
input->GetExtent(extent);
input->GetOrigin(origin);
input->GetSpacing(spacing);
vtkNew<vtkExtractVOI> extractVOI;
extractVOI->SetInputData(input);
extractVOI->SetVOI(0, inputDims[0]-1,
0, inputDims[1]-1, inputDims[2] / 2, inputDims[2] / 2);
extractVOI->Update();
vtkImageData* extracted = extractVOI->GetOutput();
//extracted->SetOrigin(origin);
int* extractedDims = extracted->GetDimensions();
std::cout << "Dims: "
<< " x: " << extractedDims[0] << " y: " << extractedDims[1]
<< " z: " << extractedDims[2] << std::endl;
std::cout << "Number of points: " << extracted->GetNumberOfPoints()
<< std::endl;
std::cout << "Number of cells: " << extracted->GetNumberOfCells()
<< std::endl;
string dstfileName = "extractVOI.nii.gz";
vtkSmartPointer<vtkNIFTIImageWriter> envImageWriter = vtkSmartPointer<vtkNIFTIImageWriter>::New();
envImageWriter->SetInputData(extracted);
envImageWriter->SetQFormMatrix(qMatrix);
envImageWriter->SetFileName(dstfileName.c_str());
envImageWriter->Write();
return EXIT_SUCCESS;
}
extractVOI->SetVOI(inputDims[0] / 2, inputDims[0]/2,
0, inputDims[1]-1, 0, inputDims[2]-1);