Get the mouse position
//QVTKInteractor* m_interactor;
int eventPosition[2];
m_interactor->GetEventPosition(eventPosition);
Get the mouse position from the world coordinate position
//vtkSmartPointer<vtkRenderer> m_renderer;
vtkNew<vtkCoordinate> coordinate;
coordinate->SetCoordinateSystemToDisplay();
coordinate->SetValue(viewportPosition);
double* worldCoordinate = coordinate->GetComputedWorldValue(m_renderer);
Get the pixel coordinates of the world coordinate and pixel spacing gap
vtkImageData* imageData = m_reader->GetOutput();
double spacing[2];
imageData->GetSpacing(spacing);
int x = std::round(worldCoordinate[0] / spacing[0]);
int y = std::round(worldCoordinate[1] / spacing[1]);
Acquiring a pixel value for each pixel according to the number of bytes stored
int z = 0;
//各个维度的像素范围
int dims[2];
imageData->GetDimensions(dims);
string text = "";
if ((x >= 0 && x < dims[0]) && (y >= 0 && y < dims[1]))
{
int SamplePerPixel = GetSamplesPerPixel();
if (SamplePerPixel == 1)
{
auto pixelValue = (short *)imageData->GetScalarPointer(x, y, z);
text = "X:" + to_string(x) + " Y:" + to_string(y) + " Val:" + to_string(*pixelValue);
}
else if (SamplePerPixel == 3)
{
auto pixelValue = (unsigned char *)imageData->GetScalarPointer(x, y, z);
text = "X:" + to_string(x)
+ " Y:" + to_string(y)
+ " Val:"
+ to_string(*pixelValue) + ","
+ to_string(*(pixelValue + 1)) + ","
+ to_string(*(pixelValue + 2));
}
}
Wherein each pixel has acquired several bytes storing pixel values
int Reader::GetSamplesPerPixel()
{
return m_metaData->GetAttributeValue(DC::SamplesPerPixel).AsInt();
}