二维直方图用 matlab 怎么求?

要求输出一幅图像的二维直方图,怎么用matlab画出来呢?
2024-12-03 09:47:39
推荐回答(1个)
回答(1):

img=imread('westconcordorthophoto.png');
[m n]=size(img);
%snoise=0.5*randn(size(img));
%img=imadd(img,im2uint8(snoise));
%figure(2)
%imshow(img)
r=1; %邻域半径

imgn=zeros(m+2*r+1,n+2*r+1);
imgn(r+1:m+r,r+1:n+r)=img;

imgn(1:r,r+1:n+r)=img(1:r,1:n); %扩展上边界
imgn(1:m+r,n+r+1:n+2*r+1)=imgn(1:m+r,n:n+r); %扩展右边界
imgn(m+r+1:m+2*r+1,r+1:n+2*r+1)=imgn(m:m+r,r+1:n+2*r+1); %扩展下边界
imgn(1:m+2*r+1,1:r)=imgn(1:m+2*r+1,r+1:2*r); %扩展左边界

Hist=zeros(256,256);
for i=1+r:r+m
for j=1+r:r+n
pix1=uint8(imgn(i,j));
pix2=uint8(mean2(imgn(i-r:i+r,j-r:j+r)));
Hist(pix1+1,pix2+1)=Hist(pix1+1,pix2+1)+1;
end
end
mesh(double(Hist))