MATLAB图像处理学习笔记之一 第三章 图像的点运算
3.1灰度直方图
其横轴范围是(0-255)表示的是灰度值,0表示黑,255表示白。纵轴表示的是各个灰度级别(可以是一定的灰度范围内)的像素点的个数。
代码如下:
% My first matlab peogram % 3.1.2.1一般直方图 %I = imread('C:\Users\Authur\Desktop\OpenCV\image\hui1_2.jpg'); %读取图像 I = imread('hui1.jpg'); %读取图像 figure(7); %打开一个新窗口 imshow(I); title('Sourse');%显示原图 figure; % imhist(I); title('Graph');%显示直方图
figure('Name','Simulation Plot Window 0-255 64等分','NumberTitle','off'); imhist(I,64);
解说:
figure('Name','Simulation Plot Window 0-255 64等分','NumberTitle','off');这是figure的典型用法。
figure(7);
用于生成一个新的窗口,并且给窗口命名为7。
主要的实现函数就是imhist();
imhist()解说:
help中的解释为
imhist(I) displays a histogram for the image I above a grayscale colorbar. The number of bins in the histogram is specified by the image type. If I is a grayscale image, imhist uses a default value of 256 bins. If I is a binary image, imhist uses two bins.
翻译过来就是说 imhist()显示的是图像I(PS:I 代表图像的名称,若图像不在该脚本目录下,则需要指定路径。路径不需要两个斜杠!区别于OpenCV)在以灰度等级色条为纵轴的直方图,直方图中bin的数目室友图像的类型决定的,对与灰度级图像,imhist()中默认的bin值是255,对于二值图像,bin值为2。
对于bin的理解,笔者认为这是横轴的划分段数。直方图是离散的,bin值决定了在0-255中划分为几个取样点,对与灰度图像,取255个点(0-255),二值图像取2个(0和1)
imhist()只能够输入黑白图像(二值图像)或者灰度级图像。这里需要注意的是,使用画图生成的图像都是 ColorType: 'truecolor' ,所以输入报错。
读取图像信息的函数 >> a = imfinfo('W.jpg')
【单通道的图像怎么生成,有待进一步学习】
imhist(I, n) displays a histogram where n specifies the number of bins used in the histogram. n also specifies the length of the colorbar. If I is a binary image, n can only have the value 2.
指定bin=n;
imhist(X, map) displays a histogram for the indexed image X. This histogram shows the distribution of pixel values above a colorbar of the colormap map. The colormap must be at least as long as the largest index in X. The histogram has one bin for each entry in the colormap.
索引图像,格式gif、pcx、tif……
%显示索引图像 [X,MAP] = imread('W.gif'); figure('Name','显示索引图像'); imshow(X,MAP); colorbar;X为图像数据矩阵,MAP为调色板
[X, map] = imread(...) reads the indexedimage in filename into X and its associated colormap into map. Colormap valuesin the image file are automatically rescaled into the range [0,1].
归一化直方图
横轴表示的是0-255的灰度值,纵坐标表示的是每个灰度值范围内的像素点出现的概率。即 (某灰度区间内的像素点 / 所有点)
I = imread('C:\Users\Authur\Desktop\OpenCV\image\hui1_2.jpg'); figure('Name','归一化直方图','NumberTitle','off'); [M,N] = size(I); [counts,x] = imhist(I,32); counts = counts/M/N; stem(x,counts);
也就是说,imread()得到的图像文件名存到X中(PS:笔者的理解是将文件名或者路径输入到X中,类似指针指向文件),将调色板放到map中。colormap是取回当前的颜色映射表,返回值必须在0~1之间
【小结】
图像的分类和显示http://wenku.baidu.com/link?url=4f6YBgJ6QFH-5WrHSiUnnMKjmvuYXtdcfcG6OVn6LdEyTJWgC9E1Mhkzp0WxXKwwENkmZQLAG0lELLq6DNTktKThS6YCsn46kw_biWt49ZC