当使用VTK对图像进行显示,图像像素点的提取是其中一个重要的应用,包括对像素位置的判断,以及对像素值的读取。
对像素点提取的思路包括三个步骤:
获取renderer的指针,然后使用picker类来获得点击的点。
int *pos = this->GetInteractor()->GetEventPosition();
this->picker->Pick( pos[0], pos[1], 0.0, imageViewer->GetRenderer() );
double selectionPos[3];
this->picker->GetPickPosition(selectionPos);
在求解pick位置的过程中,需要对其进行操作,以得到具体的位置。
// Fixes some numerical problems with the picking
double *bounds = this->imageViewer->GetImageActor()->GetDisplayBounds();
//std::cout << bounds[4] << std::endl;
int axis = this->imageViewer->GetSliceOrientation();
selectionPos[axis] = bounds[2*axis];
对得到的位置进行插值,提取对应的像素值
vtkPointData* pd = image->GetPointData();
if( !pd )
{
return;
}
this->PointData->InterpolateAllocate( pd, 1, 1 );
// Use tolerance as a function of size of source data
double tol2 = image->GetLength();
tol2 = tol2 ? tol2*tol2 / 1000.0 : 0.001;
// Find the cell that contains pos
int subId;
double pcoords[3], weights[8];
vtkCell* cell = image->FindAndGetCell(
pos, NULL, -1, tol2, subId, pcoords, weights );
if( cell )
{
// Interpolate the point data
this->PointData->InterpolatePoint( pd, 0, cell->PointIds, weights );
int components =
this->PointData->GetScalars()->GetNumberOfComponents();
double* tuple = this->PointData->GetScalars()->GetTuple( 0 );
参考文献:
- http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/PickPixel
- http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/PickPixel2
- http://www.vtk.org/Wiki/VTK/Examples/Cxx/Picking/HighlightPickedActor