MATLAB: How to calculate the proportion of the color in Lab coordinate

image processingImage Processing Toolbox

I wish to calculate the proportion of the color in an image in the L*a *b space, but I don't how to implement in Matlab code.
The idea is as follow:
  1. load a png file and convert the RGB space to Lab space via "rgb2lab";
  2. divide the L vector into three section: [0, 30], [31, 61], [62, 100], divide the a vector into three sections: [-128, -44], [-43, 41], [42, 127], divide the b vector into three sections: [-128, -44], [-43, 41], [42, 127]. After dividing, we now have 3*3*3=27 sub-sapces in the L*a*b space;
  3. calculate the amount of pixel each L*a*b sub-space contains and the proportion of pixel in each L*a*b sub-space.
Could anyone tell me how to implement the idea above?

Best Answer

Try this example
im = imread('peacock.jpg');
im_lab = rgb2lab(im);
L = round(im_lab(:,:,1));
a = round(im_lab(:,:,2));
b = round(im_lab(:,:,3));
L_partition = {[0, 30], [31, 61], [62, 100]};
a_partition = {[-128, -44], [-43, 41], [42, 127]};
b_partition = {[-128, -44], [-43, 41], [42, 127]};
combs = combvec(1:3,1:3,1:3).';
in_range = @(frame, range) (frame >= range(1)) & (frame <= range(2));
result = cell(size(combs,1), 4);
for i=1:size(combs,1)
comb = combs(i,:);
l_part = L_partition{comb(1)};
a_part = a_partition{comb(2)};
b_part = b_partition{comb(3)};
pxlSum = sum(in_range(L,l_part).*in_range(a,a_part).*in_range(b,b_part), 'all');
result(i,:) = [{l_part} {a_part} {b_part} {pxlSum}];
end
T = cell2table(result, 'VariableNames', {'L range', 'a range', 'b range', 'pixel count'});
disp(T)
Result:
L range a range b range pixel count
_________ ____________ ____________ ___________
0 30 -128 -44 -128 -44 0
31 61 -128 -44 -128 -44 0
62 100 -128 -44 -128 -44 0
0 30 -43 41 -128 -44 1008
31 61 -43 41 -128 -44 6352
62 100 -43 41 -128 -44 364
0 30 42 127 -128 -44 0
31 61 42 127 -128 -44 1
62 100 42 127 -128 -44 0
0 30 -128 -44 -43 41 0
31 61 -128 -44 -43 41 0
62 100 -128 -44 -43 41 0
0 30 -43 41 -43 41 103521
31 61 -43 41 -43 41 518927
62 100 -43 41 -43 41 204572
0 30 42 127 -43 41 0
31 61 42 127 -43 41 0
62 100 42 127 -43 41 0
0 30 -128 -44 42 127 0
31 61 -128 -44 42 127 0
62 100 -128 -44 42 127 0
0 30 -43 41 42 127 0
31 61 -43 41 42 127 314
62 100 -43 41 42 127 1293
0 30 42 127 42 127 0
31 61 42 127 42 127 0
62 100 42 127 42 127 0