Generating a HSV Histogram without For-loops
3 views (last 30 days)
Show older comments
Hi,
I'm creating a video processing tool in MatLab, which works, but my code is extremely slow, which I believe is due to my use of for-loops.
One example of this is in creating a HSV Histogram for a video. The code I have is as follows:
function [hsvHist] = generateHSVHist(hsvVid, numRows, numColumns, numFrames)
H = zeros(12,1);
S = zeros(4,1);
V = zeros(4,1);
for frame = 1 : numFrames
for row = 1 : numRows
for column = 1 : numColumns
h = hsvVid(row,column,1,frame);
s = hsvVid(row,column,2,frame);
v = hsvVid(row,column,3,frame);
idxH = ceil(h*12);
idxS = ceil(s*4);
idxV = ceil(v*4);
%ensure no zero indices
if idxH == 0
idxH = 1;
end
if idxS == 0
idxS = 1;
end
if idxV == 0
idxV = 1;
end
H(idxH) = H(idxH)+1;
S(idxS) = S(idxS)+1;
V(idxV) = V(idxV)+1;
end
end
end
%Normalise the histograms
totalPixels = numFrames * numRows * numColumns;
H = H/totalPixels;
S = S/totalPixels;
V = V/totalPixels;
%Concatenate the histograms
hsvHist = [H;S;V];
end
Do you guys have any tips or advice on how I can approach this problem without using for-loops? Any help is greatly appreciated.
Thanks,
Gareth
0 Comments
Accepted Answer
Walter Roberson
on 6 Feb 2014
No loop needed.
h = hsvVid(:,:,1,frame);
idxH = max(1, ceil(h*12));
H = histc(idxH(:), 1:12);
More Answers (0)
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!