【问题标题】:histogram for black and white image (MatLab)黑白图像的直方图(MatLab)
【发布时间】:2016-09-29 16:15:39
【问题描述】:

我正在学习图像处理课程,我们需要使用 matlab。作为一项任务,我们需要创建一个计算灰度图像直方图的函数。我的代码能够处理 256 bin,但是当我尝试 128 bin 时遇到此错误:

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

我知道这是一个越界错误,但我不知道如何解决这个问题。 谢谢

【问题讨论】:

    标签: matlab image-processing


    【解决方案1】:

    它不起作用有两个原因:

    1. 你应该ceil你的阈值来获得一个整数

      threshold = ceil(128/n);
      
    2. i 中的值是整数,这意味着在执行floor 操作之前,除法将自行舍入。因此,您需要将其转换为double

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-18
      • 2017-07-21
      • 2011-01-28
      • 2018-06-10
      • 1970-01-01
      • 2015-09-16
      • 2012-10-15
      • 1970-01-01
      相关资源
      最近更新 更多