2016-09-29 563 views
0

我正在拍攝一個圖像處理類,我們需要使用matlab。作爲一項任務,我們需要創建一個計算灰度圖像直方圖的函數。我的代碼是能處理256箱,但是當我嘗試了128箱就遇到這樣的錯誤:黑白圖像直方圖(MatLab)

Error using accumarray, First input SUBS and third input SZ must satisfy ALL(MAX(SUBS)<=SZ). 

我的代碼如下:

function hist = imgrayhist(imggray,n) 
%read the image 
i = imread(imggray); 

%calculate bin threshold 
threshold = 256/n; 

%Calculate which bin each pixel belongs to 
im_level = floor(i(:)/threshold); 

%tranform the matrix to a vector 
j = im_level(:); 

hist = accumarray(j+1,1,[n 1]); 
end 

我知道這是一個出界錯誤,但我不知道如何解決這個問題。 謝謝

回答

0

有2個原因,這是行不通的:

  1. 你應該ceil你的門檻在i一個整數

    threshold = ceil(128/n); 
    
  2. 的值是整數,其中意味着在執行floor操作之前,該部門將進行四捨五入。因此,你需要將其轉換爲double

    im_level = floor(double(i(:))/threshold); 
    
+0

非常感謝你。這解決了我的問題 – Kai