2017-04-14 478 views
1

我已经从图像中提取颜色。 然后我想显示图像下面的颜色和颜色名称,就像这张图片一样。如何在Matlab中绘制颜色块

enter image description here

但我不知道怎么画色块。请帮帮我。

回答

1

没有准备好的Matlab函数来绘制那种颜色块。
你可以用几行代码来绘制它。

  • 使用plot函数绘制正方形(作为图形标记)。
  • 使用text函数来绘制文本。
  • 使用dec2hex将每两个十六进制数字转换为红色绿色和蓝色的颜色值。

我刻意保持代码的简单(无循环,数组和结构):

%Read image from imgur hosting sight 
I = imread('https://i.stack.imgur.com/z6Hlh.jpg'); 

figure, imshow(I), hold on 

%x1, y1 - center coordinate of upper square. 
x1 = 150; 
y1 = 330; 
text1 = '#684630'; %Color as hex string. 

%Convert hex string to RGB triple. 
color1 = hex2dec([text1(2:3); text1(4:5); text1(6:7)]); 

x2 = x1; 
y2 = y1+25; 
text2 = '#211310'; 
color2 = hex2dec([text2(2:3); text2(4:5); text2(6:7)]); 

x3 = x2; 
y3 = y2+25; 
text3 = '#b2b0ae'; 
color3 = hex2dec([text3(2:3); text3(4:5); text3(6:7)]); 

%Plot squares as markers 
plot(x1, y1, 'square', 'MarkerSize', 15, 'MarkerEdgeColor', color1/255, 'MarkerFaceColor', color1/255); 
plot(x2, y2, 'square', 'MarkerSize', 15, 'MarkerEdgeColor', color2/255, 'MarkerFaceColor', color2/255); 
plot(x3, y3, 'square', 'MarkerSize', 15, 'MarkerEdgeColor', color3/255, 'MarkerFaceColor', color3/255); 

%Plot text 
text(x1+20, y1, text1, 'FontSize', 12, 'FontName', 'Courier New', 'FontWeight', 'bold'); 
text(x2+20, y2, text2, 'FontSize', 12, 'FontName', 'Courier New', 'FontWeight', 'bold'); 
text(x3+20, y3, text3, 'FontSize', 12, 'FontName', 'Courier New', 'FontWeight', 'bold'); 

结果:
enter image description here