在MatLab中使用自建直方图的困难

在MatLab中使用自建直方图的困难,matlab,histogram,Matlab,Histogram,我试图通过MatLab实现一个与默认hist()相同的简单函数 我们有两个亮度不同的相同图像,我们必须将它们转换为灰度,然后使用MatLab的默认函数hist()来获得直方图(到目前为止还不错!) 然后我们必须实现函数histmy_hist(),当我试图计算强度的频率时,结果是不一样的 似乎它将254&255的频率加起来是254,255是零!我不知道是什么问题,任何帮助都将不胜感激 以下是命令行的代码: %Read the images and convert them from rgb to

我试图通过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

这是图像和。

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

>> a= uint8(255)

a =

  255

>> a=a+1

a =

  255
使用将数据转换为type
uint16

j = uint16(j);
y = uint16(y);
你的问题应该消失了:

>> a=uint16(a)

a =

    255

>> a=a+1

a =

    256

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

>> a= uint8(255)

a =

  255

>> a=a+1

a =

  255
使用将数据转换为type
uint16

j = uint16(j);
y = uint16(y);
你的问题应该消失了:

>> a=uint16(a)

a =

    255

>> a=a+1

a =

    256

我更新了一点术语。在Matlab中,内置函数实际上是Mathworks(而不是您自己)在软件中内置的函数。我对术语做了一些更新。在Matlab中,内置函数实际上是Mathworks(而不是您自己)在软件中内置的函数。我希望范围为0-255,但问题已经解决。。我刚刚找到了最小和最大强度,并根据它们创建了两个向量,直方图创建得很好…但无论如何,请努力尝试,我希望范围为0-255,但问题已经解决。。我刚刚找到了最小和最大强度,并根据它们创建了两个向量,柱状图创建得很好…但无论如何都要感谢,请努力尝试