Access data from database via GUI,using guide

2 views (last 30 days)
Dear sir,
help me!
I have faced the following problem.GUI does not know 'NoduleDatabase' could you please help me? THanks all
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);
AdjImg=imadjust(G);
%imtool(AdjImg);
initialT=mean2(AdjImg);
T=initialT;
Image=AdjImg;
[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);
I3=imadjust(G);
%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);
I3=imadjust(G);
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
  3 Comments
Walter Roberson
Walter Roberson on 18 Feb 2017
Edited: Walter Roberson on 18 Feb 2017
In your code section in "Third try" you have
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;
Notice that you are incrementing j after the end of the "for j" loop, and you are incrementing i after the end of the "for i" loop. The next iteration of "for i" after the "for j" loop would cause the j to be reassigned back to 1, so the only reason to do that incrementing of j or i would be if there are not going to be any further iterations of i and j and for some reason you need the final values to be 1 greater than the largest values they had during the loop. In order for that to be the case, you would need to have a point later in your code in which you used the value of i or j that you had just assigned to. But you do not. The next place you use i or j is near your section "r1=170;%160" and in those places you have "for i", which ignores any previous value you had for i. So your incrementing of i and j in the section in "Third try" is just confusing and not beneficial.
Similar commentary for incrementing i and j in the section "second TRY for different images"
Walter Roberson
Walter Roberson on 18 Feb 2017
The last thing you do in the routine is
DatabaseNodules=B(idx);
That is not a shared variable or a global variable, so as soon as the function returns that variable is going to be destroyed. If you are not going to do something with the variable then there is probably no point computing it.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 18 Feb 2017
Edited: Walter Roberson on 18 Feb 2017
Near the end of your code you have
B=NoduleDatabase';
However, NoduleDatabase will only be defined if you found at least one nodule in one of your several attempts. If you go through the logic, if every attempt fails including the RemoveBack section, then nothing would be assigned to NoduleDatabase .
A cross-check that you are in that situation is that in each case where you do assign into NoduleDatabase, you index it using the variables k and p. However, you never initialize those variables, so if you had tried to do the assignments the program would have failed with undefined variables.
Near the beginning of the code you have
% k=1;p=1;
Notice that is commented out.
Your use of indexing by k and p variables implies that you expect this code to loop over something assigning into NoduleDatabase each time, but there is no such loop. Your code does produce varying number of results, using {k,p}, {k,p+1}, {k,p+2}, {k,p+3}, but there is no loop , so if you had initialized p=1 like in the comment, you might as well use {k,1}, {k,2}, {k,3}, {k,4} instead of p, p+1 etc. And likewise because there is no loop varying p, if you initialized k = 1 then you might as well just code {1,1}, {1,2}, {1,3}, {1,4}
One might almost suspect that you are expecting NoduleDatabase to retain its value between calls to pushbutton2_Callback and that you want to "add on to the end". But you do not try to retrieve an existing NoduleDatabase from having saved it anywhere, and at the end you do not save the changed NoduleDatabase to any safe place (you write it to a file but you do not keep it as a variable.)
Perhaps you want to do something like this at the beginning of the routine:
NoduleDatabase = getappdata( ancestor(hObject, 'figure'), 'NoduleDatabase');
if isempty(NoduleDatabase)
NoduleDatabase = cell(0,4);
k = 1;
p = 1;
else
k = size(NoduleDatabase, 1) + 1;
p = 1;
end
and at the end,
setappdata( ancestor(hObject, 'figure'), 'NoduleDatabase', NoduleDatabase);
Another issue that you need to address is that you are not creating the same number of entries for each time, but you are using dlmwrite() to write the data. dlmwrite() cannot handle cell arrays with different numbers of populated cells per row -- it has no way to fill in the "empty" cells.
Actually, dlmwrite cannot be used with cell arrays at all. If you were writing scalar values into the cell array and you populated the same number of entries each time, then you could use cell2mat() to convert the cell to a numeric matrix that could be written by dlmwrite(). But that will not work when you have unpopulated cells. And then there is the difficulty that you are populating the entries with the results of bwselect() multiplied by your image, which is going to give you 2D arrays in each cell, giving you a 2D cell array in which the entries are themselves either empty or 2D numeric arrays. You are not going to do well trying to write that to a text array with dlmwrite() and you also are not going to be able to write such a thing out using xlswrite().
I recommend you remove all of the dlmwrite() calls. If you need to write the NoduleDatabase out to a file, do it once at the end of the routine rather than duplicating the logic all over the place.
The system I showed of using getappdata and setappdata does not involve saving to a file, and would have no problem with cell arrays that are only partly populated.
  3 Comments
Yuki Zue
Yuki Zue on 18 Feb 2017
the sign % does not be in front of the function pushbutton2 in my original code ,sir.
Walter Roberson
Walter Roberson on 18 Feb 2017
Look at your code again. Trace through what happens if on your line
[label, Nnodule]=bwlabel(Nodule,8);%label is labeled matrix that includes labeled objects
if the result is that Nnodule is 0.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!