MATLAB图像拼接第二弹——有噪声的新闻图拼接

  • 时间:
  • 浏览:
  • 来源:互联网

0 子图

首先导入数据并显示

clear all
%% 读取图片数据并显示
X = {};
for i = 1:18
    X{i} = imread(['G:\图像处理\matlab\拼接图像\附件1-文字图片\00',num2str(i),'.bmp']);
end
for i=1:18
    subplot(1,18,i)
    imshow(X{i})
end

在这里插入图片描述

1 去除椒盐噪声

%% 三种去除椒盐噪声的方法
%参考https://blog.csdn.net/chaolei3/article/details/79769703,中值滤波代码
fprintf('=======中值滤波=====\n');
img = X{1};
[~,~,ch]  = size(img);
if ch ==3
    img =rgb2gray(img);   %由RGB图变为灰度图
end
subplot(1,4,1);imshow(img);title('原始图像');
img_med = medfilt2(img, [2,2]);
subplot(1,4,2);imshow(img_med);title('2*2中值滤波');

psnr_med = eval_psnr(img,img_med);
% 使用[3,3]的窗口
img_med2 = medfilt2(img, [3,3]);
subplot(1,4,3);imshow(img_med2);title('3*3中值滤波');
psnr_med2 = eval_psnr(img,img_med2);

fprintf('=======频域滤波=====\n');
[m,n,ch]  = size(img);
if ch == 3
    img = rgb2gray(img);
end
%DCT变换
img_dct = dct2(img); 
I = zeros(m,n);
% 高频屏蔽
I(1:m/3,1:n/3)=1; 
Ydct = img_dct .* I;
%逆DCT变换
img_dct = uint8(idct2(Ydct)); 
%结果输出
subplot(1,4,4);imshow(img_dct);title('去噪之后');
psnr_dct = eval_psnr(img,img_dct);

%由子图看出,3*3中值滤波在三个滤波器中效果最好

在这里插入图片描述

%% 3*3中值滤波
X_med2 = {};
for i = 1:18
    X_med2{i} = medfilt2(rgb2gray(X{i}), [3,3]);
end
figure
for i=1:18
    subplot(1,18,i)
    imshow(X_med2{i})
end

在这里插入图片描述

2 灰度图转化为二值图

%% 灰度图转化为二值图
%https://blog.csdn.net/hhhhhyyyyy8/article/details/77866129
% 示意图
f=X_med2{1};
subplot(1,3,1);
imshow(f);
title('原图');
bw=im2bw(f);
subplot(1,3,2);
imshow(bw);
title('二值图像');

level=graythresh(f);
bw2=im2bw(f,level);
subplot(1,3,3);
imshow(bw2);
title('通过graythresh计算门槛值后得到二值图像');

X_01 = {};
for i = 1:18
    X_01{i} = im2bw(X_med2{i});
end```
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210320213849167.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mjc0OTk0NA==,size_16,color_FFFFFF,t_70)
# 4 拼接

```python
%% 拼接算法(最简单的判断边缘距离的算法)
%score保存边缘之间的差值平方和
N=18;
%N=12;
score = zeros(N);
for i = 1:N
    img1 =  X_01{i};
    for j = 1:N
        if i~=j
            img2 = X_01{j};
            score(i,j) = sum((img1(:,end)-img2(:,1)).^2);
        end
    end
end
figure
contourf(score)

%%  开始拼接,有左拼,右拼两种方式
from_bus = 1;
X_show = X_med2{from_bus};
bus=[from_bus];
for i = 1:N
    from_bus = bus(end);
    s = score(:,from_bus);
    s(from_bus) = [];
    [~,to_bus] = min(s);

    if to_bus >= from_bus
        to_bus = to_bus +1;
    end
    [~,idx] = sort(s);
    j=1;
    imshow([X_med2{to_bus},X_show])
    label = input('正常1,异常0,退出-1:'); 
    if label==-1
        break
    end
    while ismember(to_bus,bus) || ~label
        
        to_bus = idx(j+1);
        if to_bus > from_bus
            to_bus = to_bus +1;
        end
        imshow([X_med2{to_bus},X_show])
        j=j+1;
        label = input('正常1,异常0:');
        if label==-1
            break
        end
    end    
    if label==-1
       break
    end
    imshow([X_med2{to_bus},X_show])
    X_show = [X_med2{to_bus},X_show];
    bus = [bus,to_bus];
    from_bus = to_bus;
end



%%  右接
from_bus = 1;
for i = 1:N-length(bus)
    from_bus = bus(1);
    s = score(from_bus,:)';
    s(from_bus) = [];
    [~,to_bus] = min(s);

    if to_bus >= from_bus
        to_bus = to_bus +1;
    end
    [~,idx] = sort(s);
    j=1;
    imshow([X_show,X_med2{to_bus}])
    label = input('正常1,异常0,退出-1:'); 
    if label==-1
        break
    end
    while ismember(to_bus,bus) || ~label
        
        to_bus = idx(j+1);
        if to_bus > from_bus
            to_bus = to_bus +1;
        end
        imshow([X_show,X_med2{to_bus}])
        j=j+1;
        label = input('正常1,异常0:');
        if label==-1
            break
        end
    end    
    if label==-1
       break
    end
    imshow([X_show,X_med2{to_bus}])
    X_show = [X_show,X_med2{to_bus}];
    bus = [to_bus,bus];
    from_bus = to_bus;
end

%% 结果
imshow(medfilt2(X_show, [3,3]));
%最终结果保存在bus中,在GUI中直接用了bus

和第一弹一样的的拼接算法~
最终结果:
在这里插入图片描述

本文链接http://www.dzjqx.cn/news/show-617002.html