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
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
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.
  1 个评论
Scott Kaiser
Scott Kaiser 2020-7-14
编辑:Scott Kaiser 2020-7-16
Thank you for the suggestions. I reused the variable BW as suggested and I tried working in RGB colorspace with no noticable improvement in speed. I did more digging and found some code that worked quite well. Rather than looping every 1.5 seconds I now am able to process about 20-30 fps! See my new code below. Again, thank you for all your suggestions.
clear;clc
cam = imaq.VideoDevice('winvideo',1,'YUY2_640x640','ReturnedColorSpace', 'RGB');
I = step(cam);
h = imagesc(I);
set(h, 'EraseMode', 'none');
frames=1000;
for i=1:frames
%Acquire an image form the webcam
img = step(cam);
%% Send unfiltered image "img" to function creakmask(RGB) to filter out all colors except hunter orange
[BW]=HunterOrangeMask(img);
%% Find area in filtered image with largest blob area. Works only for BW image.
BW= bwareafilt(BW, 1);
%% Show and get coordinates of largestBlob's Centroid.
s = regionprops(BW, 'centroid');
centroids=cat(1, s.Centroid);
%Update image in figure
set(h, 'CData', img);
%Plot centroids ontop of img
hold on
plot(centroids(1,1), centroids(1,2),'wo','Markersize',8);
hold off
end
delete(cam);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by