数字图像处理MATLAB学习笔记(四)
Geometric Transformations and Image Processing
1 Transforming Points
点变换就是进行空间坐标系统的转换,即将一个在空间a的点映射到空间b中去。
我们可以将这种输入到输出的正向的空间映射用公式表达为: ( x , y ) = T { ( w , z } {(x,y)=T\{(w,z\}} (x,y)=T{(w,z}。如果可以反向映射的话,表达式满足 ( u , z ) = T − 1 { ( x , y ) } {(u,z)=T^{-1}\{(x,y)\}} (u,z)=T−1{(x,y)}。
图像的geometric transformations(几何变换)也就被定为geometric coordinate transformations(几何坐标变换)。我们令 f ( w , z ) {f(w,z)} f(w,z)表示输入空间的一幅图像, g ( x , y ) {g(x,y)} g(x,y)表示输出空间的一幅图像,那么根据上述映射关系,我么可以得到: g ( x , y ) = f ( T − 1 { ( x , y ) } ) {g(x,y)=f(T^{-1}\{(x,y)\})} g(x,y)=f(T−1{(x,y)})。
在MATLAB中,我们使用function maketform创建转换形式变量tform:
tform = maketform(transform_type, params, ...)
按照顺序来,先说一下custom:
tform = maketform('custom', ndims_in, ndims_out, forward_fcn, inv_function, tdata)
其中,tdata是关于forward_fcn和inv_function的系数。
Example Creating a custom tform structure and using it to transform points:
这里我们先设定一下输入与输出空间之间的映射关系方程为:
(
x
,
y
)
=
T
{
(
w
,
z
)
}
=
(
3
w
,
2
z
)
(
w
,
z
)
=
T
−
1
{
(
x
,
y
)
}
=
(
x
/
3
,
y
/
2
)
(x,y)=T\{(w,z)\}=(3w,2z)\\ (w,z)=T^{-1}\{(x,y)\}=(x/3,y/2)
(x,y)=T{(w,z)}=(3w,2z)(w,z)=T−1{(x,y)}=(x/3,y/2)
那么,我们可以根据上式编写代码如下:
>> forward_fcn = @(wz, tdata) [3*wz(:,1), 2*wz(:,2)];
>> inverse_fcn = @(xy, tdata) [xy(:,1)/3, xy(:,2)/2];
>> tform1 = maketform('custom', 2, 2, forward_fcn, inverse_fcn, [])
tform1 =
struct with fields:
ndims_in: 2
ndims_out: 2
forward_fcn: @(wz,tdata)[3*wz(:,1),2*wz(:,2)]
inverse_fcn: @(xy,tdata)[xy(:,1)/3,xy(:,2)/2]
tdata: []
这里我们做一下简单的测试,并使用工具箱中的function tformfwd和function tforminv进行正向和反向映射:
WZ = [1 1; 3 2];
>> XY = tformfwd(WZ, tform1)
XY =
3 2
9 4
>> WZ2 = tforminv(XY, tform1)
WZ2 =
1 1
3 2
我们再设定一个输入与输出空间之间的映射关系:
(
x
,
y
)
=
T
{
(
w
,
z
)
}
=
(
w
+
0.4
z
,
z
)
(
w
,
z
)
=
T
−
1
{
(
x
,
y
)
}
=
(
x
−
0.4
y
,
y
)
(x,y)=T\{(w,z)\}=(w+0.4z,z)\\ (w,z)=T^{-1}\{(x,y)\}=(x-0.4y,y)
(x,y)=T{(w,z)}=(w+0.4z,z)(w,z)=T−1{(x,y)}=(x−0.4y,y)
我们比着葫芦画瓢,代码很容易实现:
>> forward_fcn2 = @(wz, tdata) [wz(:,1)+wz(:,2)*0.4, wz(:,2)];
>> inverse_fcn2 = @(xy, tdata) [xy(:,1)-xy(:,2)*0.4, xy(:,2)];
>> tform2 = maketform('custom', 2, 2, forward_fcn2, inverse_fcn2, []);
>> WZ
WZ =
1 1
3 2
>> XY = tformfwd(WZ, tform2)
XY =
1.4000 1.0000
3.8000 2.0000
>> WZ2 = tforminv(XY, tform2)
WZ2 =
1.0000 1.0000
3.0000 2.0000
我们根据上述概念,自定义两个M-function pointgrid和vistform,以便于目测并检验给定的变换
function pointgrid:
function wz = pointgrid(corners)
%POINTGRID Points arranged on a grid.
% WZ = POINTGRI D (CORNERS) computes a set point of points on a
% grid containing 10 horizontal and vertical lines. Each line
% contains 50 points. CORNERS is a 2-by-2 matrix. The first
% row contains the horizontal and vertical coordinates of one
% corner of the grid. The second row contains the coordinates
% of the opposite corner. Each row of the P-by-2 output
% matrix, WZ, contains the coordinates of a point on the output
% grid.
% Create 10 horizontal lines containing 50 points each.
[w1, z1] = meshgrid(linspace(corners(1,1), corners(2,1), 46), ...
linspace(corners(1), corners(2), 10));
% Create 10 vertical lines containing 50 points each.
[w2, z2] = meshgrid(linspace(corners(1,1), corners(2,1), 10), ...
linspace(corners(1), corners(2), 46));
% Create a P-by-2 matrix containing all the input-space points.
wz = [w1(:), z1(:); w2(:), z2(:)];
function vistform:
function vistform(tform, wz)
%VISTFORM Visualization transformation effect on set of points.
% VISTFORM(TFORM, WZ) shows two plots. On the left are the
% points in each row of the P-by-2 matrix WZ. On the right are
% the spatially transformed points using TFORM.
% Transform the points to output space.
xy = tformfwd(tform, wz);
% Compute axes limits for both plots. Bump the limites outward slightly.
minlim = min([wz; xy], [], 1);
maxlim = max([wz; xy], [], 1);
bump = max((maxlim - minlim) * 0.05, 0.1);
limits = [minlim(1)-bump(1), maxlim(1)+bump(1), ...
minlim(2)-bump(2), maxlim(2)+bump(2)];
subplot(1,2,1)
grid_plot(wz, limits, 'w', 'z');
subplot(1,2,2)
grid_plot(xy, limits, 'x', 'y');
%------------------------------------------------------------
function grid_plot(ab, limits, a_label, b_label)
plot(ab(:,1), ab(:,2), '.', 'MarkerSize', 2)
axis equal, axis ij, axis(limits);
set(gca, 'XAxisLocation', 'top')
xlabel(a_label), ylabel(b_label)
我们测试一下:
>> vistform(tform1, pointgrid([0 0; 100 100]))
>> figure, vistform(tform2, pointgrid([0 0; 100 100]))
![image-20211121183001862](/Users/wanghe/Library/Application%20Support/typora-user-images/image-20211121183001862.png)
![image-20211121182924445](/Users/wanghe/Library/Application%20Support/typora-user-images/image-20211121182924445.png)
2 Affine Transformations仿射变换
这里我们把之前提到的映射方程给用矩阵进行表示:
[
x
,
y
]
=
[
w
,
z
]
[
a
11
a
12
a
21
a
22
]
+
[
b
1
,
b
2
]
为
了
方
便
处
理
b
,
所
以
空
间
坐
标
矩
阵
后
都
加
上
一
个
元
素
,
值
为
1
,
化
简
为
:
[
x
,
y
,
1
]
=
[
w
,
z
,
1
]
[
a
11
a
12
0
a
21
a
22
0
b
1
b
2
0
]
我
们
就
可
以
令
T
=
[
a
11
a
12
0
a
21
a
22
0
b
1
b
2
0
]
,
则
上
述
公
式
简
化
为
[
x
,
y
,
1
]
=
[
w
,
z
,
1
]
T
[x,y]=[w,z]\left[\begin{matrix}a_{11}&a_{12}\\a_{21}&a_{22}\end{matrix}\right]+[b_1,b_2]\\ 为了方便处理b,所以空间坐标矩阵后都加上一个元素,值为1,化简为:\\ [x,y,1]=[w,z,1]\left[\begin{matrix}a_{11}&a_{12}&0\\a_{21}&a_{22}&0\\b_1&b_2&0\end{matrix}\right]\\ 我们就可以令T=\left[\begin{matrix}a_{11}&a_{12}&0\\a_{21}&a_{22}&0\\b_1&b_2&0\end{matrix}\right],则上述公式简化为[x,y,1]=[w,z,1]T
[x,y]=[w,z][a11a21a12a22]+[b1,b2]为了方便处理b,所以空间坐标矩阵后都加上一个元素,值为1,化简为:[x,y,1]=[w,z,1]⎣⎡a11a21b1a12a22b2000⎦⎤我们就可以令T=⎣⎡a11a21b1a12a22b2000⎦⎤,则上述公式简化为[x,y,1]=[w,z,1]T
其中,
T
{T}
T矩阵也称为affine matrix仿射矩阵,这里也就是affine参数起作用了。
Example:
>> T = [1 0 0; 0.4 1 0; 0 0 1];
>> tform3 = maketform('affine', T);
>> WZ = [1 1; 3 2];
>> XY = tformfwd(WZ, tform3)
XY =
1.4000 1.0000
3.8000 2.0000
那么,affine matrix的一个特殊形式如下:
T
=
[
s
cos
θ
s
sin
θ
0
−
s
sin
θ
s
cos
θ
0
b
1
b
2
1
]
O
R
T
=
[
s
cos
θ
s
sin
θ
0
s
sin
θ
−
s
cos
θ
0
b
1
b
2
1
]
T=\left[\begin{matrix}s\cos{\theta}&s\sin{\theta}&0\\-s\sin{\theta}&s\cos{\theta}&0\\b_1&b_2&1\end{matrix}\right]\\ OR\\ T=\left[\begin{matrix}s\cos{\theta}&s\sin{\theta}&0\\s\sin{\theta}&-s\cos{\theta}&0\\b_1&b_2&1\end{matrix}\right]\\
T=⎣⎡scosθ−ssinθb1ssinθscosθb2001⎦⎤ORT=⎣⎡scosθssinθb1ssinθ−scosθb2001⎦⎤
这些,类似于旋转、平移和反射的,都属于affine transformations的子集,被称为similarity transformations。
我们简单罗列一下affine matrix对应的transformations:
Type | affine matrix | coordinate equations |
---|---|---|
Identity | [ 1 0 0 0 1 0 0 0 1 ] {\left[\begin{matrix}1&0&0\\0&1&0\\0&0&1\end{matrix}\right]\\} ⎣⎡100010001⎦⎤ | x = w y = z x=w\\y=z x=wy=z |
Scaling | [ s x 0 0 0 s y 0 0 0 1 ] {\left[\begin{matrix}s_x&0&0\\0&s_y&0\\0&0&1\end{matrix}\right]\\} ⎣⎡sx000sy0001⎦⎤ | x = s x w y = s y z x=s_xw\\y=s_yz x=sxwy=syz |
Rotation | [ s cos θ s sin θ 0 − s sin θ s cos θ 0 0 0 1 ] {\left[\begin{matrix}s\cos{\theta}&s\sin{\theta}&0\\-s\sin{\theta}&s\cos{\theta}&0\\0&0&1\end{matrix}\right]\\} ⎣⎡scosθ−ssinθ0ssinθscosθ0001⎦⎤ | x = w cos θ − z sin θ y = w sin θ + z cos θ x=w\cos{\theta}-z\sin{\theta}\\y=w\sin{\theta}+z\cos{\theta} x=wcosθ−zsinθy=wsinθ+zcosθ |
Shear(horizontal) | [ 1 0 0 α 1 0 0 0 1 ] {\left[\begin{matrix}1&0&0\\\alpha&1&0\\0&0&1\end{matrix}\right]\\} ⎣⎡1α0010001⎦⎤ | x = w + α z y = z x=w+\alpha z\\y=z x=w+αzy=z |
Shear(vertical) | [ 1 0 0 β 1 0 0 0 1 ] {\left[\begin{matrix}1&0&0\\\beta&1&0\\0&0&1\end{matrix}\right]\\} ⎣⎡1β0010001⎦⎤ | x = w y = β w + z x=w\\y=\beta w+z x=wy=βw+z |
Vertical reflection | [ 1 0 0 0 − 1 0 0 0 1 ] {\left[\begin{matrix}1&0&0\\0&-1&0\\0&0&1\end{matrix}\right]\\} ⎣⎡1000−10001⎦⎤ | x = w y = − z x=w\\y=-z x=wy=−z |
Translation | [ 1 0 0 0 − 1 0 δ x δ y 1 ] {\left[\begin{matrix}1&0&0\\0&-1&0\\\delta_x&\delta_y&1\end{matrix}\right]\\} ⎣⎡10δx0−1δy001⎦⎤ | x = w + δ x y = z + δ y x=w+\delta_x\\y=z+\delta_y x=w+δxy=z+δy |
3 Projection Transformations
投影变换属于几何变换的一种,这里与affine transfromations不同的是,辅助坐标不是constant。表达式如下:
[
x
′
,
y
′
,
h
]
=
[
w
,
z
,
1
]
[
a
11
a
12
a
13
a
21
a
22
a
23
b
1
b
2
1
]
C
o
n
d
i
t
i
o
n
s
:
a
13
≠
0
,
a
23
≠
0
,
x
=
x
′
/
h
,
y
=
y
′
/
h
[x',y',h]=[w,z,1]\left[\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\b_1&b_2&1\end{matrix}\right]\\ Conditions:a_{13}≠0 , a_{23}≠0\space , x=x'/h , y=y'/h
[x′,y′,h]=[w,z,1]⎣⎡a11a21b1a12a22b2a13a231⎦⎤Conditions:a13=0,a23=0 ,x=x′/h,y=y′/h
使用的参数时projective。Example:
>> T = [-2.7391 0.2928 -0.6376
0.7426 -0.7501 0.8088
2.8750 0.7500 1.0000];
>> tform = maketform('projective', T);
>> vistform(tform, pointgrid([0 0; 1 1]));
![image-20211122220939612](/Users/wanghe/Library/Application%20Support/typora-user-images/image-20211122220939612.png)
4 Applying Geometric Transformations to Images
逆映射使用function imtransform,语法如下:
g = imtransform(f, tform)
下面进行一些应用练习:
Example Geometric transformations of images:
affine transform遵循的scaling affine matrix为 T = [ s x 0 0 0 s y 0 0 0 1 ] {T=\left[\begin{matrix}s_x&0&0\\0&s_y&0\\0&0&1\end{matrix}\right]} T=⎣⎡sx000sy0001⎦⎤,rotation affine matrix为 T = [ s cos θ s sin θ 0 − s sin θ s cos θ 0 0 0 1 ] {T=\left[\begin{matrix}s\cos{\theta}&s\sin{\theta}&0\\-s\sin{\theta}&s\cos{\theta}&0\\0&0&1\end{matrix}\right]} T=⎣⎡scosθ−ssinθ0ssinθscosθ0001⎦⎤
>> f = checkerboard(50);
>> sx = 0.75;
>> sy = 1.25;
>> T = [sx 0 0; 0 sy 0; 0 0 1];
>> t1 = maketform('affine', T);
>> g1 = imtransform(f, t1);
>> theta = pi/6;
>> T2 = [cos(theta) sin(theta) 0; -sin(theta) cos(theta) 0; 0 0 1];
>> t2 = maketform('affine', T2);
>> g2 = imtransform(f, t2);
>> T3 = [0.5768 0.0782 -0.0009; 0.0127 0.5371 -0.0009; 0.5987 0.5761 1.0000];
>> tform3 = maketform('projective', T3);
>> g3 = imtransform(f, tform3);
>> subplot(221);imshow(f);title('original image');xlabel('(a)');
>> subplot(222);imshow(g1);title('scaling image');xlabel('(b)');
>> subplot(223);imshow(g2);title('rotation image');xlabel('(c)');
>> subplot(224);imshow(g3);title('projective image');xlabel('(d)');
![image-20211122223415659](/Users/wanghe/Library/Application%20Support/typora-user-images/image-20211122223415659.png)
5 Image Coordinate Systems in MATLAB
MATLAB的坐标系统:这里强调一下,MATLAB中的image coordinate的原点在左上角,为了方便查看像素点位置坐标,我们可以使用如下语句,使得下回每一次使用imshow显示图像时,图像上都可以显示坐标轴
>> iptsetpref imshowAxesVisible on
5.1 Output Image Location
我们可以通过imtransform获得图片反映射后的坐标信息:
[g, xdata, ydata] = imtransform(f, tform, 'FillValue', 255)
Note:imtransform中的FillerValue指的是对于任何输出图像像素的值,对应输入图像边界外的输入空间位置,默认情况下为0。
Example Displaying input and output images together in a common coordinate system:
>> theta = 3*pi/4;
>> T = [cos(theta) sin(theta) 0; -sin(theta) cos(theta) 0; 0 0 1];
>> tform = maketform('affine', T);
>> [g, xdata, ydata] = imtransform(f, tform, 'FillValue', 255);
>> imshow(f)
>> hold on
>> imshow(g, 'XData', xdata, 'YData', ydata)
>> axis auto
>> axis on
>> T = [1 0 0; 0 1 0; 1300 200 1];
>> tform = maketform('affine', T);
>> [g, xdata, ydata] = imtransform(f, tform, 'FillValue', 255);
>> imshow(f)
>> hold on
>> imshow(g, 'XData', xdata, 'YData', ydata)
>> axis on
>> axis auto
![image-20211123154305219](/Users/wanghe/Library/Application%20Support/typora-user-images/image-20211123154305219.png)
![image-20211123154434361](/Users/wanghe/Library/Application%20Support/typora-user-images/image-20211123154434361.png)
5.2 Controlling the Output Grid
imtransform在输出空间的定位与计算步骤:
- 决定输入image得边界矩形
- 在边界矩形上把点映射到输出空间
- 计算变换后输出空间中点的边界矩形
- 计算位于输出空间中、边界矩形内的网格上的输出图像像素
这里说一下imtransform2函数,实际上是对imtransform函数的一个封装,可以直接对输入与输出的图像进行比较。
function g = imtransform2(f, varargin)
%IMTRANSFORM2 2-D image transformation with fixed output location
% G = IMTRANSFORM2(F, TFORM, ...) applies a 2-D geometric
% transformation to an image. IMTRANSFORM2 fixes the output image
% location to cover the same region as the input image.
% IMTRANSFORM2 takes the same set of optional parameter/value
% pairs as IMTRANSFORM.
[M, N] = size(f);
xdata = [1 N];
ydata = [1 M];
g = imtransform(f, varargin{:}, 'XData', xdata, 'YData', ydata);
6 Image Interpolation 图像内插
内插:用离散数据构建连续定义的函数
通常有两个步骤:
- 连续数据离散化,即连续域到离散域的映射,通常要用到相应的内插核函数。常见的内插核有:
- 盒状核: h B ( x ) = { 1 − 0.5 ≤ x ≤ 0.5 0 otherwise {h_B(x)=\begin{cases}1&-0.5\le x\le 0.5 \\ 0 & \text{otherwise}\end{cases}} hB(x)={10−0.5≤x≤0.5otherwise
- 三角核: h T ( x ) = { 1 − ∣ x ∣ x ≤ 1 0 otherwise {h_T(x)=\begin{cases}1-|x|&x\le1\\0&\text{otherwise}\end{cases}} hT(x)={1−∣x∣0x≤1otherwise
- 立方核: h C ( x ) = { 1.5 ∣ x ∣ 3 − 2.5 ∣ x ∣ 2 + 1 ∣ x ∣ ≤ 1 − 0.5 ∣ x ∣ 3 + 2.5 ∣ x ∣ 2 − 4 ∣ x ∣ + 2 1 < ∣ x ∣ ≤ 2 0 otherwise {h_C(x)=\begin{cases}1.5{|x|}^3-2.5{|x|}^2+1&|x|\le1\\-0.5{|x|}^3+2.5{|x|}^2-4{|x|}+2&1\lt|x|\le2\\0&\text{otherwise}\end{cases}} hC(x)=⎩⎪⎨⎪⎧1.5∣x∣3−2.5∣x∣2+1−0.5∣x∣3+2.5∣x∣2−4∣x∣+20∣x∣≤11<∣x∣≤2otherwise
- 在离散位置估计函数值。
6.1 Interpolation in Two Dimensions
常用的二维内插方法是把二维内插问题化为多个一维内插任务。
例如:求 f ( x , y ) {f(x,y)} f(x,y)先行内插的 f ′ ( 2.6 , 1.4 ) {f'(2.6,1.4)} f′(2.6,1.4)
- 用 f ( 2 , 1 ) {f(2,1)} f(2,1)和 f ( 3 , 1 ) {f(3,1)} f(3,1)之间的线性内插决定 f ′ ( 2.6 , 1 ) {f'(2.6,1)} f′(2.6,1)
- 用 f ( 2 , 2 ) {f(2,2)} f(2,2)和 f ( 3 , 2 ) {f(3,2)} f(3,2)之间的线性内插决定 f ′ ( 2.6 , 2 ) {f'(2.6,2)} f′(2.6,2)
- 用 f ′ ( 2.6 , 1 ) {f'(2.6,1)} f′(2.6,1)和 f ′ ( 2.6 , 2 ) {f'(2.6,2)} f′(2.6,2)之间的线性内插决定 f ′ ( 2.6 , 1.4 ) {f'(2.6,1.4)} f′(2.6,1.4)
![image-20211123182915027](/Users/wanghe/Library/Application%20Support/typora-user-images/image-20211123182915027.png)
6.2 Comparing Interpolation Methods
我们自定义一个function reprotate,这个函数的作用是比较计算速度和图像质量(我们用timeit记录时间)
我们通过使用reprotate比较nearest、bilinear、bicubic三种插值方法的效果,图片进行十二次旋转输入。
function g = reprotate(f, interp_method)
%REPROTATE Rotate image repeatedly
% G = REPROTATE (F, INTERP METHOD) rotates the input image, F,
% twelve times in succession as a test of di fferent inte rpolation
% methods. INTERP METHOD can be one of the strings nearest' ,
% 'bilinear' , or 'bicubic'.
% Form a spatial transformation that rotates the image about its
% center point. The trans formation is formed as a composite of
% three affine trans formations :
%
% 1. Transform the center of the image to the origin.
center = fliplr(1 + size(f) / 2);
A1 = [1 0 0; 0 1 0; -center, 1];
% 2. Rotate 30 degrees about the origin
theta = 30*pi/180;
A2 = [cos(theta) -sin(theta) 0; sin(theta) cos(theta) 0; 0 0 1];
% 3. Transform from the origin back to the original center location.
A3 = [1 0 0; 0 1 0; center, 1];
% Compose the three transforms using matrix multiplication
A = A1 * A2 * A3;
tform = maketform('affine', A);
% Apply the rotation 12 times in sequence. Use imtransform2 so that
% each successive transformation is computed using the same location
% and size as the original image.
g = f;
for k = 1:12
g = imtransform2(g, tform, interp_method);
end
Example Comparing speed and image quality for several interpolation methods:
>> f = imread('Fig0517(a).tif');
>> timeit(@() reprotate(f, 'nearest'))
ans =
0.0585
>> timeit(@() reprotate(f, 'bilinear'))
ans =
0.0918
>> timeit(@() reprotate(f, 'bicubic'))
ans =
0.1676
我们可以发现,nearest最近邻内插值速度最快,其次是bilinear双线性插值,bicubic双三次内插最慢。效果图对比:
![image-20211123195654210](/Users/wanghe/Library/Application%20Support/typora-user-images/image-20211123195654210.png)
最终,我们可以发现,nearest呈锯齿状,bilinear的效果最差,图像中心较为模糊,bicubic虽然运行时间最慢,但是效果最好,具有更为平滑的边缘和更少的模糊。
7 Image Registration图像配准
7.1 The Registration Process
图像配准处理的基本操作主要如下:
- 检测特征
- 匹配相应的特征
- 推断/确定几何变换
- 通过几何变换用另一幅图像对准一幅图像
7.2 Manual Feature Selection and Matching Using cpselect
使用function cpselect手动筛选特征点进行匹配,使用规则如下:
>> cpselect(FIGURE1, FIGURE2)
7.3 Inferring Transformation Parameters Using cp2tform 使用cp2tform推断变换参数
cp2tform的语法为:
tform = cp2tform(input_points, base_points, transformtype)
其中,input_points和base_points是两个Px2大小的包含相应特征的矩阵。transformtype是所希望的变换类型。对应的tform类型和可以生成的maketform或cp2tform。其中,maketform用于直接指定变换参数,cp2tform用于相应的特征位置对估计变换参数。
Type of Transformation | Description | Functions |
---|---|---|
assine | 缩放、旋转、剪切和平移的结合;直线保持为直线,平行线保持平行 | maketform、cp2tform |
box | 独立地沿着每一维缩放和平移,仿射的子集 | maketform |
composite复合 | 几何变换的集合,可顺序地使用 | maketform |
custom定制 | 用户定义的几何变换,函数自定义 | maketform |
LWM | 局部加权平均,局部变化的几何变换 | cp2tform |
Nonreflective similarity | 缩放、旋转、剪切和平移的结合;直线保持为直线,平行线保持平行;保持物体的基本形状 | cp2tform |
Piecewise linear | 局部变换的几何变换,不同的仿射变换用于三角形区域 | cp2tform |
Polynomial | 使用2阶、3阶、4阶多项式形式的几何变换 | cp2tform |
Projective | 仿射变换的超集,和仿射变换一样;直线保持为直线,平行线汇聚于尽头 | maketform、cp2tform |
Similarity | 具有额外反射的可能性,类似于非反射相似 | cp2tform |
7.4 Visualing Aligend Image 观察对准图像
这里自定义一个用于对准图像的函数function visreg。
这个函数的主要功能是将两幅图像根据特征点位置进行覆盖重合,以此来展示对准后的图像。其中,在输入的图片中,一幅是参考图像fref,一般位于对准后的底层;f是输入图像,一般是fref中截取的一部分,用于重合到fref中去。
function h = visreg(fref, f, tform, layer, alpha)
%VISERG Visualize registered images
% VISREG (FREF, F, TFORM) displays two registered images together .
% FREF is the reference image. F is the input image, and TFORM
% defines the geometric transformation that aligns image F with
% image FREF .
%
% VISREG (FREF, F, TFORM, LAYER) displays F transparently over FREF
% if LAYER is 'top' ; otherwise it displays FREF transparently over
% F.
%
% VISREG(FREF, F, TFORM, LAYER, ALPHA) uses the scalar value
% ALPHA, which ranges between 0.0 and 1.0, to control the level of
% transparency of the top image. If ALPHA is 1.0, the top image
% is opaque. If ALPHA is 0.0, the top image is invisible .
%
% H = VISREG(...) returns a vector of handles to the two di splayed
% image objects. H is in the form [HBOTTOM, HTOP].
if nargin < 5
alpha = 0.5;
end
if nargin < 4
layer = 'top';
end
% Transform the input image, f, recording where the result lies in
% coordinate space
[g, g_xdata, g_ydata] = imtransform(f, tform);
[M, N] = size(fref);
fref_xdata = [1 N];
fref_ydata = [1 M];
if strcmp(layer, 'top')
% Display the transformed input image above the reference image
top_image = g;
top_xdata = g_xdata;
top_ydata = g_ydata;
% The transformed input image is likely to have regions of black
% pixels because they correspond to "out of bounds" locations on
% the original image. (See Example 5.2.) These pixe1s should be
% displayed completely transparently. To compute the appropriate
% transparency matrix, we can start with a matrix filled with the
% value ALPHA and then transform it with the same transformation
% applied to the input image. Any zeros in the result will cause
% the black "out of bounds" pixels in g to be displayed
% transparently.
top_alpha = imtransform(alpha * ones(size(f)), tform);
bottom_image = fref;
bottom_xdata = fref_xdata;
bottom_ydata = fref_ydata;
else
% Display the reference image above the transformed input image
top_image = fref;
top_xdata = fref_xdata;
top_ydata = fref_ydata;
top_alpha = alpha;
bottom_image = g;
bottom_xdata = g_xdata;
bottom_ydata = g_ydata;
end
% Display the bottom image at the correct location in coordinate space.
h_botton = imshow(bottom_image, 'XData', bottom_xdata, 'YData', bottom_ydata);
hold on
% Display the top image with the appropriate transparency
h_top = imshow(top_image, 'XData', top_xdata, 'YData', top_ydata);
set(h_top, 'AlphaData', top_alpha);
% The first call to imshow above has the effect of fixing the axis limits.
% Use the axis command to let the axis limits be chosen automatically to
% fully encompass both images.
axis auto
if nargout > 0
h = [h_botton, h_top];
end
7.5 Area-Based Registration
对于外在特征选择和匹配的可供选择的方式基于区域的配准。在这个过程中,我们是将模版图像的第一幅图像移动到覆盖第二幅图像的每个位置。在每个位置中,计算相似性(similarity metric)。
对于Area-Based Registration,我们需要对similarity metric归一化处理为correlation coefficient相关系数。那么input image于template image之间的归一化互相关定义为:
γ
(
x
,
y
)
=
∑
s
,
t
[
w
(
s
,
t
)
−
w
ˉ
]
[
f
(
x
+
s
,
y
+
t
)
−
f
x
y
ˉ
]
∑
s
,
t
[
w
(
s
,
t
)
−
w
ˉ
]
2
[
f
(
x
+
s
,
y
+
t
)
−
f
x
y
ˉ
]
2
\gamma(x,y)=\frac{\sum_{s,t}[w(s,t)-\bar{w}][f(x+s,y+t)-\bar{f_{xy}}]}{\sqrt{\sum_{s,t}[w(s,t)-\bar{w}]^2[f(x+s,y+t)-\bar{f_{xy}}]^2}}
γ(x,y)=∑s,t[w(s,t)−wˉ]2[f(x+s,y+t)−fxyˉ]2∑s,t[w(s,t)−wˉ][f(x+s,y+t)−fxyˉ]
其中,
w
{w}
w是模版,
w
ˉ
{\bar{w}}
wˉ是模版元素的均值,只计算一次。
f
{f}
f是图像,
f
x
y
ˉ
{\bar{f_{xy}}}
fxyˉ是
w
{w}
w和
f
{f}
f覆盖区域图像的均值。求和是对图像和模版覆盖的
s
{s}
s和
t
{t}
t进行。
在MATLAB中,使用function normxcorr2实现:
g = normxcorr2(template, f)
Example Using function normxcorr2 to locate a template in an image:
>> onion = rgb2gray(imread('onion.png'));
>> peppers = rgb2gray(imread('peppers.png'));
>> imshowpair(peppers,onion,'montage')
>> c = normxcorr2(onion,peppers);
>> figure, surf(c), shading flat
>> [ypeak, xpeak] = find(c==max(c(:)));
>> % Compute translation from max location in correlation matrix
>> yoffSet = ypeak-size(onion,1);
>> xoffSet = xpeak-size(onion,2);
>> figure
>> hAx = axes;
>> imshow(peppers,'Parent', hAx);
>> drawrectangle(hAx, 'Position', [xoffSet+1, yoffSet+1, size(onion,2), size(onion,1)]);
![image-20211124224542688](/Users/wanghe/Library/Application%20Support/typora-user-images/image-20211124224542688.png)
Example Using normxcorr2 to register two images differing by a translation:
>> g1 = normxcorr2(w, f1);
>> g2 = normxcorr2(w, f2);
>> [y1, x1] = find(g1 == max(g1(:)));
>> [y2, x2] = find(g2 == max(g2(:)));
>> delta_x = x1 - x2;
>> delta_y = y1 - y2;
>> tform = maketform('affine', [1 0 0; 0 1 0; delta_x delta_y 1]);
>> visreg(f1, f2, tform)
至此,我们上面讲到的***7.2***和**7.5都属于手工处理的图像配准方法