#VTK读取CT数据中的Pixel data
头文件
#include <io.h>
#include <iostream>
#include <vector>
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkImageCast.h>
#include <vtkDICOMImageReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkImageData.h>
#include <vtkCoordinate.h>
readDicom.cpp
void getFiles(string path, vector<string>& files) {
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p; //Store path
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
//如果是目录,迭代之(即文件夹内还有文件夹)
if ((fileinfo.attrib & _A_SUBDIR))
{
//文件名不等于"."&&文件名不等于".."
//.表示当前目录
//..表示当前目录的父目录
//判断时,两者都要忽略,不然就无限递归跳不出去了!
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
//如果不是,加入列表
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
//_findclose函数结束查找
_findclose(hFile);
}
}
int str2int(string a, int len) {
/*string tmp = "D:\\data\\LungPoint\\13\\13\\14\\dcm\\1.2.840.113619.2.428.3.695552.35.1623805458.976.399";
int n = tmp.size() - 3;*/
//cout << n << endl;
//const int n = len;
int result = 0;
for (int i = 0; i != a.size() - len; ++i) {
int bb = *(a.end() - 1 - i) - '0';
result += bb * int(pow(10, i));
}
return result;
}
void dicomread(vector<string> FileName_List) {
const int threshold = 800;
//vector<string> index;
vector<Tuple> Index_list;
float slice = 1;
vtkDICOMImageReader* reader = vtkDICOMImageReader::New();
const char *path = FileName_List[0].c_str();
reader->SetFileName(path); //read dcm data
//reader->SetFileName("lena.bmp");
reader->Update();
double spaceing[3];
reader->GetOutput()->GetSpacing(spaceing);
std::cout << "parameter:" << spaceing[0] << " "
<< spaceing[1] << " " << spaceing[2] << std::endl;
thick = spaceing[2];
space = spaceing[0];
int sum = 0;
int max = 0;
for (auto it : FileName_List) {
//cout << it << endl;
const DicomImage dcm_image(it.c_str());
const auto rows = dcm_image.getHeight();
const auto cols = dcm_image.getWidth();
/*cout << "rows: " << rows << " ";
cout << "cols: " << cols << endl;*/
DcmFileFormat file_format;
const auto condition = file_format.loadFile(it.c_str());
DcmDataset* data_set = file_format.getDataset();
const E_TransferSyntax xfer = data_set->getOriginalXfer();
// decompress data set if compressed
data_set->chooseRepresentation(xfer, nullptr);
// data_set->chooseRepresentation(EXS_LittleEndianExplicit, nullptr);
DcmElement* element = nullptr;
OFCondition result = data_set->findAndGetElement(DCM_PixelData, element);
unsigned short* pix_data;
result = element->getUint16Array(pix_data);
signed short* data = nullptr;
signed short int b = 0;
//Threshold selection
for (int i = rows; i < rows; i++) {
// could use dst2.at<unsigned short>(i, j) = ?
for (int j = 0; j < cols; j++) {
auto temp = pix_data[i * cols + j];
}
//cout << endl;
}
//cout << endl;
//
}
}