matlab读取医学图像MetaImage类型的.mha和.mhd文件
0.简单说明:
matlab读取DICOM医学图像文件的方式很简单直接,matlab自带dicomread函数。
>> help dicomread
dicomread - Read DICOM image
This MATLAB function reads the image data from the compliant Digital Imaging and
Communications in Medicine (DICOM) file filename.
X = dicomread(filename)
X = dicomread(info)
[X,map] = dicomread(...)
[X,map,alpha] = dicomread(...)
[X,map,alpha,overlays] = dicomread(...)
[...] = dicomread(filename, 'frames', v)
[...] = dicomread(___,'UseVRHeuristic',TF)
但是,如果想要另外一种医学图像类型.mha或者.mhd的话,matlab就没有自带对应函数了。如果想继续用dicomread()函数,就需要用itk自己转换成dicom了,反而麻烦了。所以直接access MetaImage为宜,经自己测试,附上代码如下:
1. 代码正文:
1. 测试脚本1:
mhaAccessDemo.m
close all
clear
clc%% mha or mhd file access demo
hdInfo = mha_read_header('./liverMaskDemo.mha');
imgData = mha_read_volume(hdInfo);
imshow((imgData(:,:,round(end/2))));% 显示Axial(Z轴中间层数据)
测试结果图:
2. 图像头文件信息获取函数:
mha_read_header.m
function info =mha_read_header(filename)
% Function for reading the header of a Insight Meta-Image (.mha,.mhd) file
%
% info = mha_read_header(filename);
%
% examples:
% 1, info=mha_read_header()
% 2, info=mha_read_header('volume.mha');
if(exist('filename','var')==0)[filename, pathname] = uigetfile('*.mha', 'Read mha-file');filename = [pathname filename];
end
fid=fopen(filename,'rb');
if(fid<0)fprintf('could not open file %s\n',filename);return
end
info.Filename=filename;
info.Format='MHA';
info.CompressedData='false';
readelementdatafile=false;
while(~readelementdatafile)str=fgetl(fid);s=find(str=='=',1,'first');if(~isempty(s))type=str(1:s-1); data=str(s+1:end);while(type(end)==' '); type=type(1:end-1); endwhile(data(1)==' '); data=data(2:end); endelsetype=''; data=str;endswitch(lower(type))case 'ndims'info.NumberOfDimensions=sscanf(data, '%d')';case 'dimsize'info.Dimensions=sscanf(data, '%d')';case 'elementspacing'info.PixelDimensions=sscanf(data, '%lf')';case 'elementsize'info.ElementSize=sscanf(data, '%lf')';if(~isfield(info,'PixelDimensions'))info.PixelDimensions=info.ElementSize;endcase 'elementbyteordermsb'info.ByteOrder=lower(data);case 'anatomicalorientation'info.AnatomicalOrientation=data;case 'centerofrotation'info.CenterOfRotation=sscanf(data, '%lf')';case 'offset'info.Offset=sscanf(data, '%lf')';case 'binarydata'info.BinaryData=lower(data);case 'compresseddatasize'info.CompressedDataSize=sscanf(data, '%d')';case 'objecttype',info.ObjectType=lower(data);case 'transformmatrix'info.TransformMatrix=sscanf(data, '%lf')';case 'compresseddata';info.CompressedData=lower(data);case 'binarydatabyteordermsb'info.ByteOrder=lower(data);case 'elementdatafile'info.DataFile=data;readelementdatafile=true;case 'elementtype'info.DataType=lower(data(5:end));case 'headersize'val=sscanf(data, '%d')';if(val(1)>0), info.HeaderSize=val(1); endotherwiseinfo.(type)=data;end
end
switch(info.DataType)case 'char', info.BitDepth=8;case 'uchar', info.BitDepth=8;case 'short', info.BitDepth=16;case 'ushort', info.BitDepth=16;case 'int', info.BitDepth=32;case 'uint', info.BitDepth=32;case 'float', info.BitDepth=32;case 'double', info.BitDepth=64;otherwise, info.BitDepth=0;
end
if(~isfield(info,'HeaderSize'))info.HeaderSize=ftell(fid);
end
fclose(fid);
3. 图像数据(坐标、灰度值)函数:
mha_read_volume.m
function V = mha_read_volume(info)
% Function for reading the volume of a Insight Meta-Image (.mha, .mhd) file
%
% volume = tk_read_volume(file-header)
%
% examples:
% 1: info = mha_read_header()
% V = mha_read_volume(info);
% imshow(squeeze(V(:,:,round(end/2))),[]);
%
% 2: V = mha_read_volume('test.mha');
if(~isstruct(info)), info=mha_read_header(info); end
switch(lower(info.DataFile))case 'local'otherwise% Seperate fileinfo.Filename=fullfile(fileparts(info.Filename),info.DataFile);
end% Open file
switch(info.ByteOrder(1))case ('true')fid=fopen(info.Filename,'rb','ieee-be'); %去掉fopen第一个参数的转置'otherwisefid=fopen(info.Filename,'rb','ieee-le'); %ditto
end
switch(lower(info.DataFile))case 'local'% Skip headerfseek(fid,info.HeaderSize,'bof');otherwisefseek(fid,0,'bof');
end
datasize=prod(info.Dimensions)*info.BitDepth/8;
switch(info.CompressedData(1))case 'f'% Read the Dataswitch(info.DataType)case 'char'V = int8(fread(fid,datasize,'char')); case 'uchar'V = uint8(fread(fid,datasize,'uchar')); case 'short'V = int16(fread(fid,datasize,'short')); case 'ushort'V = uint16(fread(fid,datasize,'ushort')); case 'int'V = int32(fread(fid,datasize,'int')); case 'uint'V = uint32(fread(fid,datasize,'uint')); case 'float'V = single(fread(fid,datasize,'float')); case 'double'V = double(fread(fid,datasize,'double'));endcase 't'switch(info.DataType)case 'char', DataType='int8';case 'uchar', DataType='uint8';case 'short', DataType='int16';case 'ushort', DataType='uint16';case 'int', DataType='int32';case 'uint', DataType='uint32';case 'float', DataType='single';case 'double', DataType='double';endZ = fread(fid,inf,'uchar=>uint8');V = zlib_decompress(Z,DataType);
end
fclose(fid);
V = reshape(V,info.Dimensions);
function M = zlib_decompress(Z,DataType)
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier
a=java.io.ByteArrayInputStream(Z);
b=java.util.zip.InflaterInputStream(a);
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
c = java.io.ByteArrayOutputStream;
isc.copyStream(b,c);
M=typecast(c.toByteArray,DataType);
2. 写在最后:
周末偷闲临时小纪,更多资料大家可以访问mathworks官网去获取。
matlab读取医学图像MetaImage类型的.mha和.mhd文件相关推荐
- matlab读取xml文档并储存为.mat文件详解
起因是需要将c++中的矩阵传入matlab中进行处理,发现xml文档可以保留精度,因此选择xml作为中介完成从c++到matlab的数据传递. xml文档结构如下: <opencv_storag ...
- matlab将txt数据分类,MATLAB读取txt文件,txt里面有字符串和数值两种类型
MATLAB读取txt文件,txt里面有字符串和数值两种类型 mip版 关注:96 答案:4 悬赏:30 解决时间 2021-01-29 08:48 已解决 2021-01-28 15:04 M ...
- Matlab读取 mp4 视频 Error Creating Source Reader Reason: 不支持给定的 URL 的字节流类型
问题描述 在window环境下,使用Matlab读取视频时,如下蓝色代码读取视频 % 读取视频 inputPathName='C:\download\'; [filename,inpu ...
- matlab 打开txt文件窗口,Matlab读取txt文件、xlsx文件
MATLAB读取和写入txt文件 https://blog.csdn.net/jisuanjiguoba/article/details/79997805 txt文件中既有空格又有tab键,请问怎么正 ...
- MATLAB读取二进制文件------fread
fread: 读取二进制文件中的数据 语法: A = fread(fileID) A = fread(fileID,sizeA) A = fread(fileID,sizeA,precision) A ...
- Matlab读取点云数据显示
求matlab读取三维点云数据的程序. clear A=importdata('data.txt'); [IX,IY]=size(A); x=A(:,1 ...
- matlab读取txt数据绘图(python命令行传参)
(1)命令行实现高斯分布 一:综述 Python唯一支持的参数传递方式是『共享传参』(call by sharing)多数面向对象语言都采用这一模式,包括Ruby.Smalltalk和Java(Jav ...
- matlab读取文件夹下所有文件的字符串,MATLAB读取文件夹下所有文件的文件名并读取数据...
MATLAB读取文件夹下特定类型格式文件的文件名并读取该格式文件的数据 利用代码对大量数据进行自动批处理时,首先需要获取该数据存放的文件夹(文件夹路径),然后获取该文件夹下某一类型数据所有的文件名,最 ...
- matlab读lExcel文字,matlab读取excel文件及其数据处理
在许多时候我们要借助于matlab读取excel的内容进行处理,以下是一种常用的处理方法 office的表格文件也就是xls文件本质上就是一个二维矩阵,二维矩阵是用来保存数据的最佳方式,所以在日常工作 ...
最新文章
- JS 正则表达式 0.001 ~99.999
- 让面试官颤抖的 HTTP 2.0 协议面试题
- 使用正则表达式模拟读写INI文件
- SAP更新数据表的程序执行需要SE38后执行
- HTML中的div标签
- 支持自定义的离线语音模块WT516P6Core 串口协议使用说明
- HashTable 和HashMap区别
- 哪本书是对程序员最有影响(stackoverflow)
- IntelliJ IDEA 12 与 Tomcat7 配置
- 12条人生规则《12 Rules for Life: An Antidote to Chaos》
- SpringBoot学习---thymeleaf模板引擎
- Tomcat下work文件夹的作用
- 图格 Pro for Mac(多功能照片拼图切图大师)
- 在浏览器输入URL,按下回车之后的流程?
- 中文转拼音全拼和首字母
- ECharts 示例——双Y轴
- 这里有一份CAD 快捷键指南,请查收~
- Quartus TCL
- QQ一来消息或一些提示声,媒体、视频、音频、游戏就会卡顿【解决方法】
- 什么是 jQuery?