Brain Tumor Segmentation Using Convolutional Neural Network Source Code
Brain Tumor Segmentation Using Convolutional Neural Network Source Code
MRI Images
Source code:
function varargout = mainmenu(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @mainmenu_OpeningFcn, ...
'gui_OutputFcn', @mainmenu_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
% --- Outputs from this function are returned to the command line.
function varargout = mainmenu_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)
global Input1
fullpathname = strcat(pathname,filename);
text=fileread(fullfile(pathname, filename));
% disp(fullpathname);
axes(handles.axes1);
imshow(Input_Image)
handles.ImgData = Input_Image;
set(handles.edit1,'string',fullpathname);
ip_img=Input_Image;
IMG=double(rgb2gray(ip_img));
CLM = 2;
[ IN, CEN, NOF ] = SUB_FUN8( IMG, CLM );
lb = zeros(2,1);
figure;
subplot(1,3,1);
imshow(IMG,[]);
for i=1:CLM
subplot(1,3,i+1);
imshow(IN(:,:,i),[]);
end
EN_INP = mat2gray(E);
axes(handles.axes2);
imshow(EN_INP);
imshow(BW1);
axes(handles.axes2);
imshow(EN_IP);
BW_OP = bwareaopen(BW1,2000);
axes(handles.axes2);
imshow(BW_OP);
nhood = true(9);
C_BW = imclose(BW_OP,nhood);
imshow(C_BW)
R_M = imfill(C_BW,'holes');
imshow(R_M);
axes(handles.axes2);
imshow(EN_IP);
SRT = EN_IP;
SRT(R_M) = 0;
axes(handles.axes2);
imshow(SRT);
EN_INP2 = entropyfilt(SRT);
E_INP2 = mat2gray(EN_INP2);
axes(handles.axes2);
imshow(E_INP2);
BW_IN = im2bw(E_INP2,graythresh(E_INP2));
axes(handles.axes2);
imshow(BW_IN)
axes(handles.axes2);
imshow(EN_IP);
MSK = bwareaopen(BW_IN,1000);
axes(handles.axes2);
imshow(MSK);
TXT1 = EN_IP;
TXT1(~MSK) = 0;
TXT2 = EN_IP;
TXT2(MSK) = 0;
axes(handles.axes2);
imshow(TXT1);
axes(handles.axes2);
imshow(TXT2);
boundary = bwperim(MSK);
SMNT_RST = EN_IP;
SMNT_RST(boundary) = 255;
axes(handles.axes2);
imshow(SMNT_RST);
SM = stdfilt(EN_IP,nhood);
axes(handles.axes2);
imshow(mat2gray(SM));
RS = rangefilt(EN_IP,ones(5));
axes(handles.axes2);
imshow(RS);
global img2;
level=0.2;
I = Input_Image;
gray = rgb2gray(I);
% Otsu Binarization for segmentation
level = graythresh(I);
%gray = gray>80;
img = im2bw(I,.6);
img = bwareaopen(img,80);
img2 = im2bw(I);
% Try morphological operations
%gray = rgb2gray(I);
%tumor = imopen(gray,strel('line',15,0));
axes(handles.axes2)
imshow(img);
bw = im2bw(Input_Image, graythresh(Input_Image));
bw = bwareaopen(bw, 50);
L = bwlabel(bw);
s = regionprops(L,'PixelIdxList');
s=bwarea(bright_objects)
level=0.2;
IMG_CONV = im2bw(Input_Image, graythresh(Input_Image));
BW_DTC = bwlabel(IMG_CONV);
RGN_SEG = regionprops(BW_DTC,'PixelIdxList');
RGN_SEG=bwarea(BRT_OBJ)
guidata(hObject, handles);
if (bright_objects==1)
uiwait(msgbox('The segmented Image is Normal'));
else (bright_objects==2)
end
Input_Image = Input_Image+0.01*randn(size(Input_Image));
Input_Image(Input_Image<0) = 0; Input_Image(Input_Image>1) = 1;
D = 2;
SIG = [7 0.4];
Input_Image1 = DATA(Input_Image,D,SIG);
scale = 1e10;
J = scale * imnoise(Input_Image1/scale, 'poisson');
axes(handles.axes2);
imshow(J);
IMG=rgb2gray(Input_Image);
axes(handles.axes2);
imshow(IMG);
IMG_RZ=imresize(Input_Image1,[500 500]);
IMG_RZ=im2double(IMG_RZ);
level=0.8;
INP = im2bw(IMG_RZ,(level+(.009)));
INP=~INP;
CN_EG=edge(INP,'prewitt');
TG_IMG=CN_EG&INP;
Data.m
function B = DATA(Input_Image,D,SIG)
if ~exist('Input_Image','var') || isempty(Input_Image)
error('Input image Input_Image is undefined or invalid');
end
if ~isfloat(Input_Image) || ~sum([1,3] == size(Input_Image,3)) || ...
min(Input_Image(:)) < 0 || max(Input_Image(:)) > 1
error(['Input image Input_Image must be a double precision ',...
'matrix of size NxMx1 or NxMx3 on the closed ',...
'interval [0,1].']);
end
if size(Input_Image,3) == 1
B = BGRY(Input_Image,D,SIG(1),SIG(2));
else
B = BCLR(Input_Image,D,SIG(1),SIG(2));
end
function B = BGRY(Input_Image,D,SIGD,SIGR)
[X,Y] = meshgrid(-D:D,-D:D);
G = exp(-(X.^2+Y.^2)/(2*SIGD^2));
dim = size(Input_Image);
B = zeros(dim);
for i = 1:dim(1)
for j = 1:dim(2)
iMin = max(i-D,1);
iMax = min(i+D,dim(1));
jMin = max(j-D,1);
jMax = min(j+D,dim(2));
I = Input_Image(iMin:iMax,jMin:jMax);
H = exp(-(I-Input_Image(i,j)).^2/(2*SIGR^2));
F = H.*G((iMin:iMax)-i+D+1,(jMin:jMax)-j+D+1);
B(i,j) = sum(F(:).*I(:))/sum(F(:));
end
end
close(h);
function B = BCLR(Input_Image,D,SIGD,SIGR)
if exist('applycform','file')
Input_Image = applycform(Input_Image,makecform('srgb2lab'));
else
Input_Image = colorspace('Lab<-RGB',Input_Image);
end
[X,Y] = meshgrid(-D:D,-D:D);
G = exp(-(X.^2+Y.^2)/(2*SIGD^2));
SIGR = 100*SIGR;
dim = size(Input_Image);
B = zeros(dim);
for i = 1:dim(1)
for j = 1:dim(2)
iMin = max(i-D,1);
iMax = min(i+D,dim(1));
jMin = max(j-D,1);
jMax = min(j+D,dim(2));
I = Input_Image(iMin:iMax,jMin:jMax,:);
dL = I(:,:,1)-Input_Image(i,j,1);
da = I(:,:,2)-Input_Image(i,j,2);
db = I(:,:,3)-Input_Image(i,j,3);
H = exp(-(dL.^2+da.^2+db.^2)/(2*SIGR^2));
F = H.*G((iMin:iMax)-i+D+1,(jMin:jMax)-j+D+1);
norm_F = sum(F(:));
B(i,j,1) = sum(sum(F.*I(:,:,1)))/norm_F;
B(i,j,2) = sum(sum(F.*I(:,:,2)))/norm_F;
B(i,j,3) = sum(sum(F.*I(:,:,3)))/norm_F;
end
% waitbar(i/dim(1));
end
if exist('applycform','file')
B = applycform(B,makecform('lab2srgb'));
else
B = colorspace('RGB<-Lab',B);
end
ConvAnn.m
inputs = norm_feat;
targets = meas;
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, plotfit(net,inputs,targets)
%figure, plotregression(targets,outputs)
%figure, ploterrhist(errors)
CNN.m
inputs = meas;
targets = norm_feat;
if(performance<0.4)
msgbox('Stage 1: The Classification Image is Affected')
disp('Stage 1: The Classification Image is Affected')
elseif(performance<0.6)
msgbox('Stage 2: The Classification Image is Affected')
disp('Stage 2: The Classification Image is Affected')
else
msgbox('Stage 3: The Classification Image is Affected')
disp('Stage 3: The Classification Image is Affected')
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
signal1 = img2(:,:);
%Feat = getmswpfeat(signal,winsize,wininc,J,'matlab');
%Features = getmswpfeat(signal,winsize,wininc,J,'matlab');
[cA1,cH1,cV1,cD1] = dwt2(signal1,'db4');
[cA2,cH2,cV2,cD2] = dwt2(cA1,'db4');
[cA3,cH3,cV3,cD3] = dwt2(cA2,'db4');
DWT_feat = [cA3,cH3,cV3,cD3];
G = princomp(DWT_feat);
whos DWT_feat
whos G
g = graycomatrix(G);
stats = graycoprops(g,'Contrast Correlation Energy Homogeneity');
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
Mean = mean2(G);
Standard_Deviation = std2(G);
Entropy = entropy(G);
RMS = mean2(rms(G));
%Skewness = skewness(img)
Variance = mean2(var(double(G)));
a = sum(double(G(:)));
Smoothness = 1-(1/(1+a));
Kurtosis = kurtosis(double(G(:)));
Skewness = skewness(double(G(:)));
% Inverse Difference Movement
m = size(G,1);
n = size(G,2);
in_diff = 0;
for i = 1:m
for j = 1:n
temp = G(i,j)./(1+(i-j).^2);
in_diff = in_diff+temp;
end
end
IDM = double(in_diff);
load Trainset.mat
load Normalized_Features.mat
xdata = meas;
group = label;
svmStruct1 = svmtrain(xdata,group,'kernel_function', 'linear');
species = svmclassify(svmStruct1,feat,'showplot',false);
% --- Outputs from this function are returned to the command line.
function varargout = analysis_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)
Analysis.m
Data.m
if nargin < 2
N_CLTR = 2;
end
for i=1:N_CLTR
CNTR(i,1) = sum(sum(UP_VAL(:,:,i).*INP_IMG))/sum(sum(UP_VAL(:,:,i)));
end
pre_obj_fcn = 0;
for i=1:N_CLTR
pre_obj_fcn = pre_obj_fcn + sum(sum((UP_VAL(:,:,i) .*INP_IMG -
CNTR(i)).^2));
end
fprintf('Initial objective fcn = %f\n', pre_obj_fcn);
Unow = zeros(size(UP_VAL));
for i=1:row
for j=1:col
for uII = 1:N_CLTR
tmp = 0;
for uJJ = 1:N_CLTR
disUp = abs(INP_IMG(i,j) - CNTR(uII));
disDn = abs(INP_IMG(i,j) - CNTR(uJJ));
tmp = tmp + (disUp/disDn).^(2/(E_N-1));
end
Unow(i,j, uII) = 1/(tmp);
end
end
end
now_obj_fcn = 0;
for i=1:N_CLTR
now_obj_fcn = now_obj_fcn + sum(sum((Unow(:,:,i) .*INP_IMG -
CNTR(i)).^2));
end
fprintf('Iter = %d, Objective = %f\n', ITR, now_obj_fcn);
if max(max(max(abs(Unow-UP_VAL))))<EPLN_VL || abs(now_obj_fcn -
pre_obj_fcn)<EPLN_VL %ÒýÈë2¸öÅж¨
break;
else
UP_VAL = Unow.^E_N;
for i=1:N_CLTR
CNTR(i,1) =
sum(sum(UP_VAL(:,:,i).*INP_IMG))/sum(sum(UP_VAL(:,:,i)));
end
pre_obj_fcn = now_obj_fcn;
end
end