matlab:颜色对象的跟踪和视频中的位置

时间:2018-09-04 20:03:56

标签: matlab object position tracking color-tracking

我是使用Matlab的新手,我开始研究如何从其颜色中检测物体。我正在尝试执行的操作是获取检测到的对象的位置并将它们保存在文件中,以便随帧获取时间绘制它们。确实,我需要找到一种在文件中也包含帧获取时间的方法。 我发布了代码:

 clear all 

a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);


% Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
vid = videoinput(camera_name, camera_id, format);

% Settiamo le proprietà dell'oggetto video
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;

% Iniziamo la presa dati del video
start(vid)


% Settiamo il numero di frame da acquisire (in questo caso si ferma dopo
 % 100 frames)
 while(vid.FramesAcquired<=200)

% Get the snapshot of the current frame
data = getsnapshot(vid);

%Per tracciare gli oggetti in tempo reale bisogna 
%sottrarre la componente rossa dall'immagine scalata dei grigi
diff_im = imsubtract(data(:,:,1), rgb2gray(data));

%Usiamo un median filter per filtrare via il rumore
diff_im = medfilt2(diff_im, [3 3]);

% Conversione dell'immagine nella scala dei grigi in binario
diff_im = im2bw(diff_im,0.18);


%Per migliorare ancora la presa dati eliminiamo tutti i pixel
%inferiori ad una certa soglia
% 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

% Ciclo per chiudere gli oggetti rossi in un box rettangolare
 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');

    posizione_x = num2str(round(bc(1)));
    posizione_y = num2str(round(bc(2)));
 end

hold off

end
 % Chiusura dei due cicli.

 % Stoppiamo la presa dati
 stop(vid);

 % Flush all the image data stored in the memory buffer.
 flushdata(vid);

 % Ripuliamo tutte le variabili
 clear all
 sprintf('%s','Fine  tracking :) ')

0 个答案:

没有答案
相关问题