You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Hi, How can I detect and track an object while its moving in a video and want to know its position?
33 views (last 30 days)
Show older comments
Hi, How can I detect and track an object while its moving in a video and want to know its position?
Accepted Answer
Image Analyst
on 6 Nov 2022
See my attached demo where I track a green Sharpie in a video.
16 Comments
Batuhan Istanbullu
on 13 Nov 2022
Thanks for the help!, but I just want to change my color to blue and in order to do that I need to change your parameters for green to blue; How I am able to do that ? I mean how can I find thresholds for blue at HSV? Do you mind helping me more ? cause if you will, it means a lot for my research at my MSc.
Image Analyst
on 13 Nov 2022
You can use the Color Thresholder app on the Apps tab of the tool ribbon to determine the thresholds for your image(s).
Batuhan Istanbullu
on 14 Nov 2022
I did change the HSV values to mine but I couldn't find any data related to this particular part in your code;
hsv = rgb2hsv(double(thisFrame));
hue=hsv(:,:,1);
sat=hsv(:,:,2);
val=hsv(:,:,3);
my new HSV values;
hThresholds = [0.969, 0.588];
sThresholds = [0.000, 0.386];
vThresholds = [0.959, 1.000];
Batuhan Istanbullu
on 14 Nov 2022
And also do I need these values for detecting an object during my recorded video?, or are these values only for saturation and other purposes?
Image Analyst
on 14 Nov 2022
If you're trying to find vividly colored items, then the sThreshold is wrong and the hThreshold is reversed. They should be
hThresholds = [0.588, 0.969];
sThresholds = [0.386, 1.000];
vThresholds = [0.959, 1.000];
ALso check the vThreshold because that looks awfully high.
Batuhan Istanbullu
on 20 Nov 2022
I will check it, but before checking I just want to show you this; I changed the video and hsv values of the desired thing that I want but I got this errors. I must say I don't need to have histogramic graphs and hue, saturation and value part. All I want is the exact position of the image by every time it moves. How may I implement some codes and change to what I want? Any help would means a lot to me
Batuhan Istanbullu
on 20 Nov 2022
and to answer your question or more than a question your advice I did check it with the color thresholder app and it gives me that data cause I want to find a whitee led light that has been placed on my finger in a video
Walter Roberson
on 20 Nov 2022
It looks to me as if the number of frames request was being applied to an empty video.
Batuhan Istanbullu
on 20 Nov 2022
% Demo to track green color. Finds and annotates centroid and bounding box of green blobs.
% Modify thresholds to detect different colors.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Specify input video file name.
folder = pwd;
% fullFileName = 'rhinos.avi';
% fullFileName = 'traffic.avi';
baseFileName = 'BlueLed.wmv';
fullFileName = fullfile(folder, baseFileName);
% Instantiate a video reader object for this video.
videoObject = VideoReader(fullFileName);
% Setup other parameters
numberOfFrames = videoObject.NumberOfFrame;
% Set HSV thresholds for the green sharpie in the demo video.
% Modify the thresholds to detect different colors.
hThresholds = [0.588, 0.969];
sThresholds = [0.386, 1.000];
vThresholds = [0.959, 1.000];
% Read one frame at a time, and find specified color.
for k = 1 : numberOfFrames
% Read one frame
thisFrame=read(videoObject,k);
hImage=subplot(3, 4, 1);
% Display it.
imshow(thisFrame);
axis on;
caption = sprintf('Original RGB image, frame #%d 0f %d', k, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow;
hsv = rgb2hsv(double(thisFrame));
hue=hsv(:,:,1);
sat=hsv(:,:,2);
val=hsv(:,:,3);
if k == 1
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
hCheckbox = uicontrol('Style','checkbox',...
'Units', 'Normalized',...
'String', 'Finish Now',...
'Value',0,'Position', [.2 .96 .4 .05], ...
'FontSize', 14);
end
% Compute histograms for H, S, and V channels
% Let's compute and display the histogram.
binaryH = hue >= hThresholds(1) & hue <= hThresholds(2);
binaryS = sat >= sThresholds(1) & sat <= sThresholds(2);
binaryV = val >= vThresholds(1) & val <= vThresholds(2);
% Overall color mask is the AND of all the masks.
coloredMask = binaryH & binaryS & binaryV;
% Filter out small blobs.
coloredMask = bwareaopen(coloredMask, 500);
[labeledImage, numberOfRegions] = bwlabel(coloredMask);
if numberOfRegions >= 1
stats = regionprops(labeledImage, 'BoundingBox', 'Centroid');
% Delete old texts and rectangles
if exist('hRect', 'var')
delete(hRect);
end
if exist('hText', 'var')
delete(hText);
end
% Display the original image again.
subplot(3, 4, 5); % Switch to original image.
hImage=subplot(3, 4, 5);
imshow(thisFrame);
axis on;
hold on;
caption = sprintf('%d blobs found in frame #%d 0f %d', numberOfRegions, k, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow;
%This is a loop to bound the colored objects in a rectangular box.
for r = 1 : numberOfRegions
% Find location for this blob.
thisBB = stats(r).BoundingBox;
thisCentroid = stats(r).Centroid;
hRect(r) = rectangle('Position', thisBB, 'EdgeColor', 'r', 'LineWidth', 2);
hSpot = plot(thisCentroid(1), thisCentroid(2), 'y+', 'MarkerSize', 10, 'LineWidth', 2)
hText(r) = text(thisBB(1), thisBB(2)-20, strcat('X: ', num2str(round(thisCentroid(1))), ' Y: ', num2str(round(thisCentroid(2)))));
set(hText(r), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
hold off
drawnow;
end
% See if they want to bail out
if get(hCheckbox, 'Value')
% Finish now checkbox is checked.
msgbox('Done with demo.');
return;
end
end
msgbox('Done with demo.');
here is the code itself too^^
Walter Roberson
on 20 Nov 2022
I do not see a link to the video, and I do not see your current code?
Batuhan Istanbullu
on 20 Nov 2022
and this is picture of the video, not a whole video but a screenshot of it. I want to track that led light during video and that finger is moving
Walter Roberson
on 20 Nov 2022
That video is Microsoft ASF codec. That codec is not supported on MacOS. When I convert the video to m4v using VLC then the program runs without problem for me.
I suspect that your system is not reading the video properly.
I find some indications that historically those kinds of videos were supported by MATLAB on Windows, but that there were difficulties that the frame count could not be determined until the file had been completely read. I do not know if that is still the case.
Batuhan Istanbullu
on 21 Nov 2022
I will try to search any solutions to that problem if possible, thank you for your help and attention Walter.
Have a good day!
More Answers (1)
Walter Roberson
on 6 Nov 2022
If this is in the context of Driving (video is really a camera), see https://www.mathworks.com/help/driving/ref/multiobjecttracker.html
If this were in the context of multiple sensors then see https://www.mathworks.com/help/fusion/multi-object-trackers.html
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)