2016-09-29 62 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