简体   繁体   English

嗨,谁能告诉我代码如何在没有 matlab 工具箱(imhist,hist)的情况下创建直方图?

[英]Hi, can anyone tell me code how to create histogram without the toolbox (imhist,hist) from matlab?

can anyone tell me how to create histogram from an grayscale image without the function from the matlab.谁能告诉我如何在没有 matlab 函数的情况下从灰度图像创建直方图。

Thanks谢谢

A bit more efficient than your code:比您的代码更有效:

histogram = zeros(1,256);
for value = 1:256 % loop through possible values
    histogram(value) = histogram(value) + length(find(F(:)==value));
end

Note that this code makes a histogram for values from 1 to 256. For grayscale images you might need one from 0 to 255. But I'll let you change this on your own for matters of practice.请注意,此代码为从 1 到 256 的值制作了一个直方图。对于灰度图像,您可能需要一个从 0 到 255 的直方图。但为了练习,我会让您自己更改它。

edit : Since you asked for a revision of your code, let me comment on it:编辑:既然你要求修改你的代码,让我评论一下:

 [m n]=size(F);
 x=0;

No need to set x to zero, you are overwriting it later.无需将x设置为零,稍后您将覆盖它。

 H=0;

Instead of setting H to zero you should initialize it as an array, since you want to fill H(x) later.您应该将H初始化为一个数组,而不是将H设置为零,因为您想稍后填充H(x) Something like H = zeros(1,256);H = zeros(1,256);

for z=1:256

I'm not sure what this loop over z is supposed to do.我不确定 z 上的这个循环应该做什么。 You never use z .你从不使用z You can remove it.你可以删除它。

for i=1:m
    for j=1:n       
        x==F(i,j);

As I said below, this should be x=F(i,j);正如我在下面所说的,这应该是x=F(i,j); instead, as == is a test for equality.相反,因为==是对相等性的测试。

        H(x+1)=H(x+1)+1

This works if all values in F are guaranteed to be between 0 and 255. Though I would say it's not very good style.如果F中的所有值都保证在 0 到 255 之间,这将起作用。尽管我会说这不是很好的风格。

Also, put a semicolon at the end of this line to suppress the outputs, otherwise your command line gets flooded (which is also slowing down things a lot).另外,在这一行的末尾放一个分号来抑制输出,否则你的命令行会被淹没(这也会大大减慢速度)。

    end
end
H;

The last line does nothing.最后一行什么都不做。 Why is it there?为什么会在那里?

end

The outer loop over z is not needed (see above).不需要 z 上的外循环(见上文)。

So, here is a "sanitized" version of your code:因此,这是您的代码的“消毒”版本:

[m,n] = size(F);
H = zeros(1,256);
for i=1:m
    for j=1:n
        x=F(i,j);
        H(x+1)=H(x+1)+1;
    end
end

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM