Don't understand the following script for image evaluation
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hey guys,
I have a script for image evaluation (for an image like in the attachment) and unfortunately I don't understand one part. The reading of the image I understand. I don't understand the middle part with "%%%%FINDING Lengths in TOP ROW" (I have marked the part boldly).
What exactly happens there?
Any help would be greatly appreciated!
________________________________________________________________________
,%referncesize=input('Please Enter Refernce size in nm/Pixel: ');
close all
clear
clc
referncesize=690;
%Opening image
[imagefiles ,pathname] = uigetfile({'*.bmp'},'MultiSelect', 'on'); inputimage=strcat(pathname, imagefiles);
files = string(inputimage);
imfiles = string(imagefiles);
for k = 1:length(files)
try
disp(files(k))
I = imread(files(k));
J=rgb2gray(I);
J = imadjust(J);
J = medfilt2(J);
level = graythresh(J);
BW=imbinarize(J,level);
BW = medfilt2(BW);
se = strel('disk',0,0);
erodedBW = imerode(BW,se);
%%%%% CHANGE HERE FOR OTHER RESULTS: USUALLY: [...]('disk',10,0)
se = strel('disk',0,0);
erodedBW = imdilate(erodedBW,se);
figure
imshow(erodedBW)
%%%%FINDING Lengths in TOP ROW
[row,column]=size(erodedBW);
startingpointr1=[];
endingpointr1=[];
startingflag=0;
tempflag=0;
for i=1:column
if erodedBW(1,i)==1
tempflag=tempflag+1;
if tempflag>5
if startingflag==0
startingpointr1=[startingpointr1 i];
startingflag=1;
end
end
else
if tempflag>5
startingflag=0;
endingpointr1=[endingpointr1 i];
end
tempflag=0;
end
end
%%%% FINDING LENGTH IN MIDDLE
startingpointr2=[];
endingpointr2=[];
startingflag=0;
tempflag=0;
for i=1:column
if erodedBW(row/2,i)==1
tempflag=tempflag+1;
if tempflag>5
if startingflag==0
startingpointr2=[startingpointr2 i];
startingflag=1;
end
end
else
if tempflag>5
startingflag=0;
endingpointr2=[endingpointr2 i];
end
tempflag=0;
end
end
%%%% FINDING LENGTH IN Bottom
startingpointr3=[];
endingpointr3=[];
startingflag=0;
tempflag=0;
for i=1:column
if erodedBW(row,i)==1
tempflag=tempflag+1;
if tempflag>5
if startingflag==0
startingpointr3=[startingpointr3 i];
startingflag=1;
end
end
else
if tempflag>5
startingflag=0;
endingpointr3=[endingpointr3 i];
end
tempflag=0;
end
end
%%%%%%%%%%%%%%%FIND COLUMN WISE WIDTH
startingpointc1=[];
endingpointc1=[];
startingflag=0;
tempflag=0;
for i=1:row
if erodedBW(i,1)==1
tempflag=tempflag+1;
if tempflag>5
if startingflag==0
startingpointc1=[startingpointc1 i];
startingflag=1;
end
end
else
if tempflag>5
startingflag=0;
endingpointc1=[endingpointc1 i];
end
tempflag=0;
end
end
%%%%%%%%%COLUMN WISE IN MIDDLE
startingpointc2=[];
endingpointc2=[];
startingflag=0;
tempflag=0;
for i=1:row
if erodedBW(i,(column/2))==1
tempflag=tempflag+1;
if tempflag>5
if startingflag==0
startingpointc2=[startingpointc2 i];
startingflag=1;
end
end
else
if tempflag>5
startingflag=0;
endingpointc2=[endingpointc2 i];
end
tempflag=0;
end
end
%%%%%%%%%COLUMN WISE IN END
startingpointc3=[];
endingpointc3=[];
startingflag=0;
tempflag=0;
for i=1:row
if erodedBW(i,(column))==1
tempflag=tempflag+1;
if tempflag>5
if startingflag==0
startingpointc3=[startingpointc3 i];
startingflag=1;
end
end
else
if tempflag>5
startingflag=0;
endingpointc3=[endingpointc3 i];
end
tempflag=0;
end
end
%startingpointr1
%endingpointr1
r1c1=endingpointr1(1)-startingpointr1(1);
r1c2=endingpointr1(2)-startingpointr1(2);
%startingpointr2
%endingpointr2
r2c1=endingpointr2(1)-startingpointr2(1);
r2c2=endingpointr2(2)-startingpointr2(2);
%startingpointr3
%endingpointr3
r3c1=endingpointr3(1)-startingpointr3(1);
r3c2=endingpointr3(2)-startingpointr3(2);
%startingpointc1
%endingpointc1
r11c1=endingpointc1(1)-startingpointc1(1);
r22c1=endingpointc1(2)-startingpointc1(2);
%startingpointc2
%endingpointc2
r11c2=endingpointc2(1)-startingpointc2(1);
r22c2=endingpointc2(2)-startingpointc2(2);
%startingpointc3
%endingpointc3
r11c3=endingpointc3(1)-startingpointc3(1);
r22c3=endingpointc3(2)-startingpointc3(2);
T1 = {'Position' 'Value (nm)';
'r1c1' r1c1*referncesize;
'r1c2' r1c2*referncesize;
'r2c1' r2c1*referncesize;
'r2c2' r2c2*referncesize;
'r3c1' r3c1*referncesize;
'r3c2' r3c2*referncesize;
'r11c1' r11c1*referncesize;
'r22c1' r22c1*referncesize;
'r11c2' r11c2*referncesize;
'r22c2' r22c2*referncesize;
'r11c3' r11c3*referncesize;
'r22c3' r22c3*referncesize;
};
T = cell2table(T1);
writetable(T,'data.xlsx','Sheet',imfiles(k),'WriteVariableNames',false)
catch
T1 = {'Position' 'Value (nm)';};
T = cell2table(T1);
writetable(T,'data.xlsx','Sheet',imfiles(k),'WriteVariableNames',false)
end
end
0 Commenti
Risposte (1)
Simran
il 18 Feb 2025
I see you want to understand what the “Finding lengths in TOP ROW” part of the code you mentioned. The explaination goes as below:
1.)
[row, column] = size(erodedBW);
startingpointr1 = [];
endingpointr1 = [];
startingflag = 0;
tempflag = 0;
Here “startingpointr1” and “endingpointr1” are initialised as emplty arrays. These will store the starting and ending column indices of detected segments in the top row. Then “startingflag” and “emptyflag” are intialised to 0. These flags help in detecting the start and end of a continuous segment of pixels.
2.)
% Loop through each column in the first row
for i = 1:column
if erodedBW(1, i) == 1 % Check if the pixel is part of a segment
tempflag = tempflag + 1; % Increment the temporary counter
if tempflag > 5 % Check if the segment length is significant
if startingflag == 0 % If a new segment is detected
startingpointr1 = [startingpointr1 i]; % Record the starting point
startingflag = 1; % Set the flag indicating a segment has started
end
end
else
if tempflag > 5 % If the segment ends and was significant
startingflag = 0; % Reset the starting flag
endingpointr1 = [endingpointr1 i]; % Record the ending point
end
tempflag = 0; % Reset the temporary counter
end
end
Here the code is divided into two segments- “Detection of Segments” and “End of Segment”.
“Detection of Segments”-
1.) Here If a pixel is part of a segment (erodedBW(1, i) == 1), ”tempflag” is incremented. If ”tempflag” exceeds 5, it then indicates there is a continuous segment of at least 6 pixels, hence suggesting a significant structure.
2.) If the “startingflag” is 0, it then means the start of a new segment is detected, and the current index ”i” is added to ”startingpointr1”. ”startingflag” is then set to 1 to mark that a segment has started.
“End of Segment”
1.) If a pixel is not part of a segment (erodedBW(1, i) == 0), and tempflag is greater than 5, it then marks the end of a segment. The current index ”i” is added to ”endingpointr1”, and ”startingflag” is reset to 0.
2.) “tempflag” is reset to 0 whenever a non-segment pixel is encountered.
This process effectively identifies the start and end of segments in the top row of the binary image, allowing the calculation of lengths of these segments.
You can refer these documentation links for more details on the functions used in the script:
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!