There is a problem with the find function
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示



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 ?
Riccardo Scorretti
2022 年 4 月 28 日
I don't understand the question. Could you provide the full matrix bw and the expected result?
文辉 沈
2022 年 4 月 28 日
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
文辉 沈
2022 年 4 月 28 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
