本文讲述如何利用Matlab Image Processing Toolbox中的图像配准工具实现线性正投影、仿射、投影、多项式、分段线性、局部加权平均配准的过程。
实验平台
X86 PC,Windows XP sp2, Matlab 7.1
资源的获取
图片资源来自http://vision.ece.ucsb.edu/registration/satellite/testimag.html,其中每个压缩包里存有两副图片,每副图片以矩阵形式保存。
matlab工具的使用方法:查看帮助mage Processing Toolbox User's Guide——Image registration。
涉及配准方法简介
该工具箱提供的配准方法均需手工选择图像间的匹配点对(control points pair),均属于交互配准方法。其基本过程为:读入图像数据->在两副图像上选择足够匹配点->选择配准算法,计算变换参数->变换图像。
假设input image(输入图像)为欲进行配准的图像,base image为配准是的参考图像。以下是我参考matlab帮助给出了简介。
1.线性正投影(linear conformal):最简单。平面映射成平面。
当输入输入图像与参考图像对比,只是存在全局的平移、旋转、缩放或其三者组合的差别时(正方形仍对应正方形),选择此配准方法。此方法至少需要2对匹配点。
2.仿射(affine):将平行线转换成平行线。
当输入图像形状存在切变现象(正方形对应平行四边形),选此法。至少需3对匹配点。
3.投影(projective):将直线映射成直线。
如果输入图像呈现倾斜,翘起现象,选此法。至少需4对匹配点。
4.多项式(polynomial):将直线映射成曲线。
如果输入图像出现不规则曲变,采用此法。Matlab中提供有2、3、4次幂的实现,分别至少需要6,10,10对匹配点。
5.分段线性(piecewise linear)
如果输入图像的各个局部之间的退化模式明显不一样,选此法。至少需要4对匹配点。
6.局部加权平均(local weighted mean)
与分段线性一致,但效果较之好。至少需要6对(推荐12对)匹配点。
实验步骤
1.读取图像数据。
因为源图像以矩阵形式存在一个二进制的文件里,用fread可将其读取到变量矩阵中。将读取文件编制成一个子函数(RTIread.m),源代码如下:
function imMatrix=RTIread(FILENAME,SIZE)
%RTIread Read the image matrix from binary "Registration Test Image" file.
% imMatrix=RTIread(FILENAME,SIZE) opens the file FILENAME, and reads the
% number of elements specified by SIZE.
%
% FILENAME is a string containing the name of the file to be opened.
% Valid entries for SIZE are:
% N read N elements into a column vector.
% inf read to the end of the file.
% [M,N] read elements to fill an M-by-N matrix, in column order.
% N can be inf, but M can't.
%
% It returns the image matrix.
fid=fopen(FILENAME,'r');
imMatrix=fread(fid,SIZE,'uint8=>uint8');
fclose(fid);
%image(imMatrix);
这里我们选取了两张600×600的图片,文件名为“casitas84”和“casitas86”。运行以下代码读取图像矩阵:
% 1. Read the images into the MATLAB workspace.
base=RTIread('casitas84',[600,600]);
input=RTIread('casitas86',[600,600]);
2.选取匹配点(control points)。
根据预定的配准方法,选定足够的匹配点对。运行下列代码:
% 2.Specify control point pairs n the images and save.
cpselect(input,base);
出现GUI界面。
操作很简单,只需注意选点要均匀布开,以增加其代表性。选定完毕,File-> Save Points to Workspace将数据保存到工作区中。Workspace立刻多出两个N×2的数组(其中N为选定的匹配点对数),分别为input_points和base_points,如:
input_points =
3.利用十字相关法调整选定了的匹配点。
这步可选。运行代码:
% 3.Fine-tune the control points using cross-correlation.
input_points_corr = cpcorr(input_points,base_points,input,base); %optimism the points
input_points_corr为优化后在输入图片的对应匹配点。
4.计算变换公式的参数。
利用cp2tform,选定变换类型(即配准方法),计算变换参数。以下只需选定一种即可。
% 4.Specify the type of transformation to be used and infer its parameters
% (1) not Fine-tune points
Tlinear = cp2tform(input_points,base_points,'linear conformal');
Taffine = cp2tform(input_points,base_points,'affine');
Tprojective = cp2tform(input_points,base_points,'projective');
Tpolynomial2 = cp2tform(input_points,base_points,'polynomial',2);
Tpolynomial3 = cp2tform(input_points,base_points,'polynomial',3);
Tpolynomial4 = cp2tform(input_points,base_points,'polynomial',4);
Tpiecewise = cp2tform(input_points,base_points,'piecewise linear');
Tlwm = cp2tform(input_points,base_points,'lwm');
% (2)Fine-tune points
fTlinear = cp2tform(input_points_corr,base_points,'linear conformal');
fTaffine = cp2tform(input_points_corr,base_points,'affine');
fTprojective = cp2tform(input_points_corr,base_points,'projective');
fTpolynomial2 = cp2tform(input_points_corr,base_points,'polynomial',2);
fTpolynomial3 = cp2tform(input_points_corr,base_points,'polynomial',3);
fTpolynomial4 = cp2tform(input_points_corr,base_points,'polynomial',4);
fTpiecewise = cp2tform(input_points_corr,base_points,'piecewise linear');
fTlwm = cp2tform(input_points_corr,base_points,'lwm');
诸如Tlinear的变量为一个称为TFORM的数据结构,尚没做仔细研究:
Tlinear =
5.变换图像。
% 5.Transform the unregistered image to bring it into alignment.
title('image registration polynomial method');
subplot(2,2,1);
imshow(base);
title('Base image');
subplot(2,2,2);
imshow(input);
title('Input image');
subplot(2,2,3);
imshow(imtransform(input,Tpolynomial2));
title('registered image');
subplot(2,2,4);
imshow(imtransform(input,fTpolynomial2));
title('registered image(fine-tune points)');
结果如下:
总结
1.image和imshow区别。前者视base,input此类二维图片矩阵为索引图像,在系统的index库中选取颜色。
2.选择适当的方法来建立转换参数,并非算法越复杂越好,应参考成像因素(退化因素)。
3.尚没有看出十字相关法的好处。
4. 利用cpselect选择匹配点,cpselect可以返回一个GUI句柄。欲实现如下功能:当打开cpselect GUI 时,m文件程序暂停运行,关闭之后继续执行。因为对GUI编程不懂,