matlab两张彩色图片直方图匹配代码
时间: 2024-08-14 22:01:07 浏览: 42
在MATLAB中,直方图匹配是一种图像处理技术,用于将一张图像的灰度分布映射到另一张图像上,使得两幅图像在对比度上有更好的一致性。以下是基本的代码示例,假设我们有两张彩色图像`img1`和`img2`:
```matlab
% 加载彩色图像
img1 = imread('image1.jpg'); % 替换为实际文件名
img2 = imread('image2.jpg'); % 替换为实际文件名
% 提取每个颜色通道(红、绿、蓝)
red1 = img1(:,:,1);
green1 = img1(:,:,2);
blue1 = img1(:,:,3);
red2 = img2(:,:,1);
green2 = img2(:,:,2);
blue2 = img2(:,:,3);
% 分别对每个通道做独立的直方图匹配
hist1_red = imhist(red1);
hist2_red = imhist(red2);
[match_index_red, ~] = histeq(red2(:), hist1_red); % 使用imhisteq函数进行匹配
hist1_green = imhist(green1);
hist2_green = imhist(green2);
[match_index_green, ~] = histeq(green2(:), hist1_green);
hist1_blue = imhist(blue1);
hist2_blue = imhist(blue2);
[match_index_blue, ~] = histeq(blue2(:), hist1_blue);
% 应用匹配到原图像
matched_red1 = uint8(match_index_red);
matched_green1 = uint8(match_index_green);
matched_blue1 = uint8(match_index_blue);
% 组合成新的彩色图像
matched_img1 = cat(3, matched_red1, matched_green1, matched_blue1);
% 显示原始和匹配后的图像
subplot(1, 2, 1), imshow(img1);
title('Original Image');
subplot(1, 2, 2), imshow(matched_img1);
title('Histogram Matched Image');
```
阅读全文