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('匹配之后图像直方图');
数字图像处理(3)——直方图规定化
最新推荐文章于 2022-11-23 23:26:23 发布