How do I draw a colour bar with known discrete RGB values and known percentage of pixels represent each RGB values?

2 views (last 30 days)
Hi,
I extracted 3 key colours from an image using clustering method. The RGB values of the 3 colours are as below:
RGB1=[24 70 187]; (blue)
RGB2=[208 118 22]; (orange)
RGB3=[88 90 28]; (olive green)
My question is:
How to draw a colour bar (shown below) in matlab with the 3 known colours and the known percentage of the pixels represent each colour (blue 50%, orange 30%, and olive green 20%). In this fashion it tells that the dominant colour extracted from the image is blue followed by orange, then olive green.
Currently this colour bar was drawn in photoshop unfortunately.

Accepted Answer

Jan
Jan on 24 Jan 2018
Edited: Jan on 24 Jan 2018
As pixel image:
Map = [24 70 187; 208 118 22; 88 90 28] / 255;
Ind = repmat(repelem([1, 2, 3], [50, 30, 20]), 40, 1);
RGB = ind2rgb(Ind, Map);
figure;
image(RGB)
Or as patch:
Map = [24 70 187; 208 118 22; 88 90 28] / 255;
a = 0.5; b = 0.8;
vert = [0 0; 0 1; a 1; a 0; b 1; b 0; 1, 1; 1, 0];
face = [1, 2, 3, 4; 4, 3, 5, 6; 6, 5, 7, 8];
figure;
patch('Vertices', vert, 'Faces', face, ...
'FaceVertexCData', Map, ...
'FaceColor', 'flat', 'EdgeColor', 'none')
  2 Comments
Salad Box
Salad Box on 24 Jan 2018
Hi Jan,
Thanks for your answer. It did solve 99.9% of my problem. The only 0.01% unsolved is that with your code for pixel image I get:
Instead of getting the output image with a slightly high height (h), how can I get a low height (h1) so it will look more like what I initially request? That would be really helpful.
Jan
Jan on 24 Jan 2018
Edited: Jan on 25 Jan 2018
You can control the dimension of a displayed image by setting the 'Position' of the axes:
axes('Position', [0.1, 0.1, 0.8, 0.1], ...
'visible', 'off', 'NextPlot', 'add');
image(RGB);
Or by defining the Y-limits. The same works for the patch object.

Sign in to comment.

More Answers (1)

Benjamin Kraus
Benjamin Kraus on 24 Jan 2018
You can use a patch object for this.
values = [0.5 0.3 0.2];
colors = [24 70 187; 208 118 22; 88 90 28]/255;
n = numel(values);
x = [0 cumsum(values)].*[1;1];
y = ones(2,n+1).*[1;0];
vertices = [x(:) y(:)];
faces = [1 2 4 3]+((2:2:2*n)-2)';
p = patch(...
'Vertices',vertices,...
'Faces',faces,...
'FaceVertexCData', colors, ...
'FaceColor', 'flat');
xlim([-0.1 1.1]);
ylim([-0.1 1.1]);

Community Treasure Hunt

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

Start Hunting!