如何在 Matlab 中绘制色块
How can I draw color blocks in Matlab
我已经从图像中提取了颜色。
然后我想像这张图一样在图片下方显示颜色和颜色名称。
但是我不会画色块。请帮助我。
没有现成的绘制那种色块的Matlab函数。
您可以用几行代码绘制它。
我故意保持代码简单(没有循环、数组和结构):
%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');
结果:
我已经从图像中提取了颜色。 然后我想像这张图一样在图片下方显示颜色和颜色名称。
但是我不会画色块。请帮助我。
没有现成的绘制那种色块的Matlab函数。
您可以用几行代码绘制它。
我故意保持代码简单(没有循环、数组和结构):
%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');
结果: