vtk坐标变换:由UI鼠标位置得到一个像素的像素值

获取鼠标位置

//QVTKInteractor* m_interactor;

int eventPosition[2];
m_interactor->GetEventPosition(eventPosition);

从鼠标位置得到世界坐标位置

//vtkSmartPointer<vtkRenderer> m_renderer;

vtkNew<vtkCoordinate> coordinate;
coordinate->SetCoordinateSystemToDisplay();
coordinate->SetValue(viewportPosition);
double* worldCoordinate = coordinate->GetComputedWorldValue(m_renderer);

由世界坐标和像素间隙spacing得到像素坐标

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]);

根据每个像素存储的字节数来获取像素值

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));
	}
}

其中,获取每个像素有几个字节存储像素值

int Reader::GetSamplesPerPixel()
{
	return m_metaData->GetAttributeValue(DC::SamplesPerPixel).AsInt();
}

猜你喜欢

转载自blog.csdn.net/ClamReason/article/details/93042567
今日推荐