Convert multiple Dicom images(.dcm files) into grayscale images

6 views (last 30 days)
I have a listbox, pushbutton and axes to display dicom images. After I have load the images into listbox, I want to convert all the images to grayscale images. After I had created dicom2gray function, it shows black screen images. Can someone help me check the code? Thanks
function varargout = fyp2016(varargin)
% FYP2016 MATLAB code for fyp2016.fig
% FYP2016, by itself, creates a new FYP2016 or raises the existing
% singleton*.
%
% H = FYP2016 returns the handle to a new FYP2016 or the handle to
% the existing singleton*.
%
% FYP2016('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FYP2016.M with the given input arguments.
%
% FYP2016('Property','Value',...) creates a new FYP2016 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before fyp2016_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to fyp2016_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help fyp2016
% Last Modified by GUIDE v2.5 10-Apr-2016 14:29:03
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @fyp2016_OpeningFcn, ...
'gui_OutputFcn', @fyp2016_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before fyp2016 is made visible.
function fyp2016_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to fyp2016 (see VARARGIN)
% Choose default command line output for fyp2016
% Choose default command line output for DICOMFiles
handles.center = 1024; handles.width = 80;
handles.output = hObject;
handles.cdir = pwd;
set(handles.DicomListBox,'enable','off');
set(handles.BrightnessSlider, 'Enable', 'off');
set(handles.ContrastSlider, 'Enable', 'off');
set(handles.editBrightness,'String', sprintf('%10s:%4.0f%%', 'Brightness', 100*get(handles.BrightnessSlider,'Value')));
set(handles.editContrast,'String', sprintf('%10s:%4.0f%%', 'Contrast', 100*get(handles.ContrastSlider,'Value')));
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes fyp2016 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = fyp2016_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in InsertPushButton.
function InsertPushButton_Callback(hObject, eventdata, handles)
% hObject handle to InsertPushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.output = hObject;
fn = uigetdir(handles.cdir,'Select directory');
if fn ~= 0
handles.cdir = fn;
img = dir(fullfile(handles.cdir,'*.dcm'));
for x = 1 : length(img)
handles.I{x} = dicomread(fullfile(handles.cdir,img(x).name));
end
if length(img) ~= 0, set(handles.DicomListBox,'enable','on');
else
set(handles.DicomListBox,'enable','off');
end
set(handles.NofFiles,'string',handles.cdir);
set(handles.DicomListBox,'string',{img.name});
set(handles.BrightnessSlider, 'Enable', 'on');
set(handles.ContrastSlider, 'Enable', 'on');
end
guidata(hObject, handles);
% --- Executes on selection change in DicomListBox.
function DicomListBox_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
handles.output = hObject;
index = get(handles.DicomListBox,'value');
MD=max(max(handles.I{index}));
ND=min(min(handles.I{index}));
axes(handles.OriginalDicom);
imshow(handles.I{index},[ND MD]);
axes(handles.HistOriginal);
imhist(handles.I{index});
%Display Images
im = handles.I{index};
handles.Im2 = dicom2gray(im, handles.center, handles.width);
axes(handles.OutputDicom);
imshow(handles.Im2);
axes(handles.HistOutput);
imhist(handles.Im2);
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function DicomListBox_CreateFcn(hObject, eventdata, handles)
% hObject handle to DicomListBox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
return
*******And my dicom2gray function is *************
function im_gray = dicom2gray(im, center, width)
% -- convert 16-bit DICOM image into unsigned integer 8-bit based on
% window width & window center and pixel value / Hounsfield Unit(HU) -- %
% -- pixel < 0, new pixel = 0; pixel >= 81, new pixel = 255
% -- 0 < pixel <= 80, new pixel = ceil((old pixel-min)*255/max)
min = center - width/2;
max = center + width/2; % max = min + width;
[row, column] = size(im);
im = double(im);
im_gray = double(ones(row, column));
for a = 1:column
for b = 1:row
if im(b,a)<min
im_gray(b,a) = 0;
elseif im(b,a)>=min && im(b,a)<=max
im_gray(b,a) = (im(b,a)-min)*255/width;
elseif im(b,a)>max
im_gray(b,a) = 255;
end
end
end
im_gray = uint8(im_gray);

Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!