image - RGB histogram using bitshift in matlab -
i'm trying create mozaic image in matlab. database consists of rgb images gray scale images.
i need calculate histograms - in example of wikipedia article color histograms - rgb images , thought using bitshift operator in matlab combine r,g , b channels.
nbins = 4; nbits = 8; index = bitshift(bitshift(image(:,:,1), log2(nbins)-nbits), 2*log2(nbins)) + ... + bitshift(bitshift(image(:,:,2), log2(nbins)-nbits), log2(nbins)) + ... + bitshift(image(:,:,3), log2(nbins)-nbits) + 1;
index matrix of same size image index corresponding bin pixel value.
how can sum occurences of unique values in matrix histogram of rgb image?
is there better approach bitshift calculate histogram of rgb image?
calculating indices
the bitshift
operator seems ok do. me create lookup relationship relates rgb value bin value. first have figure out how many bins in each dimension want. example, let's wanted 8 bins in each channel. means have total of 512 bins together. assuming have 8 bits per channel, produce relationship creates index so:
% // figure out split our bins accessred = floor(256 / num_red_bins); accessgreen = floor(256 / num_green_bins); accessblue = floor(256 / num_blue_bins); %// figures out index histogram redchan = floor(red / accessred); greenchan = floor(green / accessgreen); bluechan = floor(blue / accessblue); %// find single index out = 1 + redchan + (num_red_bins)*greenchan + (num_red_bins*num_green_bins)*bluechan;
this assumes have split our channels red
, green
, blue
. offset our indices 1 matlab indexes arrays starting @ 1. makes more sense me, bitshift
operator looks more efficient.
onto histogram question
now, supposing have indices stored in index
, can use accumarray
function that. accumarray
takes in set of locations in array, "weights" each location. accumarray
find corresponding locations weights , aggregate them together. in case, can use sum
. accumarray
isn't limited sum
. can use operation provides 1-to-1 relationship. example, suppose had following variables:
index = 1 2 3 4 5 1 1 2 2 3 3 weights = 1 1 1 2 2 2 3 3 3 4 4
what accumarray
each value of weights
, take @ corresponding value in index
, , accumulate value corresponding slot.
as such, doing (make sure index
, weights
column vectors):
out = accumarray(index, weights); out = 6 7 9 2 2
if take look, indices have value of 1, values in weights
share same index of 1 summed first slot of out
. have 3 values: 1, 2 , 3. similarly, index 2 have values of 1, 3 , 3, give 7.
now, apply application, given code, indices start @ 1. calculate histogram of image, have set of weights 1 , use accumarray
accumulate entries. therefore:
%// make sure these column vectors index = index(:); weights = ones(numel(index), 1); %// calculate histogram h = accumarray(index, weights); %// can do: %// h = accumarray(index, 1); - special case if every value %// in weights same number
accumarray
's behaviour default invokes sum
. should give need. also, should there indices missing values, (for example, suppose index of 2 missing index matrix), accumarray
conveniently place 0 in location when aggregate. makes sense right?
good luck!
Comments
Post a Comment