|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
至于图像直方图均匀化的概念我就不说了,下面直接给出直方图均匀化的代码吧。
" a% m6 @3 ^5 p3 o
9 H% Z! v8 d+ F2 K+ K1 w+ s- function om=myhisteq(im,a,b,flag)
- % HISTOGRAM EQUALIZATION
- %
- % input parameters
- % im (must): input grayscale image, can be double, uint8, uint16 and int16
- % a,b (must): piecewise linear stretching parameters, must be a vector between [0 1],
- % a and b must have the same length
- % flag (optional): 1 meaning plot the result, otherwise don't plot
- %
- % output parameters
- % om: output grayscale image after histogram equalization, have the same
- % class with im
- %
- % example
- % close all
- % im=imread('darklena.gif');
- % a=[0 0.1 0.2 1];
- % b=[0 0.8 0.95 1];
- % % 关于myhisteq的使用说明参见myhisteq.m内部
- % om=myhisteq(im,a,b,1);
- %
- % By LaterComer of MATLAB技术论坛
- % See also http://www.matlabsky.com
- % Contact me matlabsky@gmail.com
- % Modifid at 2010-10-13 12:52:53
- %
- % close all
- if nargin<3
- error('Input parameters must have im, a, b at least!');
- elseif nargin==3
- flag=0;
- elseif nargin>4
- error('Too many input parameters, the function have 4 arguments at most!');
- end
- if flag==1
- figure('name','Piecewise Linear Function')
- plot(a,b,'o-')
- xlabel('Input')
- ylabel('Output')
- title('Piecewise Linear Function')
- saveas(gcf,'_Piecewise Linear Function.jpg')
- end
- dataclass=class(im);
- switch dataclass
- case {'double','sigle'}
- if ~all(im<=1 & im>=0)
- error('If input image is double, then the data "im" must be between [0 1]!');
- end
- case {'uint8','uint16','int16'}
- vmin=double(intmin(dataclass));
- vmax=double(intmax(dataclass));
- a=a*(vmax-vmin)+vmin;
- b=b*(vmax-vmin)+vmin;
- end
- om=zeros(size(im),dataclass);
- for i=1:length(a)-1
- pix = im>=a(i) & im<a(i+1);
- om(pix)=(b(i+1)-b(i))/(a(i+1)-a(i))*(im(pix)-a(i))+b(i);
- end
- if flag==1
- figure('name','Input Image')
- imshow(im)
- title('Input Image')
- imwrite(im,'_Input Image.jpg');
- figure('name','Histogram of Input Image')
- imhist(im)
- title('Histogram of Input Image')
- saveas(gcf,'_Histogram of Input Image.jpg');
- figure('name','Oput Image')
- imshow(om)
- title('Oput Image')
- imwrite(om,'_Oput Image.jpg')
- figure('name','Histogram of Output Image')
- imhist(om)
- title('Histogram of Output Image')
- saveas(gcf,'_Histogram of Output Image.jpg')
- end- B! K" z/ w8 K+ ]0 H
; B$ q4 X- B4 h) E! V3 W
# z2 ^/ R) M9 V: _# Y, f; N |
|