You are on page 1of 7

clear;

clc;
[f p]=uigetfile('*.*','');
path=[p f];
im=imread(path);
figure;
imshow(im)
title('Input Image')
flt1=medfilt2(im(:,:,1),[6 6]);
flt2=medfilt2(im(:,:,2),[6 6]);
flt3=medfilt2(im(:,:,3),[6 6]);
im(:,:,1)=flt1;
im(:,:,2)=flt2;
im(:,:,3)=flt3;
figure;
imshow(uint8(im))
title('Filtered Image')
h = fspecial('unsharp');
im = imfilter(im,h);
im=im(3:end-3,3:end-3,:);
figure;
imshow(im)
title('Unsharp Masked Image')
%%%%%%%%%%%%%%%%%%
labTransformation = makecform('srgb2lab');
img = applycform(im,labTransformation);
figure;
imshow(img)
title('Image in Lab colorspace')
figure;
subplot(2,2,1)
L=img(:,:,1);
imshow(L)
title('L')
subplot(2,2,2)
a=img(:,:,2);
imshow(a)

title('a')
subplot(2,2,3)
b=img(:,:,3);
imshow(b)
title('b')
%%%%%%%%%%%%
mx=double(max(max(b)));
mn=double(min(min(b)));

% max value in b space


% min value in b space

[r c]=size(b);
b1=imresize(b,[256 256]); % resize image to reduce time
comsumption
pts=reshape(b1,256*256,1); % convert 2D matrix to 1D vector
seg=zeros(length(pts),1);
%%%%%%%%%%
noc=3;
% no of clusters
cn=mn:(mx-mn)/2:mx; % cluster initial centers
er=Inf;
ittr=1;
while(er>1)

% clustering

ittr=ittr+1
if ittr>3
break;
end
for i=1:noc
% reinitialize clusters
cls{1,i}=[];
end
for i=1:length(pts) % for all points in image
p=double(pts(i)); % pixel value of current point (i th
point)
dist=[];
for j=1:noc
% calculate distance between current pixel
and all clusters centers
dist=[dist abs(cn(j)-p)];
end
[m pos]=min(dist);
% get minimum distance

cls{1,pos}=[cls{1,pos} p]; % assing current pixel to min


distance cluster
seg(i,1)=pos; % save cluster no
end
for k=1:noc
% calculate new cluster centers
cnn(k)=mean(cls{1,k});
end
diff=cn-cnn; % diff between prev and current centers
er=sum(diff);
cn=cnn;
end
seg=reshape(seg,256,256);
% convert vector to image
seg=imresize(seg,[r c]); % resize to original image size
nucleus=seg==1;
%
assign first cluster as nucleus
se = strel('square',5);
nucleus=imdilate(nucleus,se); % dilate image
nucleus=bwareaopen(nucleus,40);
[labele num] = bwlabel(nucleus,8); % calculate connected
components
data = regionprops(labele,'basic');
a=0;
ind=[];
for i=1:num
% keep area having max value only
ar=data(i).Area;
if ar>a
a=ar;
ind=i;
end
end
nucleus=labele==ind;
figure;
imshow(nucleus)
title('Nucleus')
% same for cyto, no of clusters =4
seg1=zeros(length(pts),1);
noc=4;
cn=mn:(mx-mn)/3:mx;
er=Inf;
ittr=1;

while(er>1)
ittr=ittr+1
if ittr>3
break;
end
for i=1:noc
cls{1,i}=[];
end
for i=1:length(pts)
p=double(pts(i));
dist=[];
for j=1:noc
dist=[dist abs(cn(j)-p)];
end
[m pos]=min(dist);
cls{1,pos}=[cls{1,pos} p];
seg1(i,1)=pos;
end
for k=1:noc
cnn(k)=mean(cls{1,k});
end
diff=cn-cnn;
er=sum(diff);
cn=cnn;
end
seg1=reshape(seg1,256,256);
seg1=imresize(seg1,[r c]);
cyto=seg1==2;
se = strel('square',5);
cyto=imdilate(cyto,se);
cyto=bwareaopen(cyto,20);
[labele num] = bwlabel(cyto,8);
data = regionprops(labele,'basic');
a=0;
ind=[];
for i=1:num
ar=data(i).Area;
if ar>a
a=ar;
ind=i;
end
end

cyto=labele==ind;
figure;
imshow(cyto)
title('Cytoplasm')
%%%%%%%%%%%%%%
im1=im;
[rn cn]=find(nucleus);
Tpn=min(rn);
Btn=max(rn);
Ltn=min(cn);
Rtn=max(cn);
im1(Tpn,Ltn:Rtn,1)=0;
im1(Tpn,Ltn:Rtn,2)=0;
im1(Tpn,Ltn:Rtn,3)=0;
im1(Btn,Ltn:Rtn,1)=0;
im1(Btn,Ltn:Rtn,2)=0;
im1(Btn,Ltn:Rtn,3)=0;
im1(Tpn:Btn,Ltn,1)=0;
im1(Tpn:Btn,Ltn,2)=0;
im1(Tpn:Btn,Ltn,3)=0;
im1(Tpn:Btn,Rtn,1)=0;
im1(Tpn:Btn,Rtn,2)=0;
im1(Tpn:Btn,Rtn,3)=0;
[rc cc]=find(cyto);
Tpc=min(rc);
Btc=max(rc);
Ltc=min(cc);
Rtc=max(cc);
im1(Tpc,Ltc:Rtc,1)=0;
im1(Tpc,Ltc:Rtc,2)=0;
im1(Tpc,Ltc:Rtc,3)=0;
im1(Btc,Ltc:Rtc,1)=0;
im1(Btc,Ltc:Rtc,2)=0;
im1(Btc,Ltc:Rtc,3)=0;
im1(Tpc:Btc,Ltc,1)=0;
im1(Tpc:Btc,Ltc,2)=0;
im1(Tpc:Btc,Ltc,3)=0;

im1(Tpc:Btc,Rtc,1)=0;
im1(Tpc:Btc,Rtc,2)=0;
im1(Tpc:Btc,Rtc,3)=0;
figure;
imshow(im1)
%%%%%%%%%%%%%%%%%%%%%%%%%
imt=nucleus(Tpn:Btn,Ltn:Rtn); % extract nucleus
[rt ct]=size(imt);
imt1=edge(double(imt),'canny'); % detect edges
N=0;
Ns=0;
for i=1:4:rt-4
% block size 4by 4
for j=1:4:ct-4
blck=imt1(i:i+3,j:j+3);
s=sum(blck(:)); % calculate edges
if s>0
% if block contains edges
Ns=Ns+1; % increment Ns by 1
end
N=N+1;
end
end
HD=log(N)/log(Ns)
%%%%%%%%%%%%%%%%%%%%%%%%%%
[rt ct]=find(imt1);
M=length(rt);
xc=mean(ct); % centriod of nucleus
yc=mean(rt);
CSx=0;
CSy=0;
for i=1:M % for all points on edge
CSx=CSx+abs(xc-ct(i)); % distance between point and centriod
CSy=CSy+abs(yc-rt(i));
end
CSx=CSx/M
CSy=CSy/M
%%%%%%%%%%%%%%%%%%%%%%%
SFarea=length(find(imt)) % nucleus area
SFperimeter=length(find(imt1)) % nucleus perimater

SFcompactness=(SFperimeter^2)/SFarea
r1=Btn-Tpn; % major and minor axes
r2=Rtn-Ltn;
if r1>r2
a=r1;
b=r2;
else
a=r2;
b=r1;
end
SFecentricity=((a*a-b*b)^0.5/a)
rds=((rt-yc).^2+(ct-xc).^2).^0.5;
SFelongation=max(rds)/min(rds)
SFformfactor=(4*pi*SFarea)/(SFperimeter^2)
cyto=cyto-nucleus;
SFarea1=length(find(cyto));
NtoCRatio=SFarea/SFarea1
if NtoCRatio <0.9
disp('Not Cancerous')
else
disp('Cancerous')
end

You might also like