EDA365电子论坛网
标题:
在MATLAB环境下图像直方图均匀化代码实现
[打印本页]
作者:
pulbieup
时间:
2020-3-3 15:29
标题:
在MATLAB环境下图像直方图均匀化代码实现
至于图像直方图均匀化的概念我就不说了,
下面直接给出直方图均匀化的代码吧。
. t6 |4 R- j" F) H" Z
8 p+ q& o' C+ {7 r6 v/ W( ?1 y$ F
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
" q! x* I7 W$ z2 I5 g
4 A+ ?1 ?& y2 h7 y4 h i
- o. M. x2 o1 b& I
作者:
CCxiaom
时间:
2020-3-3 16:35
在MATLAB环境下图像直方图均匀化代码实现
欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/)
Powered by Discuz! X3.2