There is a problem with the find function

2 views (last 30 days)
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 Comment
Jonas
Jonas on 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

Sign in to comment.

Accepted Answer

Riccardo Scorretti
Riccardo Scorretti on 28 Apr 2022
Edited: Riccardo Scorretti on 28 Apr 2022
Dear 文辉 沈 ,
the result you obtain is quite logical. Consider the following example:
bw = zeros(4, 5) ; bw(end,2:4) = 1
bw = 4×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
You are working on the last row only:
bw(end,:)
ans = 1×5
0 1 1 1 0
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,:))
yc = 1×3
1 1 1
xc = 1×3
2 3 4
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')
yc = 1
xc = 2
Similarly for the following case, but starting from the end:
[yd, xd] = find(bw(end,:), 1, 'last')
yd = 1
xd = 4
  5 Comments
文辉 沈
文辉 沈 on 28 Apr 2022
The 1 and 227 in the script are just for this picture, so my idea is whether I can output 1 and 227 automatically instead of manually entering it
plot([xa, xd],[1, 227],'b','LineWidth',2);
text((xa+xd)/2,(1+227)/2,[' ','Length = ',num2str(length)],'Color','b');
Riccardo Scorretti
Riccardo Scorretti on 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!