【问题标题】:Difficulty with my self built histogram in MatLab我在 MatLab 中自建直方图的困难
【发布时间】:2013-09-12 01:28:55
【问题描述】:

我正在尝试实现一个简单的函数,它与 MatLab 的默认 hist() 相同。

我们有两张不同亮度的相同图像,我们必须将它们转换为灰度,然后使用 MatLab hist() 的默认功能来获取直方图(到目前为止一切都很好!)。

然后我们必须实现函数 hist my_hist() ,当我试图计算强度的频率时,结果是不一样的。

似乎将254和255的频率总结为254和255为零!我不知道问题是什么,任何帮助将不胜感激。

这是命令行的代码:

%Read the images and convert them from rgb to grayscale
i=imread('pic1.jpg');
j=rgb2gray(i);
x=imread('pic2.jpg');
y=rgb2gray(x);

%Display the two images
figure
imshow(j)
figure 
imshow(y)

%Display the histogram of the two images
[a,b] = hist(j(:),0:1:255);
figure 
plot(b,a)
[c,d]=hist(y(:),0:1:255);
figure 
plot(d,c)

%Call of the built-in function
my_hist('pic1.jpg','pic2.jpg')

这是自建函数的代码:

function  []= my_hist( x,y)

%Read the images and convert them from rgb to grayscale
pic1=imread(x);
i=rgb2gray(pic1);
pic2=imread(y);
j=rgb2gray(pic2);

%Initialize two vectors to be the axis for histogram
plotx=0:255;
ploty=zeros(1,256);

%Take the dimensions of the first image pic1
[m,n] = size(i);

%With 2 loops we go through the matrix of the image and count how many
%pixels have the same intensity

for k=1:m 
   for l=1:n
num=i(k,l)+1;
      ploty(num)=ploty(num)+1;
   end
 end

%Display the histogram for the first image pic1
figure
plot(plotx,ploty);

%Initialize two vectors to be the axis for histogram
plotx2=0:255;
ploty2=zeros(1,256);

%Take the dimensions of the second image pic2
[m2,n2] = size(j);

%With 2 loops we go through the matrix of the image and count how many
%pixels have the same intensity
for o=1:m2 
 for p=1:n2
num2=j(o,p)+1;
    ploty2(num2)=ploty2(num2)+1;
 end
end

%Display the histogram for the second image pic2
figure
plot(plotx2,ploty2);
end

这里是图片pic1pic2

【问题讨论】:

  • 我稍微更新了术语。在 Matlab 中,内置函数实际上是 Mathworks(而不是您自己)内置到软件中的函数。

标签: matlab histogram


【解决方案1】:

这是一个问题,因为您的图像是整数类型 uint8,其范围只能是 0-255:

>> a= uint8(255)

a =

  255

>> a=a+1

a =

  255

将您的数据转换为输入uint16 with

j = uint16(j);
y = uint16(y);

你的问题应该消失了:

>> a=uint16(a)

a =

    255

>> a=a+1

a =

    256

【讨论】:

  • 我希望范围为 0-255,但问题已得到解决。我刚刚找到了最小和最大强度并根据它们创建了两个向量,并且直方图创建得很好。 ..但是无论如何都感谢努力尝试
猜你喜欢
  • 2014-08-31
  • 1970-01-01
  • 2013-09-04
  • 2013-02-06
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
相关资源
最近更新 更多