tif 图片 转 vtkImageData 格式

#include "image.h"
#include <string>
#include <vtkXMLImageDataWriter.h>
int main(int, char *[])
{
	uint16 *nImage;
	int width = 0;
	int height = 0;
	int depth = 0;
	int *size = new int[3];
	std::string path = "D:/image/0.tif";
	Image *Im = new Image();
	Im->read_3d_buffer_16bit(path.c_str(), nImage, width, height, depth);
	size[0] = width;
	size[1] = height;
	size[2] = depth;
	// Create an image data
	vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
	imageData->SetSpacing(1.0, 1.0, 1.0);
	imageData->SetOrigin(0.0, 0.0, 0.0);
	imageData->SetDimensions(size);
	imageData->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
	int* dims = imageData->GetDimensions();
	for (int z = 0; z < dims[2]; z++)
	{
		for (int y = 0; y < dims[1]; y++)
		{
			for (int x = 0; x < dims[0]; x++)
			{
				unsigned short* pixel = static_cast<unsigned short*>(imageData->GetScalarPointer(x, y, z));
				pixel[0] = nImage[z*dims[0] * dims[1] + y*dims[0] + x];
			}
		}
	}
	vtkSmartPointer<vtkXMLImageDataWriter> writer = vtkSmartPointer<vtkXMLImageDataWriter>::New();
	writer->SetFileName("test.vti");
	writer->SetInputData(imageData);
	writer->Write();
	return EXIT_SUCCESS;
	// Access the data linearly
	/*
	vtkSmartPointer<vtkImageData> image =
	vtkSmartPointer<vtkImageData>::New();
	image->SetExtent(0,1,0,1,0,0);
	image->SetScalarTypeToInt();
	image->SetNumberOfScalarComponents(1);
	int* pixel;
	pixel = static_cast<int*>(image->GetScalarPointer(0,0,0));
	pixel[0] = 1;
	pixel = static_cast<int*>(image->GetScalarPointer(1,0,0));
	pixel[0] = 2;
	pixel = static_cast<int*>(image->GetScalarPointer(0,1,0));
	pixel[0] = 3;
	pixel = static_cast<int*>(image->GetScalarPointer(1,1,0));
	pixel[0] = 4;
	vtkIntArray* scalars = vtkIntArray::SafeDownCast(image->GetPointData()->GetArray("ImageScalars"));
	std::cout << "Scalars has " << scalars->GetNumberOfComponents() << " components" << std::endl;
	std::cout << "Scalars has " << scalars->GetNumberOfTuples() << " tuples" << std::endl;
	for(vtkIdType i = 0; i < scalars->GetNumberOfTuples(); i++)
	{
	std::cout << scalars->GetValue(i) << std::endl;
	}
	*/
	return EXIT_SUCCESS;
}

内存拷贝

#include <iostream>
#include <vtkImageData.h>
#include <vtkFloatArray.h>
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageData.h"
#include "vtkUnsignedShortArray.h"
#include "vtkContourFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkPointData.h"
#include "image.h"
#include <string>
#include <vtkXMLImageDataWriter.h>
int main()
{

	uint16 *nImage;
	int width = 0;
	int height = 0;
	int depth = 0;
	int *size = new int[3];
	std::string path = "D:/image/0.tif";
	Image *Im = new Image();
	Im->read_3d_buffer_16bit(path.c_str(), nImage, width, height, depth);
	size[0] = width;
	size[1] = height;
	size[2] = depth;
	// Create an image data
	vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
	vtkUnsignedShortArray* array = vtkUnsignedShortArray::New();
	array->SetArray(nImage, size[0] * size[1] * size[2], 1);
	imageData = vtkImageData::New();
	/*auto info = vtkSmartPointer<vtkInformation>::New();*/
	imageData->GetPointData()->SetScalars(array);
	imageData->SetDimensions(size);
	/*imageData->SetScalarType(VTK_UNSIGNED_SHORT,imageData->GetInformation());*/
	imageData->SetSpacing(1.0, 1.0, 1.0);
	imageData->SetOrigin(0.0, 0.0, 0.0);
	vtkSmartPointer<vtkXMLImageDataWriter> writer = vtkSmartPointer<vtkXMLImageDataWriter>::New();
	writer->SetFileName("test.vti");
	writer->SetInputData(imageData);
	writer->Write();
	//int i, j, k, kOffset, jOffset, offset;
	//float x, y, z, s, sp;

	//vtkRenderer *renderer = vtkRenderer::New();
	//vtkRenderWindow *renWin = vtkRenderWindow::New();
	//renWin->AddRenderer(renderer);
	//vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
	//iren->SetRenderWindow(renWin);

	//vtkImageData *vol = vtkImageData::New();
	//vol->SetDimensions(26, 26, 26);
	//vol->SetOrigin(-0.5, -0.5, -0.5);
	//sp = 1.0 / 25.0;
	//vol->SetSpacing(sp, sp, sp);

	//vtkFloatArray *scalars = vtkFloatArray::New();
	//for (k = 0; k < 26; k++)
	//{
	//	z = -0.5 + k*sp;
	//	kOffset = k * 26 * 26;
	//	for (j = 0; j < 26; j++)
	//	{
	//		y = -0.5 + j*sp;
	//		jOffset = j * 26;
	//		for (i = 0; i < 26; i++)
	//		{
	//			x = -0.5 + i*sp;
	//			s = x*x + y*y + z*z - (0.4*0.4);
	//			offset = i + jOffset + kOffset;
	//			scalars->InsertTuple1(offset, s);
	//		}
	//	}
	//}
	//vol->GetPointData()->SetScalars(scalars);
	//scalars->Delete();
	//vtkContourFilter *contour = vtkContourFilter::New();
	//contour->SetInput(vol);
	//contour->SetValue(0, 0.0);

	//vtkPolyDataMapper *volMapper = vtkPolyDataMapper::New();
	//volMapper->SetInput(contour->GetOutput());
	//volMapper->ScalarVisibilityOff();
	//vtkActor *volActor = vtkActor::New();
	//volActor->SetMapper(volMapper);

	//renderer->AddActor(volActor);
	//renderer->SetBackground(1, 1, 1);
	//renWin->SetSize(450, 450);

	 interact with data
	//renWin->Render();
	//iren->Start();

	 Clean up
	//renderer->Delete();
	//renWin->Delete();
	//iren->Delete();
	//vol->Delete();
	//scalars->Delete();
	//contour->Delete();
	//volMapper->Delete();
	//volActor->Delete();

	//return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值