2014-02-19 70 views
1

我一直在使用vtkImageData和一個組件進行體繪製。現在我想基於現有的vtkImageData創建一個新的vtkImageData。新的vtkImageData有兩個組件,第一個組件存儲標量數據與現有的相同,第二個組件存儲我將分配的數據。我的代碼片段是這樣的:vtkImageData如何存儲多個組件以及如何渲染它

// I read a series of dicom file: 
vtkImageData *originalData = reader->GetOutput(); 
int dim[3]; 
double spa[3], ori[3]; 
originalData->GetDimensions(dim); 
originalData->GetSpacing(spa); 
originalData->GetOrigin(ori); 

//newData is created based on the originalData's dimensions spacing and origin. 
vtkImageData *newData = vtkImagaData::New(); 
newData->SetDimensions(dim); 
newData->SetScalarTypeToShort(); 
newData->SetSpacing(spa); 
newData->SetNumberOfScalarComponents(2);//newData's component is two 
newData->SetOrigin(ori); 
newData->AllocateScalars(); 

//Now I have some puzzles:How does the vtkImageData store multiple components, I think it store data one point by one point, because each point now have two components, so it looks like this in memory: Point1(component1, component2), Point2(component1, component2), Point3.... is it right??? 
//Then I traverse the new data and assign each component of each point 
short *originalDataPointer = (short *)originalData->GetScalarPointer(); 
short *newDataPointer = (short *)newData->GetScalarPointer(); 
for(int i = 0; i < dim[0]*dim[1]*dim[2]; i++){ 
    //I assign each point's first component and second component the same data as the original data. 
    originalDataPointer[i*2] = newDataPointer[i]; 
    originalDataPointer[i*2 + 1] = newDataPointer[i]; 
} 

vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction = 
    vtkSmartPointer<vtkColorTransferFunction>::New(); 
colorTransferFunction->AddRGBPoint(0.0, 0.0, 0.5, 0.0); 
colorTransferFunction->AddRGBPoint(60.0, 1.0, 0.0, 0.0); 
colorTransferFunction->AddRGBPoint(128.0, 0.2, 0.1, 0.9); 
colorTransferFunction->AddRGBPoint(196.0, 0.27, 0.21, 0.1); 
colorTransferFunction->AddRGBPoint(255.0, 0.8, 0.8, 0.8); 
vtkSmartPointer<vtkPiecewiseFunction> piecewiseFunction = 
    vtkSmartPointer<vtkPiecewiseFunction>::New(); 
piecewiseFunction->AddPoint(20, 0.0); 
piecewiseFunction->AddPoint(120, 0.1); 
piecewiseFunction->AddPoint(255, 0.2); 
vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> fixedPointVolumeRayCastMapper = 
    vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New(); 
fixedPointVolumeRayCastMapper->SetNumberOfThreads(1); 

fixedPointVolumeRayCastMapper->SetInput(newData); 
vtkSmartPointer<vtkVolumeProperty> volumeProperty = 
    vtkSmartPointer<vtkVolumeProperty>::New(); 
//I want to use the first component as the input of opacity transfer function 
volumeProperty->SetScalarOpacity(0, piecewiseFunction); 
// I want to use the second component as the input of color transfer function 
volumeProperty->SetColor(1, colorTransferFunction); 

vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New(); 
volume->SetMapper(fixedPointVolumeRayCastMapper); 
volume->SetProperty(volumeProperty); 

vtkSmartPointer<vtkOpenGLRenderer> renderer = vtkSmartPointer<vtkOpenGLRenderer>::New(); 
renderer->AddVolume(volume); 
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New(); 
renWin->AddRenderer(renderer); 
renWin->Render(); 

體繪製的結果是僅具有相同的傳遞函數的一個部件的原始數據的結果不同勢。結果應該不一樣嗎?

每個像素都有兩個分量,第一個確定它是不透明的,第二個確定它的顏色,我該怎麼做?

+0

您是否希望每個像素都應具有不透明度變量以及用於體繪製目的的強度? – Tab

+0

每個像素都有兩個分量,第一個確定是不透明度,第二個確定是不是顏色,我應該怎麼做@Tab – user3197205

+0

通常我們會爲不是每個像素分配一個不透明度,而是指定一個特定的強度。你可以做一件事情,設置一個新的標量字段,它將是不透明的,並設置爲有效標量字段,您可以爲其設置不同的不透明度傳輸函數。 HTH – Tab

回答

1

如果有人仍然在意:您必須關閉「獨立組件」。

volumeProperty->IndependentComponentsOff() 

默認爲打開。

同時設定第一色功能:

volumeProperty->SetColor(colorTransferFunction); // not SetColor(1,... 

http://www.vtk.org/doc/release/5.10/html/classvtkVolumeProperty.html

數據是否具有獨立的組件,或者做一些只定義顏色?如果IndependentComponents處於On狀態(默認狀態),則每個組件都將獨立通過查找表以確定RGBA,並着色。某些卷映射器可以處理1至4個組件無符號字符或無符號短數據(請參閱每個映射器頭文件以確定功能)。如果IndependentComponents處於關閉狀態,則必須有2個或4個組件數據。 對於2分量數據,第一個通過第一個顏色轉移函數,第二個分量通過第一個不透明度轉移函數。法線將從第二個組件生成。對於4個分量數據,前三個將直接表示RGB(無查找表)。第四個分量將通過不透明度的第一個標量不透明度傳遞函數。法線將從第四個組件生成。

相關問題