matlab中图像映射实例,[转载]Matlab实现多种图像配准(转)

本文讲述如何利用Matlab Image Processing Toolbox中的图像配准工具实现线性正投影、仿射、投影、多项式、分段线性、局部加权平均配准的过程。

实验平台

X86 PC,Windows XP sp2, Matlab 7.1

资源的获取

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)

%RTIreadRead 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:

%Nread N elements into a column vector.

%infread 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);%please

select 15 points for test.

出现GUI界面。

a4c26d1e5885305701be709a3d33442f.png

操作很简单,只需注意选点要均匀布开,以增加其代表性。选定完毕,File-> Save Points to

Workspace将数据保存到工作区中。Workspace立刻多出两个N×2的数组(其中N为选定的匹配点对数),分别为input_points和base_points,如:

input_points =

119.5185193.5926

168.9012242.9753

105.9383140.5062

459.0247131.8642

313.3457257.7901

292.3580165.1975

276.308633.0988

283.7160380.0123

76.3086297.2963

135.567983.7160

360.2593313.3457

94.8272446.6790

70.1358354.0864

181.2469361.4938

381.2469460.2593

252.8519433.0988

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 =

ndims_in:

2

ndims_out:

2

forward_fcn:

@fwd_affine

inverse_fcn:

@inv_affine

tdata:

[1x1 struct]

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)');

结果如下:

a4c26d1e5885305701be709a3d33442f.png

总结

1.image和imshow区别。前者视base,input此类二维图片矩阵为索引图像,在系统的index库中选取颜色。

2.选择适当的方法来建立转换参数,并非算法越复杂越好,应参考成像因素(退化因素)。

3.尚没有看出十字相关法的好处。

4.

利用cpselect选择匹配点,cpselect可以返回一个GUI句柄。欲实现如下功能:当打开cpselect GUI

时,m文件程序暂停运行,关闭之后继续执行。因为对GUI编程不懂, 使用了waitfor,pause函数都没法实现。尝试中……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值