2015-06-16 21 views
0

我曾嘗試以下:是否有MATLAB腳本可以給我一個圖像中所有顏色的定量/視覺描述?

b=imread('/home2/s163720/lebron.jpg'); 
hsv = rgb2hsv(b); 
h = hsv(:,:,1); 
imhist(h,16) 

但是,它並沒有給我完全是我要找的。能夠看到不同色調的某個櫃檯,甚至是顏色的分佈,真是太棒了。 這將不勝感激。

+0

是否有任何特殊的原因,你只使用16箱?我不確定你想從中得到什麼,但是'imhist'已經顯示了每個箱子(色相)中像素數的圖表。 – beaker

回答

0

我認爲這可能符合您的要求。

%Read the image 
img = imread('/home2/s163720/lebron.jpg'); 

%convert to hsv and reshap to a N x 3 matrix 
hsv = rgb2hsv(img); 
hsv2 = squeeze(reshape(hsv, [], 1, 3)); 

%Extract hue (and convert to an angle) and saturation 
Hue = 2*pi*hsv2(:,1); 
Saturation = hsv2(:,2); 

%The number of bins in the hue and saturation directions 
RadialColorBins = 50; 
AngularColorBins = 50; 

%Where the edged of the bins are 
edges = {linspace(0, 1, RadialColorBins), linspace(0, 2*pi, AngularColorBins) - 2*pi/AngularColorBins}; 

%bin the data 
[heights,centers] = hist3([Saturation, Hue],'Edges',edges); 

%Extract the centers 
radius = centers{1}; 
angle = centers{2}; 

%Force periodicity 
angle = [angle, angle(1)]; 
heights = [heights, heights(:, 1)]'; 

%Mesh the r and theta components 
[Radius, Angle] = meshgrid(radius, angle); 

%Make a color map for the polar plot 
CMap = hsv2rgb(Angle/(2*pi), Radius, ones(size(Radius))); 

figure(1) 
imshow(img) 

%polar histogram in s-h space 
figure(2) 
surf(Radius.*cos(Angle), Radius.*sin(Angle), heights, CMap,'EdgeColor','none'); 
+0

謝謝!太棒了。 –

0

我知道這應該是答案應該在的地方,但我相當肯定你的問題沒有一個通用的答案。然而,有幾種可能性,也許你或其他人可以提出更多。

所以,你需要一個直方圖的基本問題是,你必須選擇顏色的一些代表作爲一個單一的數字。這是一個相當困難的問題。

第一種解決方案可能是將rgb顏色轉換爲波長,然後忽略圖像的強度。使用這個想法的問題是,rgb顏色比單獨的波長定義更多的顏色。參見:http://jp.mathworks.com/matlabcentral/newsreader/view_thread/313712

第二種解決方案可能是將數字定義爲A = sum(rgb。* [1,10,100]);然後使用此數字作爲您的顏色表示。

第三個解決方案是將數字的十六進制表示轉換爲十進制表示形式,然後使用該數字。

一旦您對每個像素的每種顏色都有一個表示法,您只需將矩陣重塑爲一個向量並使用標準的hist命令來繪製它。但正如所提到的,mayby可能有人更好地將顏色表示爲單個數字。

相關問題