您的位置:首页 > 其它

DCM图--按SeriesNumber进行分类

2015-11-20 17:15 537 查看
留后面参考。------将文件中的DCM图按照序列号进行分类,将相同序列号的DCM图移动到与序列号同名的文件中

#include "stdafx.h"
#include <vector>
#include <map>
#include <string>
#include <winsock2.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <math.h>

#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmdata/dcxfer.h"
#include "dcmtk/dcmdata/dcrledrg.h"
#include "dcmtk/dcmjpeg/djdecode.h"  /* for dcmjpeg decoders */
#include "dcmtk/dcmimgle/dcmimage.h" /* for DicomImage */
#include "dcmtk/dcmjpeg/dipijpeg.h"  /* for dcmimage JPEG plugin */

#include <Windows.h>
#include <io.h>
#include <direct.h>
#include <sstream>
extern "C"
{
#include "jpeglib.h"
}
#pragma comment(lib,"Netapi32.lib")
#pragma comment(lib, "ws2_32.lib")

using namespace std;

void SplitEx(const string& strOrignal , char ch , vector<string>& vctString)
{
istringstream iss(strOrignal);
string item;
while(getline(iss , item , ch)) vctString.push_back(item);
return;
}

DcmFileFormat *pFileFormat = new DcmFileFormat();

bool MoveDcmFile(string strPath)
{
if(NULL == pFileFormat)
{
cout<<"NULL == pFileFormat"<<endl;
return false;
}

OFCondition oc = pFileFormat->loadFile(strPath.c_str());
if(oc.bad())
{
cout<<"no tag: "<<strPath<<endl;
return false;
}

DcmDataset* pDataset = pFileFormat->getDataset();

int nSeriesNumber = 0;
OFString strSeriesNumber;

pDataset->findAndGetOFString(DCM_SeriesNumber, strSeriesNumber);
nSeriesNumber = atoi(strSeriesNumber.c_str());

vector<string> vctPathBlock;
SplitEx(strPath, '\\', vctPathBlock);
ostringstream oss;//创建一个流
if(vctPathBlock.size() < 0)
{
cout<<"路程错误"<<endl;
return 0;
}
for(int i = 0; i < vctPathBlock.size() - 1; i++)
{
oss<<vctPathBlock[i]<<"\\";
}
oss<<nSeriesNumber<<"\\";

//创建保存文件夹
char *strSaveFilePath = new(std::nothrow)char[oss.str().length() + 1];
memset(strSaveFilePath, 0, oss.str().length() + 1);
memcpy(strSaveFilePath, oss.str().c_str(), oss.str().length());
char *fileName = strSaveFilePath, *tag;
for(tag=fileName;*tag;tag++)
{
if (*tag=='\\')
{
char buf[1000],path[1000];
strcpy(buf,fileName);
buf[strlen(fileName)-strlen(tag)+1]=NULL;
strcpy(path,buf);
if (access(path,6)==-1)
{
int npot = 99;
npot = mkdir(path);//创建成功返回0 不成功返回-1
}
}
}
delete strSaveFilePath;

//将DCM图按序列号,移到对应的文件中
oss<<vctPathBlock[vctPathBlock.size() - 1];
string strSavePath = oss.str();

bool bMove = false;
bMove = MoveFile(strPath.c_str(), strSavePath.c_str());
//cout<<bMove<<endl;

}

//读取目录下所有的文件
void getFiles( string path, vector<string>& files )
{
//文件句柄
long   hFile   =   0;
//文件信息
struct _finddata_t fileinfo;
string p;
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(hFile);
}
}

int _tmain(int argc, _TCHAR* argv[])
{

string strPath = "D:\\Dtest16\\DICOM\\CT"; //1

vector<string> vctFilePath;
getFiles(strPath, vctFilePath);
for(int i = 0; i < vctFilePath.size(); i++)
{
MoveDcmFile(vctFilePath[i]);
}

if(NULL != pFileFormat)
{
delete pFileFormat;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: