我想要一段matlab的代码,把两张有重叠的模糊照片变得清晰一点,可以吗?
时间: 2023-05-26 19:06:06 浏览: 179
把模糊照片变清晰
4星 · 用户满意度95%
把两张有重叠的照片拼接起来,可以通过图像融合方法实现。一种简单的方法是使用拉普拉斯金字塔图像融合算法。
下面是一个用Matlab实现的示例代码:
```matlab
% 读入两张图像
img1 = imread('img1.png');
img2 = imread('img2.png');
% 对图像进行高斯模糊
sigma = 2;
gaussfilt = fspecial('gaussian', round(6*sigma+1), sigma);
img1 = imfilter(img1, gaussfilt);
img2 = imfilter(img2, gaussfilt);
% 将两张图像拼接成一张
[h,w,~] = size(img1);
half_w = floor(w/2);
overlap_width = 20;
img12 = zeros(h,w+half_w-overlap_width,3,'uint8');
img12(:,1:half_w,:) = img1(:,1:half_w,:);
img12(:,half_w-overlap_width:end,:) = img2(:,half_w-overlap_width+1:end,:);
% 拉普拉斯金字塔
levels = 5;
img1_pyramid = img_pyramid(img1, levels);
img12_pyramid = img_pyramid(img12, levels);
% 逐层进行图像融合
img_blend_pyramid = cell(1,levels);
for i = 1:levels
img_blend_pyramid{i} = laplacian_blend(img1_pyramid{i}, img12_pyramid{i});
end
% 级联重建融合后的图像
img_blend = img_blend_pyramid{levels};
for i = levels-1:-1:1
img_blend = impyramid(img_blend,'expand');
img_blend = imadd(img_blend, img_blend_pyramid{i});
end
% 显示结果
imshow(img_blend);
% 下面是一些帮助函数
function pyramid = img_pyramid(img, levels)
pyramid = cell(1, levels+1);
pyramid{1} = img;
for i = 2:levels+1
img_down = impyramid(pyramid{i-1},'reduce');
pyramid{i} = img_down;
end
end
function img_blend = laplacian_blend(img1, img2)
[h, w, ~] = size(img1);
mask = ones(h,w,3,'uint8');
mask(:,1:round(w/2),:) = 0;
img_blend = uint8(zeros(h,w,3));
for i = 1:3
img1_lap = laplacian_pyramid(img1(:,:,i), 5);
img2_lap = laplacian_pyramid(img2(:,:,i), 5);
mask_pyramid = img_pyramid(mask(:,:,i), 5);
img_lap_blend = cell(1,6);
for j = 1:6
img_lap_blend{j} = mask_pyramid{j}.*img1_lap{j} + (1-mask_pyramid{j}).*img2_lap{j};
end
img_blend(:,:,i) = laplacian_reconstruct(img_lap_blend);
end
end
function pyramid = laplacian_pyramid(img, levels)
pyramid = cell(1, levels+1);
pyramid{1} = img;
for i = 1:levels
img_down = impyramid(pyramid{i},'reduce');
img_up = impyramid(img_down,'expand');
pyramid{i+1} = pyramid{i} - img_up(1:size(pyramid{i},1),1:size(pyramid{i},2));
end
end
function img_reconstruct = laplacian_reconstruct(pyramid)
img_reconstruct = pyramid{end};
for i = length(pyramid)-1:-1:1
img_reconstruct = impyramid(img_reconstruct,'expand');
img_up = impyramid(img_reconstruct,'expand');
img_reconstruct = pyramid{i} + img_up(1:size(pyramid{i},1),1:size(pyramid{i},2));
end
end
```
阅读全文