《数字图像处理原理与实践(MATLAB版)》一书之代码Part9

本文系《数字图像处理原理与实践(MATLAB版)》一书之代码系列的Part9,,辑录该书第431至第438页之代码,供有需要读者下载研究使用。至此全书代码发布已经接近尾声,希望这些源码能够对有需要的读者有所帮助。代码执行结果请参见原书配图,建议下载代码前阅读下文:关于《数字图像处理原理与实践(MATLAB版)》一书代码发布的说明

首先给出的是原书P438所列之程序源码,详情请参见原书之相关内容。

===========================================================

I1=imread(‘box.png’);I2=imread(‘box_in_scene.png’);Options.upright=true;Options.tresh=0.0001;Ipts1=OpenSurf(I1,Options);Ipts2=OpenSurf(I2,Options);D1 = reshape([Ipts1.descriptor],64,[]); D2 = reshape([Ipts2.descriptor],64,[]); err=zeros(1,length(Ipts1));cor1=1:length(Ipts1); cor2=zeros(1,length(Ipts1));for i=1:length(Ipts1), distance=sum((D2-repmat(D1(:,i),[1 length(Ipts2)])).^2,1); [err(i),cor2(i)]=min(distance);end[err, ind]=sort(err); cor1=cor1(ind); cor2=cor2(ind);I = zeros([max(size(I1,1),size(I2,1)),size(I1,2)+size(I1,2)]);I(1:size(I1,1),1:size(I1,2))=I1;I(:,size(I1,2)+1:size(I1,2)+size(I2,2))=I2;figure, imshow(I, []); hold on;for i=1:30, c=rand(1,3); plot([Ipts1(cor1(i)).x Ipts2(cor2(i)).x+size(I1,2)],… [Ipts1(cor1(i)).y Ipts2(cor2(i)).y],’-‘,’Color’,c) plot([Ipts1(cor1(i)).x Ipts2(cor2(i)).x+size(I1,2)],… [Ipts1(cor1(i)).y Ipts2(cor2(i)).y],’o’,’Color’,c)end

由于上述代码调用的函数所占篇幅过长,于是以下部分之代码并未辑录于书中,在此详细列出,以供有需要的读者参阅。特别说明,下面的程序源码由荷兰特温特大学的Dirk-Jan Kroon博士提供,使用条款请参见相关说明。

================================================================================

function ipts=OpenSurf(img,Options)% This function OPENSURF, is an implementation of SURF (Speeded Up Robust % Features). SURF will detect landmark points in an image, and describe% the points by a vector which is robust against (a little bit) rotation % ,scaling and noise. It can be used in the same way as SIFT (Scale-invariant % feature transform) which is patented. Thus to align (register) two % or more images based on corresponding points, or make 3D reconstructions.%% This Matlab implementation of Surf is a direct translation of the % OpenSurf C# code of Chris Evans, and gives exactly the same answer. % Chris Evans wrote one of the best, well structured all inclusive SURF % implementations. On his site you can find Evaluations of OpenSURF % and the C# and C++ code. % Chris Evans gave me permisson to publish this code under the (Mathworks)% BSD license.%% Ipts = OpenSurf(I, Options)%% inputs,% I : The 2D input image color or greyscale% (optional)% Options : A struct with options (see below)%% outputs,% Ipts : A structure with the information about all detected Landmark points% Ipts.x , ipts.y : The landmark position% Ipts.scale : The scale of the detected landmark% Ipts.laplacian : The laplacian of the landmark neighborhood% Ipts.orientation : Orientation in radians% Ipts.descriptor : The descriptor for corresponding point matching%% options,% Options.verbose : If set to true then useful information is % displayed (default false)% Options.upright : Boolean which determines if we want a non-rotation% invariant result (default false)% Options.extended : Add extra landmark point information to the% descriptor (default false)% Options.tresh : Hessian response treshold (default 0.0002)% Options.octaves : Number of octaves to analyse(default 5)% Options.init_sample : Initial sampling step in the image (default 2)% % Example 1, Basic Surf Point Detection% % Load image% I=imread(‘TestImages/test.png’);% % Set this option to true if you want to see more information% Options.verbose=false; % % Get the Key Points% Ipts=OpenSurf(I,Options);% % Draw points on the image% PaintSURF(I, Ipts);%% Example 2, Corresponding points% % See, example2.m%% Example 3, Affine registration% % See, example3.m%% Function is written by D.Kroon University of Twente (July 2010)% Add subfunctions to Matlab Search pathfunctionname=’OpenSurf.m’;functiondir=which(functionname);functiondir=functiondir(1:end-length(functionname));addpath([functiondir ‘/SubFunctions’]) % Process inputsdefaultoptions=struct(‘tresh’,0.0002,’octaves’,5,’init_sample’,2,’upright’,false,’extended’,false,’verbose’,false);if(~exist(‘Options’,’var’)), Options=defaultoptions; else tags = fieldnames(defaultoptions); for i=1:length(tags) if(~isfield(Options,tags{i})), Options.(tags{i})=defaultoptions.(tags{i}); end end if(length(tags)~=length(fieldnames(Options))), warning(‘register_volumes:unknownoption’,’unknown options found’); endend% Create Integral Imageiimg=IntegralImage_IntegralImage(img);% Extract the interest pointsFastHessianData.thresh = Options.tresh;FastHessianData.octaves = Options.octaves;FastHessianData.init_sample = Options.init_sample;FastHessianData.img = iimg;ipts = FastHessian_getIpoints(FastHessianData,Options.verbose);% Describe the interest pointsif(~isempty(ipts)) ipts = SurfDescriptor_DecribeInterestPoints(ipts,Options.upright, Options.extended, iimg, Options.verbose);end

================================================================================

每天告诉自己一次,『我真的很不错』

《数字图像处理原理与实践(MATLAB版)》一书之代码Part9

相关文章:

你感兴趣的文章:

标签云: