# combine 2 image results of segmentation, into one of axes

15 views (last 30 days)
erick saputra on 29 Sep 2015
Edited: Yuki Zue on 18 Feb 2017
-----------------------------------------------------------------------------------------------------------------
I want to ask, I hope you see this can help me, thanks I've done segmentation on that image and has 9 parts segmentation, I just want to take segmentation to figures 5 and 6, but I can only take segmentation in figure 5.
I want to ask how to combine figure 5 and figure 6 on the one axes as the final result of segmentation ..
note: I only use one image only, "Lat1.bmp" and I want to know how to combine the two figures on the one axes as the final result of segmentation
Yuki Zue on 18 Feb 2017
Dear sir,
help me!
if true
% function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'image')
% do stuff
Image=handles.image;
I=Image;
k=1;p=1;
dim=ndims(I);
if dim==2
I=I;
else if dim==3
I=rgb2gray(I);
else fprintf('Nothing \n');
end
end
%imtool(I);
I=uint8(I);
I2=imopen(I,strel('disk',3));%before 3
%figure,imshow(I);
M=medfilt2(I2,[3 3]);
%imtool(M);
G=imgaussfilt(M,1);
%imtool(G);
%image=imsharpen(G);
T=initialT;
[row,col]=size(Image);
newT=0;
while T-newT~=0
for i=1:row
for j=1:col
if Image(i,j)>=T;
Image(i,j)=Image(i,j);
else Image(i,j)=0;
end
j=j+1;
end
i=i+1;
end
%figure,imshow(Image);
m1=mean2(Image);
%m2=mean2(image);
newT=(m1)/2;
formerT=T;
T=newT;
newT=formerT;
end
%figure,imshow(Image);
%imtool(image);
[R,C]=size(Image);
for i=1:R
for j=1:C
if Image(i,j)==0
Image(i,j)=0;
else Image(i,j)=255;
end
end
j=j+1;
end
i=i+1;
Final=Image;
%imtool(Final);
openImg1=imopen(Final,strel('disk',4));% for 273.jpg //ealier 6 & 8
%imtool(openImg);
EdgeImg=edge(openImg1,'canny');
%figure,imshow(EdgeImg);
ThinnedImg=bwmorph(EdgeImg,'thin');
%figure,imshow(ThinnedImg);
Td=uint8(ThinnedImg);
Constructed=Td|openImg1;
%figure,imshow(Constructed);
OPENimg=double(Constructed);
%figure,imshow(OPENimg);
[ROW,COL]=size(OPENimg);
cropped = openImg1(1:ROW,1:COL); %%Crop region of interest
%imtool(cropped);
thresholded = cropped==0;%%Threshold to isolate lungs
%figure,imshow(thresholded);
%imtool(thresholded);
clearThresh = imclearborder(thresholded); %%Remove border artifacts in image
%figure,imshow(clearThresh);
ClosedClear=imclose(clearThresh,strel('disk',5));
%imtool(clearThresh);
Liver = bwareaopen(ClosedClear,100); %Remove objects less than 100 pixels
%figure,imshow(Liver);
Liver1 = imfill(Liver,'hole'); % fill in the vessels inside the lungs
%figure,imshow(Liver1);
Liver1=uint8(Liver1);
nodule=Liver1.*cropped;
%imtool(nodule);
nodule1=imopen(nodule,strel('disk',5));% 6 ,7and 8 before
%imtool(nodule1);
nodule1a=imclose(nodule1,strel('disk',1));
nodule1a=logical(nodule1a);
nodule2=bwareafilt(nodule1a,[1 4500]);%extract objects whose values are in these range
%imtool(nodule2);
nodule2a=bwareaopen(nodule2,150); % ealier 150,changed for 322.jpg
%imtool(nodule2a);
Nodule=nodule2a;
figure,imshow(Nodule);
[label, Nnodule]=bwlabel(Nodule,8);%label is labeled matrix that includes labeled objects
if Nnodule==1
I=double(I);
Nodule= Nodule.* I;
NoduleDatabase{k,p}=Nodule;
dlmwrite('NoduleData.dat',NoduleDatabase);
else if Nnodule>1
if Nnodule==2
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
NoduleDatabase{k,p}=NoduleOne;
NoduleDatabase{k,p+1}=NoduleTwo;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
if Nnodule==3
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
[Row3, Col3]=find(label==3);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
Nodule3=bwselect(label,Col3,Row3,8);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
Nodule3= Nodule3.* I;
NoduleDatabase{k,p}=NoduleOne;
NoduleDatabase{k,p+1}=NoduleTwo;
NoduleDatabase{k,p+2}=Nodule3;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
if Nnodule>3
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
[Row3, Col3]=find(label==3);
[Row4, Col4]=find(label==4);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
Nodule3=bwselect(label,Col3,Row3,8);
Nodule4=bwselect(label,Col4,Row4,8);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
Nodule3= Nodule3.* I;
Nodule4=Nodule4.*I;
NoduleDatabase{k,p}=NoduleOne;
NoduleDatabase{k,p+1}=NoduleTwo;
NoduleDatabase{k,p+2}=Nodule3;
NoduleDatabase{k,p+3}=Nodule4;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
%imtool(Nodule);imtool(NoduleOne);imtool(NoduleTwo);
else % second TRY for different images
I2=imopen(I,strel('disk',3));%before 3
%figure,imshow(I2);
M=medfilt2(I2,[9 9]);
G=imgaussfilt(M,1);
%imtool(I3);
T=100;
[r,c]=size(I3);
for i=1:r
for j=1:c
if I3(i,j)<T
I3(i,j)=0;
else if I3(i,j)>=T
I3(i,j)=255;
else fprintf('No');
end
end
end
j=j+1;
end
i=i+1;
%imtool(I3);
openImg=imclose(I3,strel('disk',5));%before 3
%imtool(openImg);
EdgeImg=edge(openImg,'canny');
%figure,imshow(EdgeImg);
ThinnedImg=bwmorph(EdgeImg,'thin');
%figure,imshow(ThinnedImg);
Td=uint8(ThinnedImg);
Constructed=Td|openImg;
%figure,imshow(Constructed);
OPENimg=double(Constructed);
%figure,imshow(OPENimg);
[ROW,COL]=size(OPENimg);
cropped = openImg(1:ROW,1:COL);
%imtool(cropped);
thresholded = cropped==0;
%imtool(thresholded);
clearThresh = imclearborder(thresholded);
%figure,imshow(clearThresh);
ClosedClear=imclose(clearThresh,strel('disk',5));
%imtool(clearThresh);
Liver = bwareaopen(ClosedClear,100);
%figure,imshow(Liver);
Liver1 = imfill(Liver,'hole');
%figure,imshow(Liver1);
Liver1=uint8(Liver1);
nodule=Liver1.*cropped;
%imtool(nodule);
nodule1=imopen(nodule,strel('disk',5));
%imtool(nodule1);
nodule1a=imclose(nodule1,strel('disk',1));
nodule1a=logical(nodule1a);
nodule2=bwareafilt(nodule1a,[1 4500]);
%imtool(nodule2);
nodule2a=bwareafilt(nodule2,1);
Nodule=nodule2a;
figure,imshow(Nodule);
[label, Nnodule]=bwlabel(Nodule,8);
if Nnodule==1
I=double(I);
Nodule= Nodule.* I;
NoduleDatabase{k,p}=Nodule;
dlmwrite('NoduleData.dat',NoduleDatabase);
else if Nnodule>1
if Nnodule==2
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
NoduleDatabase{k,p}=NoduleOne;
NoduleDatabase{k,p+1}=NoduleTwo;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
if Nnodule==3
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
[Row3, Col3]=find(label==3);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
Nodule3=bwselect(label,Col3,Row3,8);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
Nodule3= Nodule3.* I;
NoduleDatabase{k,p}=NoduleOne;
NoduleDatabase{k,p+1}=NoduleTwo;
NoduleDatabase{k,p+2}=Nodule3;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
if Nnodule>3
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
[Row3, Col3]=find(label==3);
[Row4, Col4]=find(label==4);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
Nodule3=bwselect(label,Col3,Row3,8);
Nodule4=bwselect(label,Col4,Row4,8);
dlmwrite('NoduleData.dat',NoduleDatabase);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
Nodule3= Nodule3.* I;
Nodule4=Nodule4.*I;
NoduleDatabase{k,p}=NoduleOne;
NoduleDatabase{k,p+1}=NoduleTwo;
NoduleDatabase{k,p+2}=Nodule3;
NoduleDatabase{k,p+3}=Nodule4;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
else % Third TRY
M=medfilt2(I,[9 9]);
G=imgaussfilt(M,1);
T=17;
[r,c]=size(I3);
for i=1:r
for j=1:c
if I3(i,j)<T
I3(r,c)=0;
else if I3(i,j)>=T
I3(i,j)=255;
else fprintf('No');
end
end
end
j=j+1;
end
i=i+1;
%imtool(I3);
openImg=imclose(I3,strel('disk',5));
%imtool(openImg);
EdgeImg=edge(openImg,'canny');
%figure,imshow(EdgeImg);
ThinnedImg=bwmorph(EdgeImg,'thin');
%figure,imshow(ThinnedImg);
Td=uint8(ThinnedImg);
Constructed=Td|openImg;
%figure,imshow(Constructed);
OPENimg=double(Constructed);
%figure,imshow(OPENimg);
[ROW,COL]=size(OPENimg);
cropped = openImg(1:ROW,1:COL);
%imtool(cropped);
thresholded = cropped==0;
%imtool(thresholded);
clearThresh = imclearborder(thresholded);
%figure,imshow(clearThresh);
ClosedClear=imclose(clearThresh,strel('disk',5));
%imtool(clearThresh);
Liver = bwareaopen(ClosedClear,100);
%figure,imshow(Liver);
Liver1 = imfill(Liver,'hole');
%figure,imshow(Liver1);
Liver1=uint8(Liver1);
nodule=Liver1.*cropped;
%imtool(nodule);
nodule1=imopen(nodule,strel('disk',5));
%imtool(nodule1);
nodule1a=imclose(nodule1,strel('disk',1));
nodule1a=logical(nodule1a);
nodule2=bwareafilt(nodule1a,[1 4500]);
%imtool(nodule2);
nodule2a=bwareafilt(nodule2,1);
%imtool(nodule2a);
Nodule=nodule2a;
figure,imshow(Nodule);
[label, Nnodule]=bwlabel(Nodule,8);
if Nnodule==1
I=double(I);
Nodule = Nodule .* I;
NoduleDatabase{k,p}=Nodule;
dlmwrite('NoduleData.dat',NoduleDatabase);
else if Nnodule>1
if Nnodule==2
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
NoduleDatabase{k,p}=NoduleOne;
NoduleDatabase{k,p+1}=NoduleTwo;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
if Nnodule==3
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
[Row3, Col3]=find(label==3);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
Nodule3=bwselect(label,Col3,Row3,8);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
Nodule3= Nodule3.* I;
NoduleDatabase{k,p}=NoduleOne;
NoduleDatabase{k,p+1}=NoduleTwo;
NoduleDatabase{k,p+2}=Nodule3;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
if Nnodule>3
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
[Row3, Col3]=find(label==3);
[Row4, Col4]=find(label==4);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
Nodule3=bwselect(label,Col3,Row3,8);
Nodule4=bwselect(label,Col4,Row4,8);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
Nodule3= Nodule3.* I;
Nodule4=Nodule4.*I;
NoduleDatabase{k,p}=NoduleOne;
NoduleDatabase{k,p+1}=NoduleTwo;
NoduleDatabase{k,p+2}=Nodule3;
NoduleDatabase{k,p+3}=Nodule4;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
else
openImg1=imclose(openImg1,strel('disk',4));%before 5
%imtool(openImg1);
openImg1=logical(openImg1);
RemoveBack=bwareafilt(openImg1,[1 4500]);% remove BACKGROUND
%imtool(RemoveBack);
r1=170;%160
c1=512;
for i=1:r1
for j=1:c1
RemoveBack(i,j)=0;
end
j=j+1;
end
RemoveBack=bwareafilt(RemoveBack,[1 4500]);
RemovedBack1=RemoveBack;
%imtool(RemovedBack1);
r2=512;
c2=512;
for i=370:r2%360 but Changed for 272.jpg
for j=1:c2
RemovedBack1(i,j)=0;
end
j=j+1;
end
RemovedBack2=RemovedBack1;
%imtool(RemovedBack2);
r3=512;
c3=512;
for i=1:r3
for j=425:c3%410
RemovedBack2(i,j)=0;
end
j=j+1;
end
RemovedBack3=RemovedBack2;
r4=512;
c4=85;%60
for i=1:r4
for j=1:c4
RemovedBack3(i,j)=0;
end
j=j+1;
end
RemovedBack4=RemovedBack3;
Nodule=bwareafilt(RemovedBack4,1);% retain the LARGEST NODULE
Nodule=imfill(Nodule,'holes');
imtool(Nodule);
[label, Nnodule]=bwlabel(Nodule,8);
if Nnodule==1
I=double(I);
Nodule= Nodule.* I;
NoduleDatabase{k,p}=Nodule;
dlmwrite('NoduleData.dat',NoduleDatabase);
else if Nnodule>1
[Row1, Col1]=find(label==1);
[Row2, Col2]=find(label==2);
NoduleOne=bwselect(label,Col1,Row1,8);
NoduleTwo=bwselect(label,Col2,Row2,8);
%imtool(Nodule);imtool(NoduleOne);imtool(NoduleTwo);
I=double(I);
NoduleOne= NoduleOne.* I;
NoduleTwo=NoduleTwo.*I;
NoduleDatabase{1,p}=NoduleOne;
NoduleDatabase{1,p+1}=NoduleTwo;
dlmwrite('NoduleData.dat',NoduleDatabase);
end
end
end
end
end
end
end
end
B=NoduleDatabase';
idx=~cellfun(@isempty,B);
DatabaseNodules=B(idx);
end
end
GUI does not know NoduleDatabase.

Jon on 29 Sep 2015
Edited: Jon on 29 Sep 2015
I may not understand the question, but why not just use segmented portions 5 and 6 as a mask on the original BW image? I guess the workflow would be something like
- extract the segments
- make an image of "falses" the same size as the original called BWsegs
- set the indices of segment 5 as "true" in BWsegs
- set the indices of segment 6 as "true" in BWsegs
The BWsegs is what you're looking for. You can use regionprops to get the bounding box for cropping if you'd like.
##### 2 CommentsShowHide 1 older comment
Jon on 29 Sep 2015
It looks to me like Figure 5 = "hasil akhir segmentasi". So you know how to extract Figure 5. It also looks like you know how to extract Figure 6. So just extract them (without cropping them) and use
single_fig = Fig5 | Fig6;
Then you can crop that if you want.

erick saputra on 29 Sep 2015
please help me... I want to combine the figures 5 and 6, and displays it on "hasil akhir segmentasi". But I do not know how to combine figure 5 and 6 into a single figure
##### 2 CommentsShowHide 1 older comment
Khaing Zin Htwe on 18 Feb 2017
please help me sir, I want to show the segmented image in GUI but it does not show complete image, why? Thanks all.