本人图像处理新人,求 Visual Studio 2010读取 Dicom 图片文件的代码,最终要能显示 Dicom 图片!!! 100
展开全部
struct TagValue
{
unsigned short tag1;
unsigned short tag2;
};
void main()
{
bool isVR=true;
bool isLitteEndian=true;
int file_length=0;
char VR[3];
unsigned int pixDataLen=0;
unsigned int pixDataOffset=0;
unsigned short channle=0;
unsigned short rows=0;
unsigned short cols=0;
unsigned short dataLen=0;
unsigned short validLen=0;
int windowsWidth=0;
int windowsCenter=0;
bool ZeroIsBlack=true;
int RescaleSlope =0;
int RescaleIntercept=0;
FILE *fp;
fp=fopen("xxxx.dcm","rb");
if(fp==NULL)
{
printf("can not open file!");
return ;
}
fseek(fp,0,SEEK_END);
file_length=ftell(fp);
fseek(fp,0,SEEK_SET);
fseek(fp,128,SEEK_SET);
char headchar[5];
memset(headchar,0,5);
int read_num = fread(headchar,1,4,fp);
if(read_num!=4)
{
fclose(fp);
return;
}
if(strcmp(headchar,"DICM"))
{
fclose(fp);
printf("File is not DICM");
return;
}
while(ftell(fp)+6<file_length)
{
TagValue tag;
unsigned int len;
memset(VR,0,3);
fread(&tag,sizeof(TagValue),1,fp);
int index=ftell(fp);
if(tag.tag1==0x02)
{
fread(VR,1,2,fp);
if(!strcmp(VR,"OB")||!strcmp(VR,"OW")||!strcmp(VR,"SQ"))
{
fseek(fp,2,SEEK_CUR);
fread(&len,sizeof(unsigned int),1,fp);
}
else
{
unsigned short l;
int ss = fread(&l,sizeof(unsigned short),1,fp);
int a=ftell(fp);
len =(unsigned int)l ;
}
}
else if(tag.tag1==0xfffe)
{
if(tag.tag2==0xe000||tag.tag2==0xe00d||tag.tag2==0xe0dd)
{
fread(&len,sizeof(unsigned int),1,fp);
}
}
else if(isVR==true)
{
fread(VR,1,2,fp);
int a= ftell(fp);
if(!strcmp(VR,"OB")||!strcmp(VR,"OW")||!strcmp(VR,"SQ"))
{
fseek(fp,2,SEEK_CUR);
fread(&len,sizeof(unsigned int),1,fp);
}
else
{
unsigned short l;
l=sizeof(unsigned short);
fread(&l,sizeof(unsigned short),1,fp);
a= ftell(fp);
len =(unsigned int)l ;
}
}
else if(isVR==false)
{
fread(&len,sizeof(unsigned int),1,fp);
}
if(tag.tag1==0x02&&tag.tag2==0x10)
{
char msg[124];
memset(msg,0,124);
fread(msg,1,len,fp);
if(!strcmp(msg,"1.2.840.10008.1.2.1"))
{
isLitteEndian=true;
isVR=true;
}
else if(!strcmp(msg,"1.2.840.10008.1.2.2"))
{
isLitteEndian=false;
isVR=true;
}
else if(!strcmp(msg,"1.2.840.10008.1.2"))
{
isLitteEndian=true;
isVR=false;
}
}
else if(tag.tag1==0x7fe0&&tag.tag2==0x10)
{
pixDataLen=len;
pixDataOffset=ftell(fp);
fseek(fp,len,SEEK_CUR);
}
else if(tag.tag1==0x28&&tag.tag2==0x10)
{
fread(&rows,sizeof(unsigned short),1,fp);
}
else if(tag.tag1==0x28&&tag.tag2==0x11)
{
fread(&cols,sizeof(unsigned short),1,fp);
}
else if(tag.tag1==0x28&&tag.tag2==0x02)
{
fread(&channle,sizeof(unsigned short),1,fp);
}
else if(tag.tag1==0x28&&tag.tag2==0x101)
{
fread(&validLen,sizeof(unsigned short),1,fp);
}
else if(tag.tag1==0x28&&tag.tag2==0x100)
{
fread(&dataLen,sizeof(unsigned short),1,fp);
}
else if(tag.tag1==0x28&&tag.tag2==0x1050)
{
char msg[11];
memset(msg,0,11);
fread(msg,1,len,fp);
windowsCenter=atoi(msg);
}
else if(tag.tag1==0x28,tag.tag2==0x1051)
{
//fseek(fp,len,SEEK_CUR);
char msg[40];
memset(msg,0,40);
fread(msg,1,len,fp);
windowsWidth=atoi(msg);
}
else if(tag.tag1==0x0028&&tag.tag2==0x0004)
{
char msg[40];
memset(msg,0,40);
fread(msg,1,len,fp);
if(!strcmp(msg,"MONOCHROME1 "))
{
ZeroIsBlack=false;
}
else if(!strcmp(msg,"MONOCHROME2 "))
{
ZeroIsBlack=true;
}
}
else if(tag.tag1==0x0028&&tag.tag2==0x1052)
{
char msg[40];
memset(msg,0,40);
fread(msg,1,len,fp);
RescaleIntercept=atoi(msg);
}
else if(tag.tag1==0x0028&&tag.tag2==0x1053)
{
char msg[40];
memset(msg,0,40);
fread(msg,1,len,fp);
RescaleSlope =atoi(msg);
}
else
{
char msg[1024];
memset(msg,0,1024);
fread(msg,1,len,fp);
}
}
fseek(fp,pixDataOffset,SEEK_SET);
if(windowsCenter==0&&windowsWidth==0)
{
windowsWidth = 1 << validLen;
windowsCenter = windowsWidth / 2;
}
int min_value,max_value;
min_value=windowsCenter-windowsWidth/2.0+0.5;
max_value=windowsCenter+windowsWidth/2.0+0.5;
double pers = 255.0/(max_value-min_value);
Mat src;
if(channle==1)
{
src.create((int)rows,(int)cols,CV_8UC1);
for (int i = 0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
short gray=0;
unsigned char pix[2];
fread(pix,1,2,fp);
if(validLen<=8)
{
if(isLitteEndian)
{
gray=pix[0];
}
else
{
gray=pix[1];
}
}
else
{
if(isLitteEndian)
{
gray=*(short*)pix;
gray =gray/RescaleSlope+RescaleIntercept;
}
else
{
gray=pix[1]+pix[0]*256;
}
if(gray>0xff)
{
gray=gray;
}
if(gray<0)
{
gray=gray;
}
}
if(!ZeroIsBlack)
{
gray=255-gray;
}
src.at<uchar>(i,j)=gray;
}
}
}
else if(channle==3)
{
src.create((int)rows,(int)cols,CV_8UC3);
for (int i = 0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
unsigned char pix[3];
fread(pix,1,3,fp);
src.at<Vec3b>(i,j)[0]=pix[2];
src.at<Vec3b>(i,j)[1]=pix[1];
src.at<Vec3b>(i,j)[2]=pix[0];
}
}
}
fclose(fp);
imshow("src",src);
waitKey(0);
}
使用opencv做的显示,这里面有一些小问题,需要自己调试修改
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询