Lung cancer detection and 3D modelling
clc %clear screen
close all %close all the windows
clear all % clear all variables in workspace
%% user inputs
%n=input('Enter number of images');
%z=input('Z stack motion in mm');
%length width
n=3; % number of input images
%% main loop
for i = 1:1:n %for loop till n
%% loop reading of images
a='C:\Data\September\20\'; %path
b=num2str(i); %number to string
c='.jpg' % file extension
path=strcat(a,b,c); % joins strings
I=imread(path); %reads image
figure(),imshow(I) % display to be commented
%% size of image resolution
[m n z]=size(I); % read the size
%%checking gray image
if(z==3) %to check colorimage z==3 RGB plane
I=rgb2gray(I); %convert
end
figure(),imshow(I)
%% cropping limits
u=ceil(m/10); %to be modified as per the image
v=ceil(n/10); %to be modified as per the image
I=I(u:m-u,v:n-v);
figure(),imshow(I);
%I=edge
end
close all %close all the windows
clear all % clear all variables in workspace
%% user inputs
%n=input('Enter number of images');
%z=input('Z stack motion in mm');
%length width
n=3; % number of input images
%% main loop
for i = 1:1:n %for loop till n
%% loop reading of images
a='C:\Data\September\20\'; %path
b=num2str(i); %number to string
c='.jpg' % file extension
path=strcat(a,b,c); % joins strings
I=imread(path); %reads image
figure(),imshow(I) % display to be commented
%% size of image resolution
[m n z]=size(I); % read the size
%%checking gray image
if(z==3) %to check colorimage z==3 RGB plane
I=rgb2gray(I); %convert
end
figure(),imshow(I)
%% cropping limits
u=ceil(m/10); %to be modified as per the image
v=ceil(n/10); %to be modified as per the image
I=I(u:m-u,v:n-v);
figure(),imshow(I);
%I=edge
end
close all
clear all
%for
%a=C:\Data\September\14\project data\bangalore\Lung CT to Bindhu 1\Pt1\CT.14-0255_
%b=num2str
%c
%d= jpg
[X, map] = dicomread('C:\Data\September\14\project data\bangalore\Lung CT to Bindhu 1\Pt1\CT.14-0255_75.dcm');
montage(X, map, 'Size', [2 5]);
info = dicominfo('C:\Data\September\14\project data\bangalore\Lung CT to Bindhu 1\Pt1\CT.14-0255_75.dcm');
Y = dicomread(info);
figure, imshow(Y);
%imcontrast;
%Y=uint8(Y)
imwrite(Y,filename)
clear all
%for
%a=C:\Data\September\14\project data\bangalore\Lung CT to Bindhu 1\Pt1\CT.14-0255_
%b=num2str
%c
%d= jpg
[X, map] = dicomread('C:\Data\September\14\project data\bangalore\Lung CT to Bindhu 1\Pt1\CT.14-0255_75.dcm');
montage(X, map, 'Size', [2 5]);
info = dicominfo('C:\Data\September\14\project data\bangalore\Lung CT to Bindhu 1\Pt1\CT.14-0255_75.dcm');
Y = dicomread(info);
figure, imshow(Y);
%imcontrast;
%Y=uint8(Y)
imwrite(Y,filename)
Tumor
clear all %no variables
close all %no figures
clc %empty command window
prefix = 'C:\Data\March15\3\Pt1\CT.14-0255_'; %file name prefix
A=input ('Enter data 1 ddmmyyyyhhmmss');
fnum = 34:75; %file numbers
ext = '.dcm'; %extension
fname = [prefix num2str(fnum(1)) ext];
info = dicominfo(fname)
%extract size info from metadata (nobkpt)
voxel_size = [info.PixelSpacing; info.SliceThickness]'
Zstep=info.SliceThickness;
%read slice images; populate XYZ matrix
hWaitBar = waitbar(0,'Reading DICOM files');
for i=length(fnum):-1:1
fname = [prefix num2str(fnum(i)) ext];
D(:,:,i) = uint16(dicomread(fname));
waitbar((length(fnum)-i)/length(fnum))
end
delete(hWaitBar)
whos D
%% cropping
%----------------------------------------------------------------------
D1=D(150:350,70:450,:);
D=D1;
%explore image data using Image Viewer GUI tool
for i = 12 %middle slice
im = squeeze(D(:,:,i));
max_level = double(max(D(:)));
imtool(im,[0 max_level])
end
%custom display - image data
fig1 = figure;
max_level = double(max(D(:)));
figure()
imshow(im,[0 max_level])
%title('Coronal Slice #30')
set(fig1,'position',[601 58 392 314])
imtool close all
%add intensity legend
colorbar
%change colormap
colormap jet
%end
p=1;
l=1;
for i=1:1:41 %images
I=D(:,:,i);
I=double(I);
Imax=max(max(I));
I=round(255*I/Imax);
I=uint8(I);
I1=I;
%figure()
%imshow((I));
I=edge(I,'canny',0.5);
%figure()
%imshow((I));
[m n]= size(I)
for row=1:1:m
for col=1:1:n
if(I(row,col)==1)
x(p)=row;
y(p)=col;
z(p)=i;
p=p+1;
end
end
end
for row=1:1:m
for col=1:1:n
if(I1(row,col)<50) %why 50
I1(row,col)=0;
else
I1(row,col)=255;
end
end
end
Ift=detecttumor(I1,i);
%figure()
%imshow(Ift);
[m n]=size(Ift)
for row=1:1:m
for col=1:1:n
if(Ift(row,col)==0)
else
x2(l)=row;
y2(l)=col;
z2(l)=i;
l=l+1;
end
end
end
end
j=1;
for i=1:15:(p-1)
x1(j)=x(i);
y1(j)=y(i);
z1(j)=z(i);
j=j+1;
end
figure(24)
plot3(x1,y1,z1,'.')
hold all
figure(24)
plot3(x2,y2,z2,'r')
x3=x2;
y3=y2;
z3=z2;
save('test.mat','x3','y3','z3');
%reapeat
clear all
clc
load('test.mat','x3','y3','z3');
prefix = 'C:\Data\March15\3\Pt1\CT.14-0255_'; %file name prefix
B=input ('Enter time differance =') %12012015120000- 15022015140000
fnum = 34:75; %file numbers
ext = '.dcm'; %extension
fname = [prefix num2str(fnum(1)) ext];
info = dicominfo(fname)
%extract size info from metadata (nobkpt)
voxel_size = [info.PixelSpacing; info.SliceThickness]'
Zstep=info.SliceThickness;
%read slice images; populate XYZ matrix
hWaitBar = waitbar(0,'Reading DICOM files');
for i=length(fnum):-1:1
fname = [prefix num2str(fnum(i)) ext];
D(:,:,i) = uint16(dicomread(fname));
waitbar((length(fnum)-i)/length(fnum))
end
delete(hWaitBar)
whos D
%% Visualization
%----------------------------------------------------------------------
D1=D(150:350,70:450,:);
D=D1;
%explore image data using Image Viewer GUI tool
for i = 12 %middle slice
im = squeeze(D(:,:,i));
max_level = double(max(D(:)));
imtool(im,[0 max_level])
end
%custom display - image data
fig1 = figure;
max_level = double(max(D(:)));
figure()
imshow(im,[0 max_level])
%title('Coronal Slice #30')
set(fig1,'position',[601 58 392 314])
imtool close all
%add intensity legend
colorbar
%change colormap
colormap jet
p=1;
l=1;
for i=1:1:41
I=D(:,:,i);
I=double(I);
Imax=max(max(I));
I=round(255*I/Imax);
I=uint8(I);
I1=I;
%figure()
%imshow((I));
I=edge(I,'canny',0.5);
%figure()
%imshow((I));
[m n]=size(I)
for row=1:1:m
for col=1:1:n
if(I(row,col)==1)
x(p)=row;
y(p)=col;
z(p)=i;
p=p+1;
end
end
end
for row=1:1:m
for col=1:1:n
if(I1(row,col)<50)
I1(row,col)=0;
else
I1(row,col)=255;
end
end
end
Ift=detecttumor(I1,i);
figure()
imshow(Ift);
[m n]=size(Ift)
for row=1:1:m
for col=1:1:n
if(Ift(row,col)==0)
else
x2(l)=row;
y2(l)=col;
z2(l)=i;
l=l+1;
end
end
end
end
j=1;
for i=1:15:(p-1)
x1(j)=x(i);
y1(j)=y(i);
z1(j)=z(i);
j=j+1;
end
figure(25)
plot3(x1,y1,z1,'.')
hold all
figure(25)
plot3(x2,y2,z2,'r')
deltax=x2-x3
deltay=y2-y3
deltaz=z2-z3
close all %no figures
clc %empty command window
prefix = 'C:\Data\March15\3\Pt1\CT.14-0255_'; %file name prefix
A=input ('Enter data 1 ddmmyyyyhhmmss');
fnum = 34:75; %file numbers
ext = '.dcm'; %extension
fname = [prefix num2str(fnum(1)) ext];
info = dicominfo(fname)
%extract size info from metadata (nobkpt)
voxel_size = [info.PixelSpacing; info.SliceThickness]'
Zstep=info.SliceThickness;
%read slice images; populate XYZ matrix
hWaitBar = waitbar(0,'Reading DICOM files');
for i=length(fnum):-1:1
fname = [prefix num2str(fnum(i)) ext];
D(:,:,i) = uint16(dicomread(fname));
waitbar((length(fnum)-i)/length(fnum))
end
delete(hWaitBar)
whos D
%% cropping
%----------------------------------------------------------------------
D1=D(150:350,70:450,:);
D=D1;
%explore image data using Image Viewer GUI tool
for i = 12 %middle slice
im = squeeze(D(:,:,i));
max_level = double(max(D(:)));
imtool(im,[0 max_level])
end
%custom display - image data
fig1 = figure;
max_level = double(max(D(:)));
figure()
imshow(im,[0 max_level])
%title('Coronal Slice #30')
set(fig1,'position',[601 58 392 314])
imtool close all
%add intensity legend
colorbar
%change colormap
colormap jet
%end
p=1;
l=1;
for i=1:1:41 %images
I=D(:,:,i);
I=double(I);
Imax=max(max(I));
I=round(255*I/Imax);
I=uint8(I);
I1=I;
%figure()
%imshow((I));
I=edge(I,'canny',0.5);
%figure()
%imshow((I));
[m n]= size(I)
for row=1:1:m
for col=1:1:n
if(I(row,col)==1)
x(p)=row;
y(p)=col;
z(p)=i;
p=p+1;
end
end
end
for row=1:1:m
for col=1:1:n
if(I1(row,col)<50) %why 50
I1(row,col)=0;
else
I1(row,col)=255;
end
end
end
Ift=detecttumor(I1,i);
%figure()
%imshow(Ift);
[m n]=size(Ift)
for row=1:1:m
for col=1:1:n
if(Ift(row,col)==0)
else
x2(l)=row;
y2(l)=col;
z2(l)=i;
l=l+1;
end
end
end
end
j=1;
for i=1:15:(p-1)
x1(j)=x(i);
y1(j)=y(i);
z1(j)=z(i);
j=j+1;
end
figure(24)
plot3(x1,y1,z1,'.')
hold all
figure(24)
plot3(x2,y2,z2,'r')
x3=x2;
y3=y2;
z3=z2;
save('test.mat','x3','y3','z3');
%reapeat
clear all
clc
load('test.mat','x3','y3','z3');
prefix = 'C:\Data\March15\3\Pt1\CT.14-0255_'; %file name prefix
B=input ('Enter time differance =') %12012015120000- 15022015140000
fnum = 34:75; %file numbers
ext = '.dcm'; %extension
fname = [prefix num2str(fnum(1)) ext];
info = dicominfo(fname)
%extract size info from metadata (nobkpt)
voxel_size = [info.PixelSpacing; info.SliceThickness]'
Zstep=info.SliceThickness;
%read slice images; populate XYZ matrix
hWaitBar = waitbar(0,'Reading DICOM files');
for i=length(fnum):-1:1
fname = [prefix num2str(fnum(i)) ext];
D(:,:,i) = uint16(dicomread(fname));
waitbar((length(fnum)-i)/length(fnum))
end
delete(hWaitBar)
whos D
%% Visualization
%----------------------------------------------------------------------
D1=D(150:350,70:450,:);
D=D1;
%explore image data using Image Viewer GUI tool
for i = 12 %middle slice
im = squeeze(D(:,:,i));
max_level = double(max(D(:)));
imtool(im,[0 max_level])
end
%custom display - image data
fig1 = figure;
max_level = double(max(D(:)));
figure()
imshow(im,[0 max_level])
%title('Coronal Slice #30')
set(fig1,'position',[601 58 392 314])
imtool close all
%add intensity legend
colorbar
%change colormap
colormap jet
p=1;
l=1;
for i=1:1:41
I=D(:,:,i);
I=double(I);
Imax=max(max(I));
I=round(255*I/Imax);
I=uint8(I);
I1=I;
%figure()
%imshow((I));
I=edge(I,'canny',0.5);
%figure()
%imshow((I));
[m n]=size(I)
for row=1:1:m
for col=1:1:n
if(I(row,col)==1)
x(p)=row;
y(p)=col;
z(p)=i;
p=p+1;
end
end
end
for row=1:1:m
for col=1:1:n
if(I1(row,col)<50)
I1(row,col)=0;
else
I1(row,col)=255;
end
end
end
Ift=detecttumor(I1,i);
figure()
imshow(Ift);
[m n]=size(Ift)
for row=1:1:m
for col=1:1:n
if(Ift(row,col)==0)
else
x2(l)=row;
y2(l)=col;
z2(l)=i;
l=l+1;
end
end
end
end
j=1;
for i=1:15:(p-1)
x1(j)=x(i);
y1(j)=y(i);
z1(j)=z(i);
j=j+1;
end
figure(25)
plot3(x1,y1,z1,'.')
hold all
figure(25)
plot3(x2,y2,z2,'r')
deltax=x2-x3
deltay=y2-y3
deltaz=z2-z3
function Ift=detecttumor(I1,i)
level = graythresh(I1);
bw = im2bw(I1,level);
bw = bwareaopen(bw, 50); %noise
cc = bwconncomp(bw, 4)
J=cc.NumObjects
%figure()
%imshow(bw)
graindata = regionprops(cc, 'basic')
grain_areas = [graindata.Area];
grain = false(size(bw));
for k=1:1:J
if(grain_areas(k)<1000) %user
if(grain_areas(k)>600) %user
tumor_area=grain_areas(k)
grain = false(size(bw));
grain(cc.PixelIdxList{k}) = true;
%figure()
%imshow(grain);
end
end
end
Ift=grain;
level = graythresh(I1);
bw = im2bw(I1,level);
bw = bwareaopen(bw, 50); %noise
cc = bwconncomp(bw, 4)
J=cc.NumObjects
%figure()
%imshow(bw)
graindata = regionprops(cc, 'basic')
grain_areas = [graindata.Area];
grain = false(size(bw));
for k=1:1:J
if(grain_areas(k)<1000) %user
if(grain_areas(k)>600) %user
tumor_area=grain_areas(k)
grain = false(size(bw));
grain(cc.PixelIdxList{k}) = true;
%figure()
%imshow(grain);
end
end
end
Ift=grain;