Speed up image processing and for-loop iterations for faster centroid tracking
2 次查看(过去 30 天)
显示 更早的评论
Hello! I have some code that is working quite well to find the centroid of an object in my live video stream(via winvideo) that is a "hunter orange" color. The code records in a 1x2 array the coordinates of the centroid every for-loop iteration. Timing this process takes between 1.0-1.5 seconds, on average, per loop. Meaning I only get new centroid coords every 1.5 seconds. I would like to speed this up but I am unsure of how to go about this. Note that the color filter code was obtained from the colorthresholder app where I used the HSV colorspace and simply exported the code. Shown below is my main function which calls this code. Any suggestions for speeding up this code would be much appreciated! Thanks for all the help!
%% Main function to run HunterOrangeMask
clear; close all; clc; imaqreset;
% Create video input object.
vid= videoinput('winvideo',1,'YUY2_640x640');
set(vid,'ReturnedColorSpace', 'RGB') %Needed this to convert YUY2 to RBG to get rid of pink coloring
frames=2000;
for i=1:frames
%Acquire an image form the webcam
img=getsnapshot(vid);
%This is the function that I exported from the colorThresholder app for filtering
[BW]=HunterOrangeMask(img);
% Find spot in the filtered image with largest blob area.
largestBlob = bwareafilt(BW, 1);
%find the centre of blob
s = regionprops(largestBlob, 'centroid');
%1x2 Array with x-y coords of centroid
centroids=cat(1, s.Centroid)
end
stop(vid)
delete(vid);
1 个评论
Bjorn Gustavsson
2020-7-9
Use the matlab profiler to record where your algorithm spends its time. Then you can start to hunt bottlenecks.
The code you shown above is a very neat-looking code - but also impossible to gain any relevant information from to address your question.
回答(1 个)
Image Analyst
2020-7-9
编辑:Image Analyst
2020-7-9
You could try to do it in RGB colorspace. That would be faster, though perhaps not as precise, however if it's a quite distinct color with no others like it, then it should work.
See attached demo for my demo of HSV tracking in a video. But like I said, you could just do it in RGB color space and then you wouldn't be doing tons of time consuming math.
And perhaps just reuse BW instead of creating a new variable largestBlob.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!