追跡したオブジェクトの画面上での座標の取得

大学生の初心者です。
MATLABで動かした、赤色追跡のプログラムで取得した赤い物体の位置を画面上での座標(数的な)を知りたいです。(画面内に一つ)
どのようにすれば座標データを取得できますか?
赤色追跡の参考にしたコードのサイトです。
% Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
vid = videoinput( 'winvideo',1, 'YUY2_320x240');
% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
%start the video aquisition here
start(vid)
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=200)
% Get the snapshot of the current frame
data = getsnapshot(vid);
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
%a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
%set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
hold off
end
% Both the loops end here.
% Stop the video aquisition.
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
% Clear all variables
clear all
sprintf('%s','That was all about Image tracking, Guess that was pretty easy :) ')

 Réponse acceptée

Kenta
Kenta le 23 Sep 2019

2 votes

中心の座標は以下と思います。
bc(1),bc(2)

7 commentaires

james,k
james,k le 23 Sep 2019
ご回答どうもありがとうございます。
リアルタイムで(プログラム作動中に)同時に追跡しているモノの画面上での座標をプロット出来るのでしょうか?
Kenta
Kenta le 24 Sep 2019
はい、可能です。figure; hold onを先に打って、whileループに入れば作動中にプロットできます。うえのwhileをforに変えたほうが、うまく走るかもしれません。hold onなどで、検索すれば参考になるものもたくさんあると思います。
james,k
james,k le 24 Sep 2019
ありがとうございます。早速試したのですが
・figure; hold onを打ったら実行して表示されるFigureが1,2,3,4,,,,と断続的に表示されました(追跡は可能)
・whileをforに変えたら 演算子の使用が無効です。と出て、エラーになりました。
申し訳ございません初心者で操作には慣れておらず、アドバイス通りに出来ているか分かりません。
また、hold onで調べX軸やY軸、ターゲットの設定というのを他で行って、今の追跡プログラムとリンク?させるという、仕組みなのでしょうか?
よろしければ回答お願いいたします。
Kenta
Kenta le 24 Sep 2019
Modifié(e) : Kenta le 24 Sep 2019
figure;hold on
while ...
figureを打ってから、whileに入ります。これで図が毎回は表示されないと思いますがいかがでしょうか。
もし、もう少し詳しい内容がよろしければ、上のコードで使ったデータなど、できる限り詳しく説明・共有していただけると幸いです。
james,k
james,k le 24 Sep 2019
問題なく追跡プログラムは動きましたが赤色の中心(回答であったbc(1),bc(2) )の座標の数値を表示するには構文が必要ですか?
print文などでx、y軸の座標を表示できますか?やり方をご存じでしたらお教え願います。
宜しくお願い致します。
Kenta
Kenta le 24 Sep 2019
関数textが使えると思います。こちらが参考にしてください。
また、Matlabではネットで検索すればいろいろと出てくるので、まずはそうやって調べてみても良いかもしれません。試しに、「maltab 図 値 表示」で打つと上のページが一番上に出てきました。
james,k
james,k le 25 Sep 2019
ありがとうございます。fprintfで初めに回答いただいた中心の座標を入れたら表示することができました。
とても参考になりましたありがとうございます。

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur グラフィックス パフォーマンス dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!