Speed up image processing and for-loop iterations for faster centroid tracking
12 views (last 30 days)
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.
set(vid,'ReturnedColorSpace', 'RGB') %Needed this to convert YUY2 to RBG to get rid of pink coloring
%Acquire an image form the webcam
%This is the function that I exported from the colorThresholder app for filtering
% 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
Image Analyst on 9 Jul 2020
Edited: Image Analyst on 9 Jul 2020
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.