2017-04-14 320 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