본문 바로가기

MATLAB/dcm4che with Matlab

dcm4che with Matlab example


http://www.mathworks.com/matlabcentral/fileexchange/18390-using-the-dicom-toolkit-dcm4che-from-matlab/content/demo_dcm4che.m

from Using the DICOM toolkit dcm4che from Matlabby Patrick Bolan 

Example showing how to load and use the dcm4che dicom toolkit

demo_dcm4che.m
% An example of how to use the dcm4che toolkit from matlab for file
% reading or other dicom functions.

% Step 1 is to download the toolkit from www.dcm4che.org. Select the
% dcm4che2 tookit 'bin' archive, and unzip it.

% Then add the java libraries to your path
checkjava = which('org.dcm4che2.io.DicomInputStream');
if isempty(checkjava)
    libpath = 'c:\your_path_here\dcm4che2lib\';
    javaaddpath([libpath 'dcm4che-core-2.0.12.jar']);
    javaaddpath([libpath 'dcm4che-image-2.0.12.jar']);
    javaaddpath([libpath 'dcm4che-imageio-2.0.12.jar']);
    javaaddpath([libpath 'dcm4che-iod-2.0.12.jar']);
    javaaddpath([libpath 'slf4j-api-1.4.3.jar']);
    javaaddpath([libpath 'slf4j-api-1.4.3.jar']);
    javaaddpath([libpath 'slf4j-log4j12-1.4.3.jar']);
    javaaddpath([libpath 'log4j-1.2.13.jar']);
end

% Now create a reader as follows (other examples on dcm4che wiki)
testfile = [matlabroot '\toolbox\images\imdemos\CT-MONO2-16-ankle.dcm'];
dcm = org.dcm4che2.data.BasicDicomObject;
din = org.dcm4che2.io.DicomInputStream(...
    java.io.BufferedInputStream(java.io.FileInputStream(testfile)));
din.readDicomObject(dcm, -1);

% You can access the fields like this:
%width = dcm.getInt(org.dcm4che2.data.Tag.Width)
rows = dcm.getInt(org.dcm4che2.data.Tag.Rows);
cols = dcm.getInt(org.dcm4che2.data.Tag.Columns);
pixeldata = dcm.getInts(org.dcm4che2.data.Tag.PixelData);

img = reshape(pixeldata, rows,cols);
imagesc(img);