There is a problem with the find function
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천



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
Jonas
2022년 4월 28일
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
채택된 답변
Riccardo Scorretti
2022년 4월 28일
편집: Riccardo Scorretti
2022년 4월 28일
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
文辉 沈
2022년 4월 28일
I know what you mean,how can i make it to put 227 as it's row ?
I don't understand the question. Could you provide the full matrix bw and the expected result?
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
plot([xa, xd],[1, 227],'b','LineWidth',2);
text((xa+xd)/2,(1+227)/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~");
This is a process of my image processing, bw data can be viewed after running.
My expectation is to calculate the length of the crack, first to get the coordinates of the boundary point, the problem is here.
%%%%%%Off topic:Of course I have other pictures (with different directions of the cracks), but before solving those pictures I want to finish the calculations for this picture
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');
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
