display values in a loop

14 ビュー (過去 30 日間)
Shi Wei Foo
Shi Wei Foo 2011 年 12 月 5 日
i have this code to find all the centroids which objects have the specific area. however, I couldnt find a way to display the values and maybe pass the values to a variable(s). I really appreciate it if somebody could help me with some demos.
below is part of the code:
id = find([s.Area] > 1000 & [s.Area] < 100000 );
for ii = 1:length(id);
hold on, plot(s(id(ii)).Centroid(1),s(id(ii)).Centroid(2),'wp','MarkerSize',10,'MarkerFaceColor','r'), hold off
i want to do something like.. %disp(['Center location is (',num2str(s(id(ii)).Centroid(1),4),', ',num2str(s(id(ii)).Centroid(2),4),')'])
thanks in advance!
  2 件のコメント
David Young
David Young 2011 年 12 月 5 日
What is the problem with the code you have? I tried your suggested call to disp and it seems to work fine.
Shi Wei Foo
Shi Wei Foo 2011 年 12 月 5 日
I'm not sure on that as well. been cracking my head for this. was the syntax wrong?

サインインしてコメントする。

採用された回答

Chandra Kurniawan
Chandra Kurniawan 2011 年 12 月 5 日
Hello,
Here I give you my sample code
clear; clc;
I = imread('pillsetc.png');
bw = im2bw(I);
open = bwareaopen(bw,5);
SE = strel('square',5);
close = imclose(open, SE);
fill = imfill(close,'holes');
stat = regionprops(fill,'area','centroid');
id = find([stat.Area] > 1000 & [stat.Area] < 100000 );
imshow(I); hold on;
for x = 1 : length(id)
plot(stat(id(x)).Centroid(1),stat(id(x)).Centroid(2),'ro');
a(x) = stat(id(x)).Centroid(1);
b(x) = stat(id(x)).Centroid(2);
cenText = strcat('Center location is (',num2str(a(x),4),', ',num2str(b(x),4),')');
text(a(x)-80,b(x)+20,cenText,'color','g','fontweight','bold');
disp(cenText);
end
The x-centroid stored in variable 'a' and
the y-centroid stored in variable 'b'
Then both of figure and command window will show you the text that you want to display.
  2 件のコメント
Chandra Kurniawan
Chandra Kurniawan 2011 年 12 月 5 日
Note : Detected objects only for 1000 < stat.area < 10000
Hope this will helps you
Shi Wei Foo
Shi Wei Foo 2011 年 12 月 5 日
thank you so much it works fine!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by