clc; clear all; close all; tic I=imread('corner.png'); % I=double(I); f=rgb2gray(I); figure,imshow(f),title('原图像'); figure,imhist(f),title('一维直方图'); [m,n,d]=size(f) A=f; for i=1:1:256 for j=1:1:256 twodim2(i,j)=0; end end J = imnoise(f,'gaussian',0.05); figure,imshow(J),title('加躁后的图像'); figure,imhist(J),title('一维直方图'); H=fspecial('average'); k=imfilter(J,H); % k=medfilt2(A) for j=1:1:n; for i=1:1:m; m1=J(i,j); m2=k(i,j); twodim2(m1+1,m2+1)= twodim2(m1+1,m2+1)+1; end end i=1:1:256; j=1:1:256; figure; mesh(i,j,twodim2(i,j)/(m*n)); title('二维直方图') toc rusult:
二维直方图:
eg2:
% 二维直方图,x轴是像素灰度值,y轴是邻域平均处理后的灰度值,z轴是统计的像素数目 clear clc % 读取图像并作邻域平均处理 I = im2uint8(imread('rice.png')); f = im2double(I); w = fspecial('average',3); I_avr = im2uint8(imfilter(f,w)); % 统计图中存在的灰度值 h_gray = imhist(I); h_avr = imhist(I_avr); ind_gray = find(h_gray>0)-1; ind_avr = find(h_avr>0)-1; % 预分配2D直方图数据矩阵 [X,Y] = meshgrid(ind_gray,ind_avr); [m,n] = size(X); % m是grayind_avr的长度,n是grayind的长度 data = zeros(m,n); % 遍历存在的灰度与均值,寻找对应像素并计数 for i=1:m for j=1:n gray = (X(i,j)==I); avr = (Y(i,j)==I_avr); data(i,j) = length(find(gray & avr)); end end % 绘图 surf(X,Y,data) xlabel('灰度值') ylabel('邻域均值') zlabel('像素数') view(90,90)
result: