【图像拼接】基于matlab块匹配全景图像拼接【含Matlab源码 742期】

994 阅读6分钟

一、简介

块匹配是图像去噪,运动估计中常用的一种方法。通过将查询块与相邻的图像块进行匹配,从这些相邻块中找出距离查询块距离最近的 K 个块。所谓的相邻也并不是绝对的位置上的相邻,也由此可以引出局部搜索(local)和全局搜索(non-local)。
在这里插入图片描述
如上图所示:

  • 是一个以 (红色) 为中心的窗(搜索窗),最大的灰色矩阵区域;
  • 是以 为中心的矩阵邻域(一个图像块,查询块),紫色矩阵区域;
  • 是以 为中心的矩阵邻域(一个图像块,近邻块),紫色矩阵区域。

如果 表示整幅图像,那么就是全局搜索,否则成为局部搜索。

1 基于像素的 Non-local Means
在 Non-local Means 图像去噪中,会使用近似块来估计查询块,以实现去噪的目的,如下式所示:
其中权重的计算如下所示:
其中 为图像的平滑参数,用于控制平滑噪声的强度,如果 比较小的话,幂函数的衰减效果比较显著,细节保留程度比较高,因此会保持图像本身的细节信息。
事实上并不是所有的近邻块都与查询块相似,因此可以只考虑几个权重比较大的块来进行估计,也就是对权重进行排序,选择权重大的作为相似块。这样不仅可以提高去噪效果还可以减少计算量。
另外,Mahmoudi 在文章 《Fast Non Local Means Denoising for 3D MR Images》中也提出了一种新的方法(根据 和 的均值和方差来选择相似块)避免许多无用的计算(欧式距离)。
为了避免重复计算,可以提前计算均值和方差。

2 基于图像块的 Non-local Means
在第 2 部分中所述的都是基于单个像素(每个块的中心位置)的处理,因此导致计算量很大。但实际上我们计算的是图像块的相似度,因此可以扩展为图像块的处理。如下图所示:
在这里插入图片描述
则块匹配的计算变为:
优化之后的权重估计变为:
如下是文献《An Optimized Blockwise NL-means Denoising Filter for 3D Magnetic Resonance Images》中的实验结果:
在这里插入图片描述
其中:

  • NL-means,原始的non local means方法
  • Blockwise NL-means,每次重构一个子块,而不是单个像素
  • Optimized NL-means,针对NL-means,权重的选择进行优化,直选几个最大的权重
  • Optimized Blockwise NL-means,针对Optimized NL-means,权重选择进行了优化
    从上可以看出每次对单个像素处理比每次对一个块处理的效果好,但是计算时间长;而对权重参数进行优化后效果明显改善;另外,优化过的方法中选用了多线程的方法。

二、源代码

function varargout = Gui_Main(varargin)
% GUI_MAIN MATLAB code for Gui_Main.fig
%      GUI_MAIN, by itself, creates a new GUI_MAIN or raises the existing
%      singleton*.
%
%      H = GUI_MAIN returns the handle to a new GUI_MAIN or the handle to
%      the existing singleton*.
%
%      GUI_MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_MAIN.M with the given input arguments.
%
%      GUI_MAIN('Property','Value',...) creates a new GUI_MAIN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Gui_Main_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Gui_Main_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Gui_Main

% Last Modified by GUIDE v2.5 27-Apr-2011 09:29:29

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @Gui_Main_OpeningFcn, ...
    'gui_OutputFcn',  @Gui_Main_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Gui_Main is made visible.
function Gui_Main_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Gui_Main (see VARARGIN)

% Choose default command line output for Gui_Main
clc; warning off all;
axes(handles.axes1); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes2); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);

handles.output = hObject;
handles.file = [];
handles.MStitch = [];
handles.grayResult = [];
handles.RGBResult = [];
handles.grayListResult = [];
handles.RGBListResult = [];

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Gui_Main wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Gui_Main_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --------------------------------------------------------------------
function uipushtool1_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to uipushtool1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
    '*.*','All Files' }, '保存结果', ...
    'Result\result_gui.jpg');
if isempty(filename)
    return;
end
file = fullfile(pathname, filename);
f = getframe(gcf);
f = frame2im(f);
imwrite(f, file);
msgbox('保存GUI结果图像成功!', '提示信息', 'modal');


% --------------------------------------------------------------------
function uipushtool2_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to uipushtool2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

axes(handles.axes1); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes2); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);

handles.file = [];
handles.MStitch = [];
handles.grayResult = [];
handles.RGBResult = [];
handles.grayListResult = [];
handles.RGBListResult = [];

[filename, pathname, filterindex] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','All Image Files';...
    '*.*','All Files' }, '选择待处理图像', ...
    '.\\images\\', 'MultiSelect', 'on');
if ~isa(filename, 'cell') && isequal(filename, 0)
    return;
end

file = File_Process(filename, pathname);
if length(file) < 2
    msgbox('请选择至少两幅图像!', '提示信息', 'modal');
    return;
end
Img1 = imread(file{1});
Img2 = ImageList(file);
axes(handles.axes1);
imshow(Img1); title('图像序列1', 'FontWeight', 'Bold');
axes(handles.axes2);
imshow(Img2); title('图像序列2', 'FontWeight', 'Bold');

handles.Img1 = Img1;
handles.Img2 = Img2;
handles.file = file;
guidata(hObject, handles);


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes2); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);
axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);

handles.file = [];
handles.MStitch = [];
handles.grayResult = [];
handles.RGBResult = [];

dname = uigetdir('.\\images\\风景图像', '请选择待处理图像文件夹:');
if dname == 0
    return;
end
df = ls(dname);
if length(df) > 2
    for i = 1 : size(df, 1)
        if strfind(df(i, :), '.db');
            df(i, :) = [];
            break;
        end
    end
    if length(df) > 2
        filename = fullfile(dname, df(end, :));
        pathname = [dname '\'];
    else
        msgbox('请选择至少两幅图像!', '提示信息', 'modal');
        return;
    end
else
    msgbox('请选择至少两幅图像!', '提示信息', 'modal');
    return;
end
file = File_Process(filename, pathname);
if length(file) < 2
    msgbox('请选择至少两幅图像!', '提示信息', 'modal');
    return;
end
Img1 = imread(file{1}); 
Img2 = ImageList(file); 
axes(handles.axes1);
imshow(Img1); title('图像序列1', 'FontWeight', 'Bold');
axes(handles.axes2);
imshow(Img2); title('图像序列2', 'FontWeight', 'Bold');

handles.Img1 = Img1;
handles.Img2 = Img2;
handles.file = file;
guidata(hObject, handles);



% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton1.
function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on mouse press over axes background.
function axes2_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on mouse press over axes background.
function axes3_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on mouse press over axes background.
function axes4_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if isempty(handles.file)
    msgbox('请先载入图像!', '提示信息', 'modal');
    return;
end
if isempty(handles.MStitch)
    msgbox('请先进行图像匹配!', '提示信息', 'modal');
    return;
end
if ~isempty(handles.grayResult)
    msgbox('灰度拼接图像已完成!', '提示信息', 'modal');
    return;
end
if length(handles.file)
    [MStitch, result] = GrayMain_Process(handles.MStitch, ...
        handles.W_box, handles.H_box, handles.bdown);
end

三、运行结果

在这里插入图片描述
在这里插入图片描述

四、备注

版本:2014a