原文链接:https://blog.csdn.net/yu253/article/details/78314513?utm_source=blogxgwz9
#include <vtkSmartPointer.h>
#include <vtkObjectFactory.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkActor.h>
#include <vtkImageViewer2.h>
#include <vtkDICOMImageReader.h>
#include <vtkImageData.h>
#include <vtkInteractorStyleImage.h>
#include <vtkActor2D.h>
#include <vtkTextProperty.h>
#include <vtkTextMapper.h>
//
#include <sstream>
#include <iostream>
//
using namespace std;
class StatusMessage {
public:
static std::string Format(int slice, int maxSlice) {
std::stringstream tmp;
tmp << "Slice Number " << slice + 1 << "/" << maxSlice + 1;
return tmp.str();
}
};
class myvtkInteractorStyleImage: public vtkInteractorStyleImage {
public:
static myvtkInteractorStyleImage *New();
vtkTypeMacro(myvtkInteractorStyleImage, vtkInteractorStyleImage);
protected:
vtkImageViewer2 *_ImageViewer;
vtkTextMapper *_StatusMapper;
int _Slice;
int _MinSlice;
int _MaxSlice;
public:
void SetImageViewer(vtkImageViewer2 *imageViewer) {
_ImageViewer = imageViewer;
_MinSlice = imageViewer->GetSliceMin();
_MaxSlice = imageViewer->GetSliceMax();
_Slice = _MinSlice;
cout << "Slice: Min = " << _MinSlice << ", Max = " << _MaxSlice << endl;
}
void SetStatusMapper(vtkTextMapper *statusMapper) {
_StatusMapper = statusMapper;
}
protected:
void MoveSliceForward() {
if(_Slice < _MaxSlice) {
_Slice += 1;
cout << "MoveSliceForward::Slice = " << _Slice << endl;
_ImageViewer->SetSlice(_Slice);
std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
_StatusMapper->SetInput(msg.c_str());
_ImageViewer->Render();
}
}
void MoveSliceBackward() {
if(_Slice > _MinSlice) {
_Slice -= 1;
cout << "MoveSliceBackward::Slice = " << _Slice << endl;
_ImageViewer->SetSlice(_Slice);
std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
_StatusMapper->SetInput(msg.c_str());
_ImageViewer->Render();
}
}
virtual void OnKeyDown() {
std::string key = this->GetInteractor()->GetKeySym();
if(key.compare("Up") == 0) {
MoveSliceForward();
} else if(key.compare("Down") == 0) {
MoveSliceBackward();
}
vtkInteractorStyleImage::OnKeyDown();
}
virtual void OnMouseWheelForward() {
MoveSliceForward();
}
virtual void OnMouseWheelBackward() {
if(_Slice > _MinSlice) {
MoveSliceBackward();
}
}
};
vtkStandardNewMacro(myvtkInteractorStyleImage);
int main(int argc, char *argv[]) {
std::string folder = "../data/SE11";
vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
reader->SetDirectoryName(folder.c_str());
reader->Update();
//获取维数
int dims[3];
reader->GetOutput()->GetDimensions(dims);
std::cout << "图像维数:" << dims[0] << " " << dims[1] << " " << dims[2] << std::endl;
//
double spaceing[3];
reader->GetOutput()->GetSpacing(spaceing);
std::cout << "图像间隔:" << spaceing[0] << " " << spaceing[1] << " " << spaceing[2] << std::endl;
//得到指定像素位置处的灰度值
int x, y, z;
x = 30;
y = 30;
z = 30;
unsigned char *pixel = (unsigned char *)(reader->GetOutput()->GetScalarPointer(x, y, z)); //获取数据指针
printf("%#x", pixel); //打印Pixel指向的内容的地址
cout << endl;
printf("%d", *pixel);//打印Pixel指向的内容
cout << endl;
//输出每个点的值,需转化为int 型
cout << int(*pixel) << endl;
//===================================
vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkTextProperty> sliceTextProp = vtkSmartPointer<vtkTextProperty>::New();
sliceTextProp->SetFontFamilyToCourier();
sliceTextProp->SetFontSize(20);
sliceTextProp->SetVerticalJustificationToBottom();
sliceTextProp->SetJustificationToLeft();
vtkSmartPointer<vtkTextMapper> sliceTextMapper = vtkSmartPointer<vtkTextMapper>::New();
std::string msg = StatusMessage::Format(imageViewer->GetSliceMin(), imageViewer->GetSliceMax());
sliceTextMapper->SetInput(msg.c_str());
sliceTextMapper->SetTextProperty(sliceTextProp);
vtkSmartPointer<vtkActor2D> sliceTextActor = vtkSmartPointer<vtkActor2D>::New();
sliceTextActor->SetMapper(sliceTextMapper);
sliceTextActor->SetPosition(15, 10);
vtkSmartPointer<vtkTextProperty> usageTextProp = vtkSmartPointer<vtkTextProperty>::New();
usageTextProp->SetFontFamilyToCourier();
usageTextProp->SetFontSize(14);
usageTextProp->SetVerticalJustificationToTop();
usageTextProp->SetJustificationToLeft();
vtkSmartPointer<vtkTextMapper> usageTextMapper = vtkSmartPointer<vtkTextMapper>::New();
usageTextMapper->SetInput("- Slice with mouse wheel\n or Up/Down-Key\n- Zoom with pressed right\n mouse button while dragging");
usageTextMapper->SetTextProperty(usageTextProp);
vtkSmartPointer<vtkActor2D> usageTextActor = vtkSmartPointer<vtkActor2D>::New();
usageTextActor->SetMapper(usageTextMapper);
usageTextActor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay();
usageTextActor->GetPositionCoordinate()->SetValue(0.05, 0.95);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<myvtkInteractorStyleImage> myInteractorStyle =
vtkSmartPointer<myvtkInteractorStyleImage>::New();
myInteractorStyle->SetImageViewer(imageViewer);
myInteractorStyle->SetStatusMapper(sliceTextMapper);
imageViewer->SetupInteractor(renderWindowInteractor);
renderWindowInteractor->SetInteractorStyle(myInteractorStyle);
imageViewer->GetRenderer()->AddActor2D(sliceTextActor);
imageViewer->GetRenderer()->AddActor2D(usageTextActor);
imageViewer->GetRenderWindow()->SetSize(800, 600);
imageViewer->SetColorLevel(500);//窗位
imageViewer->SetColorWindow(1000);//窗宽
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}