绘制直方图
1.出错代码(没有意识到图片是彩色的要转换为灰度二维)
clc;close all;clear all;
image=imread('ufo.jpg');
img=imhist(image,25);
horz=linspace(0,255,25);
bar(horz,img)
2.正确代码
clc;close all;clear all;
image=imread('ufo.jpg');
image=rgb2gray(image); %灰度
img=imhist(image,25); %25是直方图容器数目
horz=linspace(0,255,25); %行向量
bar(horz,img)
或者直接imhist(image)图就出来了。
直方图均衡
使用matlab工具箱histeq,g=histeq(f,nlev),nlev为灰度级数。
clc;close all;clear all;
image=imread('ufo.jpg');
image=rgb2gray(image);
g=histeq(image,256);
subplot(2,2,1)
imshow(image);
title('原图');
subplot(2,2,2)
imhist(image);
title('直方图');
subplot(2,2,3);
imshow(g);
title('均衡后');
subplot(2,2,4)
imhist(g);
title('直方图均衡');
均衡实质是归一化直方图累加求和,还可以用
hnorm=imhist(image)./numel(image); cdf=cumsum(hnorm); x=linspace(0,1,256); plot(x,cdf)
