There is a problem with the find function
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
文辉 沈
am 28 Apr. 2022
Kommentiert: Riccardo Scorretti
am 29 Apr. 2022



Why is the y (row) of points C and D wrong? Is there something wrong with me calling the function? Isn't the output of find() an array?
1 Kommentar
Jonas
am 28 Apr. 2022
we can not check your results because your screenshot shows only the columns 133 to 144
may your view be the problem, scroll to the left and look at the first column of the last row
Akzeptierte Antwort
Riccardo Scorretti
am 28 Apr. 2022
Bearbeitet: Riccardo Scorretti
am 28 Apr. 2022
the result you obtain is quite logical. Consider the following example:
bw = zeros(4, 5) ; bw(end,2:4) = 1
You are working on the last row only:
bw(end,:)
The command find(bw(end,:)) will return all the values for which the condition bw(end,:) is verified, that is bw(end,:)~=0. These values are of course all equal to 1. In this case, find will return an array of values (yc) and an array of index (xc):
[yc, xc] = find(bw(end,:))
The command find(bw(end,:), 1, 'first') will return only one couple [value, index] (because of the argument 1) for which the condition is satisfied, starting from the beginning (because of 'first'):
[yc, xc] = find(bw(end,:), 1, 'first')
Similarly for the following case, but starting from the end:
[yd, xd] = find(bw(end,:), 1, 'last')
5 Kommentare
Riccardo Scorretti
am 29 Apr. 2022
Sorry for the delay. I'm still not sure to have understood: in your code 227 is just the size of the image (= all the cracks are more or less vertical and take the whole image)?
clc;
close all;
clear;
workspace;
format long g;
format compact;
%选择待图像输入文件夹
selpath = uigetdir(path);
if ~isequal(selpath,0)
pathname_old = selpath;
else
warndlg('selpath fail','warning');
return
end
%选择处理完成的图像输出文件夹
selpath = uigetdir(path);
if ~isequal(selpath,0)
pathname_new = selpath;
else
warndlg('selpath fail','warning');
return
end
%dir()获得指定文件夹下的所有子文件夹和文件,并存放在一种文件结构体数组
filelList = dir(fullfile(pathname_old,'*.jpg'));
n = length(filelList);
for i = 1:n
filename_old = filelList(i).name;
filename_new=strcat(filename_old(1:end-4),"_processed",".jpg");
rgbImage=imread(fullfile(pathname_old,filename_old));
%图像增强
adimage = imadjust(rgbImage,stretchlim(rgbImage));
%灰度图
I = im2gray(adimage);
%高斯低通滤波,9*9
G = fspecial('gaussian',[9,9],1);
GS = imfilter(I,G);
%阈值分割
sh = graythresh(GS);
bw = im2bw(GS,sh);
bw = ~bw;
bw = imfill(bw, 'holes');
bw = bwareafilt(bw, 1);
%第一行 起始点A 、 结束点B
[ya, xa] = find(bw(1,:), 1, 'first');
[yb, xb] = find(bw(1,:), 1, 'last');
%最后一行 起始点C 、 结束点D
[yc, xc] = find(bw(end,:), 1, 'first');
[yd, xd] = find(bw(end,:), 1, 'last');
dab = pdist2([xa, ya],[xb, yb]);
dac = pdist2([xa, ya],[xc, yc]);
dad = pdist2([xa, ya],[xd, yd]);
dbc = pdist2([xb, yb],[xc, yc]);
dbd = pdist2([xb, yb],[xd, yd]);
dcd = pdist2([xc, yc],[xd, yd]);
[mi] = [dab,dac,dad,dbc,dbd,dcd];
length = max(mi);
boundaries = bwboundaries(bw);
numberOfBoundaries = size(boundaries, 1);
imshow(rgbImage);
hold on;
for k=1:numberOfBoundaries
thisBoundary = boundaries{k};
x = thisBoundary(:,2);
y = thisBoundary(:,1);
plot(x,y,'g-','LineWidth',2);
end
val = size(bw,1); % ***
plot([xa, xd],[1, val],'b','LineWidth',2); % ***
text((xa+xd)/2,(1+val)/2,[' ','Length = ',num2str(length)],'Color','b'); % ***
hold off;
frame = getframe;
im = frame.cdata;
pathfilename_new=fullfile(pathname_new,filename_new);
imwrite(im,pathfilename_new);
end
disp("ok~");
If this is not what you need, plese clarify and send more images.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Image Processing and Computer Vision finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!