天天看点

基于matlab的图像拼接_【图像处理】基于块匹配的全景图像拼接

对于一组具有局部重叠区域的图像。采用块匹配的方法,设计运动估计方法,搜索相似部分,并进行匹配,融合生成拼接图。

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 EDITgui_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});endif 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_Mainclc; 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 structureguidata(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 structurevarargout{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;endfile = 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;endfile = File_Process(filename, pathname);if length(file) < 2    msgbox('请选择至少两幅图像!', '提示信息', 'modal');    return;endImg1 = 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;enddf = 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;    endelse    msgbox('请选择至少两幅图像!', '提示信息', 'modal');    return;endfile = File_Process(filename, pathname);if length(file) < 2    msgbox('请选择至少两幅图像!', '提示信息', 'modal');    return;endImg1 = 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;endif isempty(handles.MStitch)    msgbox('请先进行图像匹配!', '提示信息', 'modal');    return;endif ~isempty(handles.grayResult)    msgbox('灰度拼接图像已完成!', '提示信息', 'modal');    return;endif length(handles.file)    [MStitch, result] = GrayMain_Process(handles.MStitch, ...        handles.W_box, handles.H_box, handles.bdown);endgrayResult = im2uint8(mat2gray(result));axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);imshow(grayResult, []);title('灰度图像拼接结果', 'FontWeight', 'Bold');handles.grayResult = grayResult;guidata(hObject, handles);msgbox('灰度拼接图像完成!', '提示信息', 'modal');% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton3 (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;endif isempty(handles.MStitch)    msgbox('请先进行图像匹配!', '提示信息', 'modal');    return;endif ~isempty(handles.RGBResult)    msgbox('彩色拼接图像已完成!', '提示信息', 'modal');    return;endif length(handles.file)    [MStitch, result] = RGBMain_Process(handles.MStitch, ...        handles.W_box, handles.H_box, handles.bdown);endRGBResult = im2uint8(mat2gray(result));axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);imshow(RGBResult, []);title('彩色图像拼接结果', 'FontWeight', 'Bold');handles.RGBResult = RGBResult;guidata(hObject, handles);msgbox('彩色拼接图像完成!', '提示信息', 'modal');% --- Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton4 (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.grayResult) && isempty(handles.RGBResult)    msgbox('请先拼接处理图像!', '提示信息', 'modal');    return;endif ~isempty(handles.grayResult)    figure(1); imshow(handles.grayResult, []);    title('灰度图像拼接结果', 'FontWeight', 'Bold');endif ~isempty(handles.RGBResult)    figure(2); imshow(handles.RGBResult, []);    title('彩色图像拼接结果', 'FontWeight', 'Bold');endif ~isempty(handles.grayListResult)    figure(3); imshow(handles.grayListResult, []);    title('序列灰度图像拼接结果', 'FontWeight', 'Bold');endif ~isempty(handles.RGBListResult)    figure(4); imshow(handles.RGBListResult, []);    title('序列彩色图像拼接结果', 'FontWeight', 'Bold');end% --- Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton6 (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.grayResult) && isempty(handles.RGBResult)    msgbox('请先拼接处理图像!', '提示信息', 'modal');    return;end[filename, pathname] = uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...    '*.*','All Files' }, '保存结果', ...    'Result\result.jpg');if isempty(filename)    return;end[pathstr, name, ext] = fileparts(filename);if ~isempty(handles.grayResult)    file = fullfile(pathname, [name, '_gray', ext]);    imwrite(handles.grayResult, file);endif ~isempty(handles.RGBResult)    file = fullfile(pathname, [name, '_rgb', ext]);    imwrite(handles.RGBResult, file);endif ~isempty(handles.grayListResult)    file = fullfile(pathname, [name, '_grayList', ext]);    imwrite(handles.grayListResult, file);endif ~isempty(handles.RGBListResult)    file = fullfile(pathname, [name, '_rgbList', ext]);    imwrite(handles.RGBListResult, file);endmsgbox('保存拼接图像成功!', '提示信息', 'modal');% --- Executes on button press in pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton7 (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;endif ~isempty(handles.MStitch)    msgbox('图像匹配已完成!', '提示信息', 'modal');    return;endfile = handles.file;im1 = imread(file{1});MStitch.imrgb1 = double(im1); im1 = rgb2gray(im1);MStitch.im1 = double(im1); [Pheight, Pwidth] = size(im1);MStitch.Pwidth = Pwidth; MStitch.Pheight = Pheight; MStitch.W_min = round(0.60*Pwidth);MStitch.W_max = round(0.83*Pwidth); MStitch.H_min = round(0.98*Pheight); MStitch.minval = 255;im2 = imread(file{2});MStitch.imrgb2 = double(im2);im2 = rgb2gray(im2);im2 = double(im2);MStitch.im2 = double(im2);[W_box, H_box, bdown, MStitch] = Fun_Match(im2, MStitch);msgbox('图像匹配完成!', '提示信息', 'modal');handles.W_box = W_box;handles.H_box = H_box;handles.bdown = bdown;handles.MStitch = MStitch;guidata(hObject, handles);% --- Executes on button press in pushbutton8.function pushbutton8_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton8 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)choice = questdlg('您确定要退出图像拼接系统?', ...    '退出选项', ...    '退出', '取消', '取消');switch choice    case '退出'        close all;    case '取消'        return;end% --- Executes on button press in pushbutton9.function pushbutton9_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton9 (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;endif ~isempty(handles.grayListResult)    msgbox('序列灰度图像拼接已完成!', '提示信息', 'modal');    return;endif length(handles.file) == 2    if ~isempty(handles.grayResult)        msgbox('灰度拼接图像已完成!', '提示信息', 'modal');        return;    end    if isempty(handles.MStitch)        msgbox('请先进行图像匹配!', '提示信息', 'modal');        return;    end    [MStitch, result] = GrayMain_Process(handles.MStitch, ...        handles.W_box, handles.H_box, handles.bdown);    grayResult = im2uint8(mat2gray(result));    axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);    imshow(grayResult, []);    title('灰度图像拼接结果', 'FontWeight', 'Bold');        handles.grayResult = grayResult;    guidata(hObject, handles);        msgbox('灰度拼接图像完成!', '提示信息', 'modal');else    [MStitch, result] = GrayListMain_Process(handles.file);    grayListResult = im2uint8(mat2gray(result));    axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);    imshow(grayListResult, []);    title('序列灰度图像拼接结果', 'FontWeight', 'Bold');        handles.grayListResult = grayListResult;    guidata(hObject, handles);        msgbox('序列灰度拼接图像完成!', '提示信息', 'modal');end% --- Executes on button press in pushbutton10.function pushbutton10_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton10 (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;endif ~isempty(handles.RGBListResult)    msgbox('序列彩色图像拼接已完成!', '提示信息', 'modal');    return;endif length(handles.file) == 2    if ~isempty(handles.RGBResult)        msgbox('彩色拼接图像已完成!', '提示信息', 'modal');        return;    end    if isempty(handles.MStitch)        msgbox('请先进行图像匹配!', '提示信息', 'modal');        return;    end    [MStitch, result] = RGBMain_Process(handles.MStitch, ...        handles.W_box, handles.H_box, handles.bdown);    RGBResult = im2uint8(mat2gray(result));    axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);    imshow(RGBResult, []);    title('彩色图像拼接结果', 'FontWeight', 'Bold');        handles.grayResult = grayResult;    guidata(hObject, handles);        msgbox('彩色拼接图像完成!', '提示信息', 'modal');else    [MStitch, result] = RGBListMain_Process(handles.file);    RGBListResult = im2uint8(mat2gray(result));    axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel', []);    imshow(RGBListResult, []);    title('序列彩色图像拼接结果', 'FontWeight', 'Bold');        handles.RGBListResult = RGBListResult;    guidata(hObject, handles);        msgbox('序列彩色拼接图像完成!', '提示信息', 'modal');end
           
function [x, y] = CheckRC(x, y, im)y = max(y, 1);y = min(y, size(im, 1));x = max(x, 1);x = min(x, size(im, 2));
           
function file = File_Process(filename, pathname)if ~isa(filename, 'cell') && size(filename, 1) == 1    [pathstr, name, ext] = fileparts(filename);    pathname = strrep(pathname, '\', '\\');    imfile = ls([pathname, '*', strtrim(ext)]);    for i = 1 : size(imfile, 1)        file{i} = fullfile(pathname, imfile(i, :));    endelse    for i = 1 : length(filename)        file{i} = fullfile(pathname, filename{i});    endend
           
function [W_box, H_box, bdown, MStitch] = Fun_Match(im2, MStitch)Pwidth = MStitch.Pwidth;Pheight = MStitch.Pheight; W_min = MStitch.W_min; W_max = MStitch.W_max;H_min = MStitch.H_min; minval = MStitch.minval; im1 = MStitch.im1;[Fheight, Fwidth] = size(im2);hw = waitbar(0, '图像匹配进度:', 'Name', '图像匹配……');w_ind = 64; h_ind = 151;for w = W_min : W_max    for h = H_min : Fheight        imsum = 0;         x2 = 1;        for x1 = Pwidth-w : 5 : Pwidth            y2 = 1;            for y1 = Pheight-h+1 : 5 : Pheight                [x1, y1] = CheckRC(x1, y1, im1);                [x2, y2] = CheckRC(x2, y2, im2);                imsum = imsum + abs(im1(y1, x1) - im2(y2, x2));                y2 = y2 + 5;            end            x2 = x2 + 5;        end        if imsum*5*5 < minval*w*h            minval = imsum*5*5/(w*h);            w_ind = w;            h_ind = h;        end    end    rt = 0.5*(w - W_min)/(W_max - W_min);    waitbar(rt, hw, sprintf('图像匹配进度:%i%%', round(rt*100)));endW_box = w_ind-1;H_box = h_ind+1;bdown = 1;if H_box < size(im2, 1)    H_box = size(im2, 1);endfor w = W_min : W_max    for h = H_min : Fheight        imsum = 0;         x2 = 1;        for x1 = Pwidth-w : 5 : Pwidth            y1 = 1;            for y2 = Fheight-h+1 : 5 : Fheight                [x1, y1] = CheckRC(x1, y1, im1);                [x2, y2] = CheckRC(x2, y2, im2);                imsum = imsum + abs(im1(y1, x1) - im2(y2, x2));                y1 = y1 + 5;            end            x2 = x2 + 5;        end        if imsum*5*5 < minval*w*h            minval = imsum*5*5/(w*h);            w_ind = w;            h_ind = h;            bdown = 0;        end    end    rt = 0.5 + 0.5*(w - W_min)/(W_max - W_min);    waitbar(rt, hw, sprintf('图像匹配进度:%i%%', round(rt*100)));endMStitch.minval = minval;delete(hw);
           
function [MStitch, im] = Fun_Stitch(im2, W_box, H_box, bdown, MStitch)W_min = MStitch.W_min; W_max = MStitch.W_max;H_min = MStitch.H_min;minval = MStitch.minval;im1 = MStitch.im1; [Fheight, Fwidth] = size(im2); im = im1; [Pheight, Pwidth] = size(im); w = 0; hw = waitbar(0, '图像拼接进度:', 'Name', '图像拼接……');if bdown    x2 = 1;    for x1 = Pwidth-W_box : Pwidth        y2 = 1;        for y1 = Pheight-H_box+1 : Pheight            [x1, y1] = CheckRC(x1, y1, im1);            [x2, y2] = CheckRC(x2, y2, im2);            w = x2/W_box;             im(y1, x1) = im1(y1, x1)*(1.0-w) + im2(y2, x2)*w;            y2 = y2 + 1;        end        x2 = x2 + 1;        rt = 0.5*(x1 - Pwidth + W_box)/W_box;        waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));    end    rt0 = rt;    for y1 = 1 : H_box        for x3 = x2 : Fwidth            [x1, y1] = CheckRC(x1, y1, im1);            [x3, y1] = CheckRC(x3, y1, im2);            im(y1, Pwidth+x3-x2+1) = im2(y1, x3);        end        rt = rt0 + 0.5*(y1 - 1)/H_box;        waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));    endelse    x2 = 1;    for x1 = Pwidth-W_box : Pwidth        y2 = 1;        for y1 = Fheight-H_box+1 : Fheight            [x1, y1] = CheckRC(x1, y1, im1);            [x2, y2] = CheckRC(x2, y2, im2);            w = x2/W_box;             im(y1, x1) = im1(y1, x1)*(1.0-w) + im2(y2, x2)*w;            y2 = y2 + 1;        end        x2 = x2 + 1;        rt = 0.5*(x1 - Pwidth + W_box)/W_box;        waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));    end    rt0 = rt;    for y1 = Fheight-H_box+1 : Fheight        for x3 = x2 : Fwidth            [x1, y1] = CheckRC(x1, y1, im1);            [x3, y1] = CheckRC(x3, y1, im2);            im(y1, Pwidth+x3-x2+1) = im2(y1, x3);        end        rt = rt0 + 0.5*(y1 - 1)/H_box;        waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));    endendMStitch.im1 = im;[Pheight, Pwidth] = size(im); MStitch.Pwidth = Pwidth;MStitch.Pheight = Pheight; rt = 1;waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));delete(hw);
           
function [MStitch, im] = Fun_StitchRGB(im2, W_box, H_box, bdown, MStitch)W_min = MStitch.W_min;W_max = MStitch.W_max;H_min = MStitch.H_min;minval = MStitch.minval;im1 = MStitch.imrgb1;[Fheight, Fwidth] = size(im2); im = MStitch.imrgb1;im2 = MStitch.imrgb2;[Pheight, Pwidth, depth] = size(im);sz2 = size(im2);[Pheight2, Pwidth2, depth2] = size(im2); w = 0; hw = waitbar(0, '图像拼接进度:', 'Name', '图像拼接……');if bdown    x2 = 1;    for x1 = Pwidth-W_box : Pwidth        y2 = 1;        for y1 = Pheight-H_box+1 : Pheight            [x1, y1] = CheckRC(x1, y1, im1);            [x2, y2] = CheckRC(x2, y2, im2);            w = x2/W_box;             im(y1, x1, 1) = im1(y1, x1, 1)*(1.0-w) + im2(y2, x2, 1)*w;            im(y1, x1, 2) = im1(y1, x1, 2)*(1.0-w) + im2(y2, x2, 2)*w;            im(y1, x1, 3) = im1(y1, x1, 3)*(1.0-w) + im2(y2, x2, 3)*w;            y2 = y2 + 1;        end        x2 = x2 + 1;        rt = 0.5*(x1 - Pwidth + W_box)/W_box;        waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));    end    rt0 = rt;    for y1 = 1 : H_box        for x3 = x2 : Pwidth2            [x1, y1] = CheckRC(x1, y1, im1);            [x3, y1] = CheckRC(x3, y1, im2);            im(y1, Pwidth+x3-x2+1, :) = im2(y1, x3, :);        end        rt = rt0 + 0.5*(y1 - 1)/H_box;        waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));    endelse    x2 = 1;    for x1 = Pwidth-W_box : Pwidth        y2 = 1;        for y1 = Fheight-H_box+1 : Fheight            [x1, y1] = CheckRC(x1, y1, im1);            [x2, y2] = CheckRC(x2, y2, im2);            w = x2/W_box;             im(y1, x1, 1) = im1(y1, x1, 1)*(1.0-w) + im2(y2, x2, 1)*w;            im(y1, x1, 2) = im1(y1, x1, 2)*(1.0-w) + im2(y2, x2, 2)*w;            im(y1, x1, 3) = im1(y1, x1, 3)*(1.0-w) + im2(y2, x2, 3)*w;            y2 = y2 + 1;        end        x2 = x2 + 1;        rt = 0.5*(x1 - Pwidth + W_box)/W_box;        waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));    end    rt0 = rt;    for y1 = Fheight-H_box+1 : Fheight        for x3 = x2 : Fwidth            [x1, y1] = CheckRC(x1, y1, im1);            [x3, y1] = CheckRC(x3, y1, im2);            im(y1, Pwidth+x3-x2+1, :) = im2(y1, x3, :);        end        rt = rt0 + 0.5*(y1 - 1)/H_box;        waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));    endendMStitch.imrgb1 = im; [Pheight, Pwidth, depth] = size(im);MStitch.Pwidth = Pwidth; MStitch.Pheight = Pheight;rt = 1;waitbar(rt, hw, sprintf('图像拼接进度:%i%%', round(rt*100)));delete(hw);
           
function [MStitch, result] = GrayListMain_Process(file)for i = 1 : length(file)-1    if i == 1        file1 = file{i};        im1 = imread(file1);        im1 = rgb2gray(im1);        MStitch.im1 = double(im1);                [Pheight, Pwidth] = size(im1);                MStitch.Pwidth = Pwidth;         MStitch.Pheight = Pheight;                 MStitch.W_min = round(0.60*Pwidth);        MStitch.W_max = round(0.83*Pwidth);        MStitch.H_min = round(0.98*Pheight);         MStitch.minval = 255;                im1 = imread(file1);        MStitch.imrgb1 = double(im1);        im1 = rgb2gray(im1);        MStitch.im1 = double(im1);    else        MStitch.im1 = double(result1);    end    file2 = file{i+1};       im2 = imread(file2);    MStitch.imrgb2 = double(im2);    im2 = rgb2gray(im2);    im2 = double(im2);        [W_box, H_box, bdown, MStitch] = Fun_Match(im2, MStitch);    [MStitch, result1] = Fun_Stitch(im2, W_box, H_box, bdown, MStitch);endresult = result1;
           
function [MStitch, result] = GrayMain_Process(MStitch, W_box, H_box, bdown)[MStitch, result] = Fun_Stitch(MStitch.im2, W_box, H_box, bdown, MStitch);
           
function Img = ImageList(file)Img = [];Img1 = imread(file{1});spc = cat(3, ...    repmat(ones(1, 5)*255, size(Img1, 1), 1), ...    repmat(zeros(1, 5), size(Img1, 1), 1), ...    repmat(zeros(1, 5), size(Img1, 1), 1));if length(file) == 2    Img = imread(file{2});    return;endfor i = 2 : length(file)    Imgi = imread(file{i});    Img = [Img Imgi spc];end
           
function [MStitch, result] = RGBListMain_Process(file)for i = 1 : length(file)-1    if i == 1        file1 = file{i};        im1 = imread(file1);        im1 = rgb2gray(im1);        MStitch.im1 = double(im1);                [Pheight, Pwidth] = size(im1);                MStitch.Pwidth = Pwidth;         MStitch.Pheight = Pheight;                MStitch.W_min = round(0.60*Pwidth);         MStitch.W_max = round(0.83*Pwidth);         MStitch.H_min = round(0.98*Pheight);         MStitch.minval = 255;                im1 = imread(file1);        MStitch.imrgb1 = double(im1);        im1 = rgb2gray(im1);        MStitch.im1 = double(im1);    else        MStitch.im1 = double(result1);    end    file2 = file{i+1};       im2 = imread(file2);    MStitch.imrgb2 = double(im2);    im2 = rgb2gray(im2);    im2 = double(im2);        [W_box, H_box, bdown, MStitch] = Fun_Match(im2, MStitch);    [MStitch, result1] = Fun_StitchRGB(im2, W_box, H_box, bdown, MStitch);endresult = result1;
           
function [MStitch, result] = RGBMain_Process(MStitch, W_box, H_box, bdown)[MStitch, result] = Fun_StitchRGB(MStitch.im2, W_box, H_box, bdown, MStitch);
           
基于matlab的图像拼接_【图像处理】基于块匹配的全景图像拼接

往期回顾>>>>>>

【模式识别】Matlab指纹识别【优化求解】A*算法解决三维路径规划问题【优化求解】模拟退火遗传实现带时间窗的车辆路径规划问题【数学建模】Matlab实现SEIR模型【优化求解】基于NSGA-2的求解多目标柔性车间调度算法【优化求解】蚁群算法求最优值  【图像处理】粒子群算法结合模糊聚类分割算法实现图像的分割 【优化算法】基于粒子群算法的充电站最优布局【优化求解】粒子群算法之电线布局优化【优化求解】遗传算法解决背包问题

【优化求解】基于粒子群之机器人栅格路径规划

基于matlab的图像拼接_【图像处理】基于块匹配的全景图像拼接