matlab灰度直方图的2种绘制方法

来源:互联网 发布:杭州淘宝客服真实工资 编辑:程序博客网 时间:2024/05/18 03:19

matlab灰度直方图的绘制,掌握两种方法:
1

close all,clear all,clc;I=imread('filename.jpg');row=size(I,1);column=size(I,2);%2→dimisionN=zeros(1,256);%zeros(256)生成256x256矩阵for i=1:row    for j=1:column        k=I(i,j);        N(k+1)=N(k+1)+1;%记录每个灰度值的像素数    endendfigure;subplot(121);imshow(I);%subplot将窗口分为1x2两个窗口,现在在第一个小窗口绘图subplot(1,2,2);bar(N);%subplot(122)=subplot(1,2,2)%bar函数绘制直方图,为N中每一行的每一个元素绘制一个条axis tight;%设置坐标轴

绘制结果
这里写图片描述

2

A=imread(img);% get the histogram [Y,X]=size(A); % Y Row , X columngrayvalue=unique(A);imginfo=[];for i=1:length(grayvalue)        [ANSy,ANSx]=find(A==grayvalue(i));        imginfo.gray(i)=grayvalue(i);        imginfo.position{i}=[ANSy,ANSx];        imginfo.count(i)=length(ANSy)/(Y*X);endsubplot(1,2,1);imshow(A);title('Original Image');subplot(1,2,2);stem(imginfo.gray,imginfo.count,'Marker','none');%stem(x, y);绘制以x为横轴、 y为纵轴的脉冲杆图图形xlabel('Graylevel');ylabel('Proportion');axis([0 255 0 max(imginfo.count)]);title('Histogram of the orginial image')%find函数用来找出符合元素的位置,位置序号是从左到右,每列从上往下排列%以下为矩阵的一些基本操作:','为分隔列向量,‘;’分隔行向量,删除矩阵最后一列可以用x(:,end)=[]

这里写图片描述
绘制结果
这里写图片描述
imhist(A)结果
这里写图片描述