Code covered by the BSD License  

Highlights from
DICOM server connection

DICOM server connection

by

 

This script connects to a DICOM server and transfer the a dicom series to the MATLAB workspace

dcmserver_getseriesinfo(dcmStudyListVal)
function dcmserver_getseriesinfo(dcmStudyListVal)
% function dcmserver_getseriesinfo(dcmStudyListVal)
%
% Matlab library function for "dcmserver_connection.m" (see the related help).

% University of Debrecen, Institute of Nuclear Medicine
% Author: Laszlo Balkay/2010

global SelectedStudyInstanceUID SeriesList;
dcmCommonData = get(findobj('tag','dcmStudyList_info'),'UserData');

StudyListSorted =  dcmCommonData.StudyList(dcmCommonData.indexes,6);
SelectedStudyInstanceUID = StudyListSorted{dcmStudyListVal};
dcmCommonData.SelectedStudyInstanceUID = SelectedStudyInstanceUID;
opcommand1 = ['findscu.exe -aet ',dcmCommonData.dcmaet,' -aec ',dcmCommonData.dcmaec,' --study '];
opcommand2a = ['-k 0008,0052=SERIES -k 0020,000D=',SelectedStudyInstanceUID,' '];
opcommand2b = ['-k 0020,000E -k 0008,103E -k 0008,0021 -k 0020,0011 -k 0020,1003 -k 0054,1000 ', ...
    ' -k 0018,1030 -k 0008,0031 -k 0008,0031 -k 0008,0060'];
opcommand3 = ['-v ',dcmCommonData.dcmhost, ' ',dcmCommonData.dcmport];

opcommand_findseries = [opcommand1,opcommand2a,opcommand2b,opcommand3,' > ',dcmCommonData.dcmtmpfile2];

dcmtlk_path = fileparts(which('echoscu.exe'));
curr_path = cd;
cd(dcmtlk_path);
[sys_status, sys_result] = system(opcommand_findseries);
cd(curr_path);

if sys_status ~= 0; disp('Error on dicom series query'); return; end

%type(dcmCommonData.dcmtmpfile2);

fid  = fopen(dcmCommonData.dcmtmpfile2,'r');
dcmseries_list = char(fread(fid,'char'));
fclose(fid);
dcm_startpos = findstr(dcmseries_list','RESPONSE: 1 ');
if isempty(dcm_startpos);
    disp(' ');
    disp('Error on connecting to the dicom server!');
    disp('Warnings from the findscu.exe series process:');
    disp(' ');
    type(dcmCommonData.dcmtmpfile2);
    disp(' ');
    delete(dcmCommonData.dcmtmpfile2);
    return; 
end

dcm_datepos = findstr(dcmseries_list(dcm_startpos+1 : end)','0008,0021');
dcm_timepos = findstr(dcmseries_list(dcm_startpos+1 : end)','0008,0031');
dcm_modalitypos = findstr(dcmseries_list(dcm_startpos+1 : end)','0008,0060');
dcm_seriesdescrpos = findstr(dcmseries_list(dcm_startpos+1 : end)','0008,103e');
dcm_ptotocolnamepos = findstr(dcmseries_list(dcm_startpos+1 : end)','0018,1030');
dcm_seriesnumberpos = findstr(dcmseries_list(dcm_startpos+1 : end)','0020,0011');
dcm_seriesinstUIDpos = findstr(dcmseries_list(dcm_startpos+1 : end)','0020,000e');

value_distance = 15;
date_length = 8;
time_length = 6;
num_of_series = length(dcm_seriesinstUIDpos);
SeriesList = cell(num_of_series,8);
SeriesListFull = cell(num_of_series,1);

for i=1:num_of_series
    
    % get the date
    if ~isempty(dcm_datepos)
        pos1 = dcm_startpos + dcm_datepos(i)+value_distance;
        pos2 = dcm_startpos+dcm_datepos(i)+value_distance+date_length-1;
        date_value = dcmseries_list(pos1 :pos2)';
    else
        date_value = 'NoValue';
    end
    
    % get the time
    if ~isempty(dcm_timepos)
        pos1 = dcm_startpos + dcm_timepos(i)+value_distance;
        pos2 = dcm_startpos+dcm_timepos(i)+value_distance+time_length-1;
        time_value = dcmseries_list(pos1 :pos2)';
    else
        time_value = 'NoValue';
    end
    % get the protocol name 
    % find the next closing bracket at the patient name row /~using 70 char/
    if ~isempty(dcm_ptotocolnamepos)
        pos1 = dcm_startpos + dcm_ptotocolnamepos(i) + value_distance;
        pos2 = dcm_startpos + dcm_ptotocolnamepos(i) + 70;
        next_bracket = findstr(dcmseries_list(pos1 : pos2)',']');
        if ~isempty(next_bracket)
            protocolname_value = dcmseries_list(pos1 : pos1 + next_bracket(1) -2)';
        else
            protocolname_value = 'no value';
        end
    else
        protocolname_value = 'no value';
    end
    
    % get the modality type
    % find the next closing bracket at the patient name row /~using 70 char/
    pos1 = dcm_startpos + dcm_modalitypos(i) + value_distance;
    pos2 = dcm_startpos + dcm_modalitypos(i) + 70;
    next_bracket = findstr(dcmseries_list(pos1 : pos2)',']');
    modality_value = dcmseries_list(pos1 : pos1 + next_bracket(1) -2)';
    
    % get the series description
    % find the next closing bracket at the patient name row /~using 70 char/
    if ~isempty(dcm_seriesdescrpos)
        pos1 = dcm_startpos + dcm_seriesdescrpos(i) + value_distance;
        pos2 = dcm_startpos + dcm_seriesdescrpos(i) + 70;
        next_bracket = findstr(dcmseries_list(pos1 : pos2)',']');
        if ~isempty(next_bracket)
            seriesdescription_value = dcmseries_list(pos1 : pos1 + next_bracket(1) -2)';
        else
            seriesdescription_value = 'no value';
        end
    else
        seriesdescription_value = 'no value';
    end
    
    % get the seriesnumber
    % find the next closing bracket at the seriesnumber row /~using 70 char/
    pos1 = dcm_startpos + dcm_seriesnumberpos(i) + value_distance;
    pos2 = dcm_startpos + dcm_seriesnumberpos(i) + 70;
    next_bracket = findstr(dcmseries_list(pos1 : pos2)',']');
    if ~isempty(next_bracket)
        seriesnumber_value = dcmseries_list(pos1 : pos1 + next_bracket(1) -2)';
    else
        seriesnumber_value = 'no value';
    end
    
    % get the seriesinstUID value
    % find the next closing bracket at the studyinstUID row /~using 90 char/
    pos1 = dcm_startpos + dcm_seriesinstUIDpos(i) + value_distance;
    pos2 = dcm_startpos + dcm_seriesinstUIDpos(i) + 90;
    next_bracket = findstr(dcmseries_list(pos1 : pos2)',']');
    seriesinstUID_value = dcmseries_list(pos1 : pos1 + next_bracket(1) -2)';
    
    
    SeriesList{i,1} = date_value;
    SeriesList{i,2} = time_value;
    SeriesList{i,3} = protocolname_value;
    SeriesList{i,4} = modality_value;
    SeriesList{i,5} = seriesdescription_value;
    SeriesList{i,6} = seriesnumber_value;
    if length(date_value) ~= 8
        date_value = 'No Value';time_value = 'No Value'; 
        SeriesList{i,7} = 0;
    elseif length(date_value) == 8 && strcmp(time_value,'no val')
        SeriesList{i,7} = datenum([date_value,'000000'],'yyyymmddHHMMSS');
    else
        SeriesList{i,7} = datenum([date_value,time_value],'yyyymmddHHMMSS');
    end
    SeriesList{i,8} = seriesinstUID_value;
    
    SeriesListFull{i} = [date_value,'  ',time_value, blanks(10), modality_value, blanks(30-length(modality_value)), ...
        ' ',protocolname_value,blanks(30-length(protocolname_value)), seriesdescription_value, ...
        blanks(30-length(seriesdescription_value)),seriesnumber_value];
end

set(dcmCommonData.SerLh,'string',SeriesListFull);
%dcmCommonData.SeriesList = SeriesList;

set(findobj('tag','dcmStudyList_info'),'UserData',dcmCommonData);
delete(dcmCommonData.dcmtmpfile2);

Contact us