天天看點

基于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的圖像拼接_【圖像處理】基于塊比對的全景圖像拼接