用鼠标获取二维图像中某点的坐标值 #include "stdafx.h" /* 用鼠标获取二维图像中某点的坐标值
解决方法如下 1. 把窗口二维坐标转换为空间三维坐标, 用个vtkWorldPointPicker 就可以了 2. 利用图像点和三维空间坐标的映射关系, 把空间三维坐标, 转换为图像像素位置(i,j,k) 3. i,j,k 一般情况都是浮点数, 利用差值算法( 临近, 线性等)计算出此点对应的像素值 其实任何时候都可以使用vtkRenderWindowInteractor中的GetEventPosition ();来获取鼠标位置的坐标。不需要那么多转换 */ #include <iostream> #include "vtkRenderWindow.h" #include "vtkCamera.h" #include "vtkActor.h" #include "vtkRenderer.h" #include "vtkDICOMImageReader.h" #include "vtkImageViewer.h" #include "vtkInteractorStyleTrackballCamera.h" #include "vtkRenderWindowInteractor.h" #include "vtkImageViewer.h" #include "vtkImageActor.h" #include "vtkImageCast.h" #include "vtkPointPicker.h" #include "vtkCallbackCommand.h" #include "vtkImageData.h"
void left(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata); void MyExecute(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata); vtkPointPicker *picker; vtkInteractorStyleTrackballCamera *style; vtkRenderWindowInteractor *viewerinter; vtkDICOMImageReader *dr; int main() { dr = vtkDICOMImageReader::New(); dr->SetDataByteOrderToLittleEndian(); dr->SetFileName("C://DATADIR/brain/brain_007.dcm"); dr->SetDataOrigin(0.0,0.0,0.0); vtkImageViewer *viewer = vtkImageViewer::New(); viewer->SetInput(dr->GetOutput()); viewer->SetColorWindow(1024);//这就是传说中的窗宽 viewer->SetColorLevel(512/2);//窗位 viewer->Render(); viewerinter = vtkRenderWindowInteractor::New(); viewer->SetupInteractor(viewerinter); picker=vtkPointPicker::New(); vtkCallbackCommand * cmd1 = vtkCallbackCommand::New(); cmd1->SetCallback(MyExecute); picker->AddObserver(vtkCommand::EndPickEvent, cmd1); viewerinter->SetPicker(picker); style=vtkInteractorStyleTrackballCamera::New(); viewerinter-> SetInteractorStyle(style); vtkCallbackCommand * cmd = vtkCallbackCommand::New(); cmd->SetCallback(left); style-> AddObserver (vtkCommand::LeftButtonPressEvent,cmd); viewerinter->Initialize(); viewerinter->Start(); return 0; } void left(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata) { int selPt[2]; viewerinter->GetEventPosition (selPt); printf("X: %d, Y: %d\n",selPt[0],selPt[1]); float temp=dr->GetOutput()->GetScalarComponentAsFloat(selPt[0],selPt[1],0,0); printf("像素为: %f\n",temp); style->OnLeftButtonDown(); } void MyExecute(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata) { double selPt[3]; picker->GetSelectionPoint(selPt); printf("X: %f, Y: %f\n",selPt[0],selPt[1]); float temp=dr->GetOutput()->GetScalarComponentAsFloat(selPt[0],selPt[1],0,0); printf("像素为: %d\f",temp); /* double globalCoordinate[3]; picker->GetPickPosition(globalCoordinate); printf("X: %f, Y: %f, Z: %f\n", globalCoordinate[0],globalCoordinate[1],globalCoordinate[2]); */ style->OnLeftButtonDown(); } |