VTK 灰度图像转RGB(非渲染):vtkWindowLevelLookupTable/vtkLookupTable/vtkImageMapToWindowLevelColors

简介:系统中提取读取序列图像中的一张,用VTK将灰度图像转换为GRB,然后用GDI+绘图。懒得直接做颜色查找表,直接用VTK中的 vtkWindowLevelLookupTable/vtkLookupTable/vtkImageMapToWindowLevelColors,可完成灰度到RGB的转换。

VTK 灰度图像转RGB(非渲染):vtkWindowLevelLookupTable/vtkLookupTable/vtkImageMapToWindowLevelColors

 保存为RGB的文件如上图,代码如下:

void Gray2ColorImg()
{
	m_Origin[0] = -m_Dim[0] * m_Spacing[0] * 0.5;
	m_Origin[1] = -m_Dim[1] * m_Spacing[1] * 0.5;
	m_Origin[2] = -m_Dim[2] * m_Spacing[2] * 0.5;

	size_t fSize = m_Dim[0] * m_Dim[1] * m_Dim[2] * sizeof(short);

	//vtkImageData* input = vtkImageData::New();
	vtkNew<vtkImageData> input;
	input->SetDimensions(m_Dim[0], m_Dim[1], m_Dim[2]);
	input->SetSpacing(m_Spacing[0], m_Spacing[1], m_Spacing[2]);
	input->SetOrigin(m_Origin);
	input->AllocateScalars(VTK_SHORT, 1);
	void *ptr = input->GetScalarPointer();


	std::string m_ImgFile = "D:\\IMG-0001-00001.img";
	FILE *pFile = fopen(m_ImgFile.c_str(), "rb");
	if (pFile == NULL)
		return;

	fread(ptr, 1, fSize, pFile);
	fclose(pFile);


	long iCount = m_Dim[0] * m_Dim[1];
	// Create a greyscale lookup table
	vtkNew<vtkWindowLevelLookupTable> table;
	table->SetWindow(2326);
	table->SetLevel(180);
	table->Build();
	table->BuildSpecialColors();

	void *outPtr =malloc(iCount * 3);
	unsigned char *rgbPtr = (unsigned char *)outPtr;

	int windows = 2000;
	//for (int i = 0; i < 4; ++i)
	{
		//windows += 1000;
		//table->SetWindow(windows);
		table->MapScalarsThroughTable2(ptr, rgbPtr, VTK_UNSIGNED_SHORT, iCount, VTK_LUMINANCE, VTK_RGB);

		pFile = fopen("color2.img", "wb+");
		if (!pFile)
			return;

		fwrite(outPtr, 1, iCount * 3, pFile);
		fclose(pFile);
	}


	free(outPtr);

}

上一篇:RAC修改spfile位置


下一篇:《设计模式面试小炒》策略和工厂模式替代业务场景中复杂的ifelse