数字图像处理(3)——直方图规定化

im      = imread('pic2.jpg');
im=rgb2gray(im); %将原始图像改为灰度图
imRef   = imread('pic1.png');
imRef=rgb2gray(imRef);  %将参考图像改为灰度图
hist    = imhist(im);                % 获取原始图像im的直方图
histRef = imhist(imRef);  %获取参考图像的直方图
cdf     = cumsum(hist) / numel(im);  % 计算累积分布函数,cumsum返回累加和,numel函数表示满足条件的元素个数
cdfRef  = cumsum(histRef) / numel(imRef);
 
% Compute the mapping
M   = zeros(1,256);
for idx = 1 : 256
    [tmp,ind] = min(abs(cdf(idx) - cdfRef)); %找到cdfRef中与cdf(idx)最接近的值,将值写入tmp中,将值所在的行号【也就是其像素值+1】写到ind中
    M(idx)    = ind-1; %将idx替换为ind-1
end
 
% Now apply the mapping to get first image to make
% the image look like the distribution of the second image
imMatch = M(double(im)+1);
 
figure;%显示原图像、匹配图像和匹配后的图像
subplot(1,3,1),imshow(im,[]);title('原图像');
subplot(1,3,2),imshow(imRef,[]);title('匹配图像');
subplot(1,3,3),imshow(imMatch,[]);title('匹配之后图像');
figure;%显示原图像、匹配图像和匹配后图像的直方图
subplot(3,1,1),imhist(im,64);title('原图像直方图');
subplot(3,1,2),imhist(imRef,64);title('匹配图像直方图');
subplot(3,1,3),imhist(uint8(imMatch),64);title('匹配之后图像直方图');







在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值