Improving rgb2hsv Performance for video
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Walter Roberson
2014년 2월 6일
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.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Green에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!