Improving rgb2hsv Performance for video

2 views (last 30 days)
Hi, I'm working on a video processing tool and have the following code for creating a HSV and greyscale version of a video:
function [hsvVid, greyVid] = convertToHSVAndGrey(vid, numFrames)
tic
for frame = 1 : numFrames
hsvVid(:,:,:,frame) = rgb2hsv(vid(:,:,:,frame));
greyVid(:,:,:,frame) = rgb2gray(vid(:,:,:,frame));
end
toc
end
The rgb2hsv conversion takes roughly 16x longer than then greyscale conversion.
Does anyone a way to speed this up/optimise the conversion to hsv?
Thanks in advance for your help.
Cheers,
Gareth

Accepted Answer

Walter Roberson
Walter Roberson on 6 Feb 2014
Part of the overhead is the error checking. But rgb2hsv is just more complicated than rgb2gray. rgb2gray is a simple linear scaling, A * r + B * b + C * g for fixed scalar doubles A, B, C. rgb2hsv has to work with mins and max's and different formulae depending which channel is the max, and it has a bunch of border cases to take care of.
If you have the memory, then with no loops,
T = reshape( permute(vid, [1 2 4 3]), size(vid,1), size(vid,2) * size(vid,4), size(vid,3));
hsvVid = permute(reshape( rgb2hsv(T), size(vid,1), size(vid,2), size(vid,4), size(vid,3) ), [1 2 4 3]);
greyVid = reshape( rgb2gray(T), size(vid,1), size(vid,2), size(vid,4));
This rearranges the frames as if it was single much wider RGB video, does a conversion on that (so only one call overhead instead of one per frame), and re-forms back to a series of frames.
The memory-shuffling time could easily amount to more than the overhead of all of the calls.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!