[vtkusers] Picking a pixel from an image (works, but ...)

Dean Inglis dean.inglis at camris.ca
Wed Dec 16 20:30:30 EST 2009


Hi J,

here is what I would try:
make your picker a vtkPropPicker:

  this->Picker = vtkPropPicker::New();
  this->Picker->PickFromListOn();

then give it something to pick:

  this->Picker->AddPickList( imageViewer->GetImageActor() );

watch for pick events or mouse move event of an interactor etc.

  double q[3];
  this->Picker->GetPickPosition( q );

now the tricky bits where we interpolate the position to get an image value:

  vtkPointData* pd = 
imageViewer->GetImageActor() ->GetInput()->GetPointData();
  if( !pd ) return;

  vtkPointData* outPD = vtkPointData::New();
  outPD->InterpolateAllocate( pd, 1, 1 );

  // Use tolerance as a function of size of source data
  //
  double tol2 = imageViewer->GetImageActor() ->GetInput()->GetLength();
  tol2 = tol2 ? tol2*tol2 / 1000.0 : 0.001;

  // Find the cell that contains q and get it
  //
  int subId;
  double pcoords[3], weights[8];
  vtkCell* cell = imageViewer->GetImageActor() ->GetInput()->FindAndGetCell(
    q, NULL, -1, tol2, subId, pcoords, weights );
  if( cell )
  {
    int components;
    double* tuple;

    // Interpolate the point data
    //
    outPD->InterpolatePoint( pd, 0, cell->PointIds, weights );
    components = outPD->GetScalars()->GetNumberOfComponents();
    tuple = outPD->GetScalars()->GetTuple( 0 );

  here is where you would display the contents of your tuple...

vtkStdString s = "output: ";
  for(int i=0;i<components;++i)
{
s+=vtkVariant(tuple[i]).ToString();
s+=",";
}

  }

  outPD->Delete();

HTH,
Dean

Dear all,

I have a little problem picking a pixel from a vtkImageViewer2. I use 
vtkPointPicker for that. Here's the procedure I use:

1.Get mouse location in scene space:
double point[3];
picker->GetMapperPosition(point);
2.Get correct mapped z-location:
double spacing[3];
imageViewer->GetInput()->GetSpacing(spacing);
point[2] = imageViewer->GetSlice() * spacing[2];
3.Get the actual volume index of the mouse over pixel;
int index = imageViewer->GetInput()->FindPoint(point);
double* scalarPointer = 
(double*)imageViewer->GetInput()->GetScalarPointer();
double intensity = scalarPointer[index];

4.Printing the picked pixel coordinates and the pixel intensity:
cerr << "(" << point[0] << ", " << point[1] << ", " << point[2] << "): "  << 
intensity;

The problem I have is that I always get wrong values comparing to other 
programs. Maybe I miss something out.

Thank you in advance.

Regards,

J.





More information about the vtkusers mailing list