如下图所示,共有120个子图,如何用MATLAB拼接起来?

1 读取数据

首先读取所有图像数据,存在X中,并用imshow显示。

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

如图所示,这是需要拼接的120个子图

2 预处理

%% 转化为二值图
X_01 = {};
for i = 1:120X_01{i} = im2bw(rgb2gray(X{i}));
end
%示意
subplot(131);imshow(X{1})
subplot(132);imshow(rgb2gray(X{1}))
subplot(133);imshow(im2bw(rgb2gray(X{i})))

原图、rgb2gray变成的灰度图、以及二值图对比:

3 计算子图之间的相关性

只考虑边缘与边缘之间的关系。建立一个120x120的矩阵,里面分别存放着子图与子图之间的相关性。
如:如果图A左侧和图B右侧的二值接近,那么图A就在图B右边。

%%  计算分数
%score中保存着四种分数,分别对应两个图块的四种接法
score = zeros(120,120,4);
s=zeros(120,120,4);
for k = 1:4for i = 1:120        img1 = X_01{i};for j = 1:120if i~=jimg2 = X_01{j};%s(i,j,k) = img12img2(img1,img2,k);if k==1score(i,j,k) = sum((img1(:,1)-img2(:,end)).^2);elseif k==2score(i,j,k) = sum((img1(1,:)-img2(end,:)).^2);elseif k==3score(i,j,k) = sum((img1(:,end)-img2(:,1)).^2);elsescore(i,j,k) = sum((img1(end,:)-img2(1,:)).^2);endendendend
end
score1 = score(:,:,1);
score2 = score(:,:,2);
score3 = score(:,:,3);
score4 = score(:,:,4);
%注意:score1=score3'

4 列子图拼接

下一步开始拼接,这一步的拼接过程是数字判断+人工干预。
随机选择一个初始子图,然后往下拼接。

%% 拼接,首先把纵向的接好
%随机选择一个图块to,找上下图分别是?
bus_result = [];
%% 往下拼接
% eg to=99,找子图99的上下子图
to = 5;
from_bus = to;
X_show = [];
bus = from_bus;
X_show = X{from_bus};
for i = 1:12from_bus = bus(end);s = score2(:,from_bus);s(from_bus) = [];[~,to_bus] = min(s);if to_bus >= from_busto_bus = to_bus +1;end[~,idx] = sort(s);j=1;imshow([X_show;X{to_bus}])label = input('正常1,异常0,退出-1:'); if label==-1breakendwhile ismember(to_bus,bus) || ~labelto_bus = idx(j+1);if to_bus > from_busto_bus = to_bus +1;endimshow([X_show;X{to_bus}])j=j+1;label = input('正常1,异常0:');if label==-1returnendend    if label==-1returnendimshow([X_show;X{to_bus}])X_show = [X_show;X{to_bus}];bus = [bus;to_bus];from_bus = to_bus;
end
imshow(X_show)
%如果选中的to在中间,需要上下子图都找,99子图在最上面,所以只找下图就可


上图拼接正常,因此输入1.



拼接异常,输入0.

拼接正常,输入1
当向下拼接结束后,再向上拼接。

%% 往上拼接
from_bus = to;
for i = 1:12from_bus = bus(1);s = score4(:,from_bus);s(from_bus) = [];[~,to_bus] = min(s);if to_bus >= from_busto_bus = to_bus +1;end[~,idx] = sort(s);j=1;imshow([X{to_bus};X_show])label = input('正常1,异常0,退出-1:'); if label==-1breakendwhile ismember(to_bus,bus) || ~labelto_bus = idx(j+1);if to_bus > from_busto_bus = to_bus +1;endimshow([X{to_bus};X_show])j=j+1;label = input('正常1,异常0:');if label==-1returnendend    if label==-1returnendimshow([X{to_bus};X_show])X_show = [X{to_bus};X_show];bus = [to_bus;bus];from_bus = to_bus;
end
imshow(X_show)bus_result = [bus_result,bus];

上下都拼接结束后,结果如下:

重复,把十二个列图拼接完成。

下面是子图代码。

bus_result =[99    35     6    91    22    40    92    32    34   119    35    55;3   105    30    11    16    68   115    96    69   101    54    45;77    51    33    62    74   113    72    60    78    70   107     7;86    28    17    81   100    38    76   103    58    89    42    41;120   104    93    46    98   111    14    47    80    56    63   117;8     5    48    59    90    84    83    67    97   106    79   108;53    49    27    37    25    21   109    29    52    64    31   110;61    44    15    94    87    50    43    88    26    19   102    73;1     4    18    65    82   116    23    36    71     2   114    85;75    10    12    66    39    95     9    24    13    20    57   118;]

5 行子图拼接

% 把列子图二值化X_col = {};
for j = 1:12Z = [];for i = 1:10Z = [Z;X{bus_result(i,j)}];endX_col{j} = Z;
end
X_med2 = X_col;
X_01 = {};
for i = 1:12X_01{i} = im2bw(X_med2{i});
end


%% 拼接算法(最简单的判断边缘距离的算法)
%score保存边缘之间的差值平方和N=12;
score = zeros(N);
for i = 1:Nimg1 =  X_01{i};for j = 1:Nif i~=jimg2 = X_01{j};score(i,j) = sum((img1(:,end)-img2(:,1)).^2);endend
endfrom_bus = 1;X_show = X_med2{from_bus};
bus=[from_bus];
for i = 1:12s = score(from_bus,:)';s(from_bus) = [];[~,to_bus] = min(s);if to_bus >= from_busto_bus = to_bus +1;end[~,idx] = sort(s);j=1;imshow([X_show,X_med2{to_bus}])label = input('正常1,异常0,退出-1:'); if label==-1breakendwhile ismember(to_bus,bus) || ~labelto_bus = idx(j+1);if to_bus > from_busto_bus = to_bus +1;endimshow([X_med2{to_bus},X_show])j=j+1;label = input('正常1,异常0:');if label==-1breakendend    if label==-1breakendimshow([X_show,X_med2{to_bus}])X_show = [X_show,X_med2{to_bus}];bus = [bus,to_bus];from_bus = to_bus;
end
% 没有用到左接,因为选择的是左边缘

最终结果:

一段没有用到的右连接代码,如果选择的初始子图是中间的列子图,需要这一段。

%%
bus_ = zeros(10,12);
for i = 1:12bus_(:,i)=bus_result(:,bus(i));
end
%最终在GUI中使用的是bus_矩阵中的数据%%  左接
% from_bus = 1;
% for i = 1:N-length(bus)
%     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 = [to_bus,bus];
%     from_bus = to_bus;
% end

MATLAB图像拼接——怎么用MATLAB做拼图?相关推荐

  1. 基于SIFT特征的图像拼接融合(matlab+vlfeat实现)

    基于SIFT特征的图像拼接融合(matlab+vlfeat实现) piccolo,之前做的东西,简单整理下,不是做图像方向的,写的不好轻喷 主要原理参看SIFT算法详解和SIFT特征匹配算法介绍--寻 ...

  2. matlab的combuilder系列-matlab下做com组件 zzfrom SMTH bbs

    matlab的combuilder系列-matlab下做com组件 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com ...

  3. 【Matlab图像拼接】块匹配全景图像拼接【含源码 742期】

    一.代码运行视频(哔哩哔哩) [Matlab图像拼接]块匹配全景图像拼接[含源码 742期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] 蔡利梅.MATLA ...

  4. matlab图像拼接融合(四种方法)

    matlab图像拼接的四种方法  1.直接拼接,  2.亮度调整后拼接,  3.按距离比例融合,  4.亮度调整后按距离比例融合 流程: 1.读入左,右图,并取出重合部分,并转化为亮度图 2.分别把每 ...

  5. matlab 图像拼接算法,MATLAB图像拼接算法及实现

    MATLAB图像拼接算法及实现 图像拼接算法及实现(一)论文关键词:图像拼接 图像配准 图像融合 全景图论文摘要:图像拼接(image mosaic)技术是将一组相互间重叠部分的图像序列进行空间匹配对 ...

  6. 用matlab做音乐仿真,Matlab课程设计报告--MATLAB GUI的音乐键盘仿真

    Matlab课程设计报告--MATLAB GUI的音乐键盘仿真 1 MATLAB MATLAB 实践实践 课程设计课程设计 目目 录录 1.1.设计目的设计目的3 3 2.2.题目分析题目分析3 3 ...

  7. matlab留学生作业代做,代做DFT留学生作业、代写Matlab实验作业、Matlab程序语言作业调试、FFT课程作业代做代做数据库SQL|调试Matlab程序...

    Introduction This lab is a revision of the Discrete Fourier Transform (DFT), and the Fast Fourier Tr ...

  8. 求助!matlab软件下SVM(支持向量机)做回归预测,软件版本为2016b看,老是报错看,不知道怎么解决这个问题。

    有大神帮忙看看吗? 网上找的代码,在我的电脑下运行,总是出错,出错位置如下: >> [py,mse] = svmpredict(Y_test,X_test,model); Mean squ ...

  9. matlab经典编程题,matlab

    时间:2019-05-12 12:56:56 作者:admin MATLAB 编程题总结LY 1.输出x,y两个中值较大的一个值. x=input('x='); y=input('y='); if x ...

最新文章

  1. linux加微软的数据库,在Linux上使用Microsoft SQL – 安装SQL
  2. 软件测试女孩学适合吗
  3. 聚美优品 html 资源,跨界整合行业资源 聚美优品向阳而生
  4. php链接Access数据库代码,PHP连接Access数据库代码
  5. 第四章、项目整合管理【PMP】
  6. Win 7英文系统显示中文乱码的解决
  7. java p8级别_JAVA程序员月入5000+很迷茫,如何能在一年内改变达到月入过万?
  8. 如何学习前端知识?优秀的前端开发工程师应该具备什么条件?
  9. 让php来搞定一切!,ubuntu安装和配置php5
  10. Android应用程序插件化研究之DexClassLoader
  11. 7. 从数据库获取数据- 从零开始学Laravel
  12. linux_network
  13. Eclipse、STS 常用设置、操作 与 常用快捷键
  14. rbac yii 1
  15. 无限容量还不限速的网盘,了解一下~
  16. 鸿蒙生死印是谁的,逆天邪神:鸿蒙印的器灵还存在,或许云澈将知道些关于远古的秘密...
  17. [配置]keepalived配置高可用虚拟IP不通
  18. 新股网上发行申购程序
  19. mysql首字母大写函数_string - 首字母大写。MySQL的
  20. Flutter安卓系统把状态栏设置为透明色

热门文章

  1. webpack5 基础配置(4下)模块化原理 初识source map
  2. MongoDB 数据库(一):MongoDB的介绍与安装
  3. ESTIMATE 包 error/报错 无法匹配基因
  4. 三相变频电源相位角的概念
  5. C# FIR滤波器(含低通、高通、带通、带阻)
  6. error LNK2001: 无法解析的外部符号 解决方法
  7. springBoot打包瘦身
  8. 2021年气象为何反常?未来会是神马?
  9. 从苏宁电器到卡巴斯基第20篇:曲折考研路(补)
  10. RC低通滤波原理(笔记)