Hi, How can I detect and track an object while its moving in a video and want to know its position?
34 次查看(过去 30 天)
显示 更早的评论
Hi, How can I detect and track an object while its moving in a video and want to know its position?
采纳的回答
Image Analyst
2022-11-6
See my attached demo where I track a green Sharpie in a video.
16 个评论
Batuhan Istanbullu
2022-11-13
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
2022-11-13
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
2022-11-14
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
2022-11-14
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
2022-11-14
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
2022-11-20
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
2022-11-20
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
2022-11-20
It looks to me as if the number of frames request was being applied to an empty video.
Batuhan Istanbullu
2022-11-20
% 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^^
Batuhan Istanbullu
2022-11-20
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
2022-11-20
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
2022-11-21
I will try to search any solutions to that problem if possible, thank you for your help and attention Walter.
Have a good day!
更多回答(1 个)
Walter Roberson
2022-11-6
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)