[MATLAB] [Function introduction] The cp2tform function infers spatial transformation from control point pairs

Official link: https://ww2.mathworks.cn/help/images/ref/cp2tform.html?s_tid=doc_ta .

cp2tformThe function is to infer the spatial transformation parameters based on the coordinates of the incoming control point pair.

The function syntax is as follows:

 从控制点对推断空间变换,并将此变换作为tform结构返回,有些转换类型具有可选的附加参数transformationType,比如下面的语法所示
tform = cp2tform(movingPoints, fixedPoints, transformationType)
 指定要使用的多项式的顺序进行空间变换
tform = cp2tform(movingPoints, fixedPoints, 'polynomial', degree)
 通过使用相邻控制点在每个控制点推断多项式来创建映射。任何位置的映射都取决于这些多项式的加权平均值,可以选择用于推断每个多项式的点数n,n个最接近的点用于推断每个控制点对的2阶多项式
tform = cp2tform(movingPoints, fixedPoints, 'lwm', n));
 创建固定控制点的Delaunay三角测量,并将相应的移动控制点映射到固定控制点。映射对于每个三角形是线性当设的,并且在控制点上是连续的,但由于每个三角形都有自己的映射,所以不连续可微
tform = cp2tform(movingPoints, fixedPoints, 'piecewise linear')
 在usedMP和usedFP中返回用于分段线性变换的控制点,还在badMP和badFP中返回被相处的控制点,因为它们是退化折叠三角形的重点顶点
[tform, usedMP, usedFP, badMP, badFP] = cp2tform(movingPoints, fixedPoints, 'piecewise linear')
 使用cpstruct结构来存储运动图像和固定运动图像的控制点坐标
tform = cp2tform(cpstruct, transformationType,____)
 在usedMP和usedFP中返回用于转换的控制点,不使用不匹配点和预测点。
[tform, usedMP, usedFP] = cp2tform(cpstruct, transformationType, ____)

Parameter Description:

Input parameters:

  • movingPoints: Control points in the image to be registered or moved, specified as m × 2 m \times 2m×2 matrix, each row specifies[x y]the coordinates of a control point, and the data type is double. Example:[11 11; 41 71].
  • fixedPoints: Control point in the datum or fixed image, specified as m × 2 m \times 2m×2 matrix, each row specifies[x y]the coordinates of a control point, and the data type is double. Example:[14 44; 70 81].
  • tranformationType:Conversion type, specified as one of the following types, listed in order of increasing complexity. The cp2tform function requires a minimum number of control point pairs to infer the structure of each transformation type.

Table 1 - Conversion types and their descriptions

conversion type describe Minimum number of control point pairs
nonreflective similarity Non-reflective similarity, Use this transformation when the shape in a moving image is unchanged, but the image is distorted by some combination of translation, rotation, and scaling. Straight lines stay straight and parallel lines stay parallel 2
similarity Same as non-reflective with optional reflection added 3
affine Use this transform when shapes in the input image exhibit erroneous effects. After transformation, straight lines are still straight lines and parallel lines are still parallel lines, but the rectangle becomes a parallelogram. 3
projective Use this transformation when the scene appears tilted. After the transformation, the straight lines are still straight lines, but the parallel lines are no longer parallel. 4
polynomial This transformation is used when objects in the image are curved. The higher the order of the polynomial selected in the fitting, the better the fitting effect, but the registered image contains more curves than the reference image and also requires more reference point pairs. 6-Second level
10-Third level
15-Fourth level
piecewise linear This transformation is used when the image exhibits segmental deformation. 4
lwm Consider using this transformation when the deformation in the image has localized characteristics and the piecewise linear conditions are not sufficient. 6. Recommendation 12
  • cpstruct: cosine selected control point, specified as a structure, cpstruct contains information about the x and y whitening of all control points in the moving image and the fixed image, including unpaired and predicted control points. cpstruct2pairsThe function eliminates mismatched and predicted control points and returns a valid set of control point pairs. cpstruct is the structure generated by the Control Point Selection tool when the Move Points to Workspace option is selected.
  • degree: The degree of polynomial transformation, specified as an integer 2, 3, or 4, which can be a numerical type supported by matlab.
  • n: Number of points used in the local weighted average calculation, specified as a positive integer, n can be as small as 6, but making n small risks generating ill-conditioned polynomials.

Output parameters:

  • tform:TFORM structure, returned as TFORM structure;
  • usedMP: moving control points used to infer spatial transformation, in n × 2 n \times 2n×2 is returned in the form of a matrix, without using unmatched points and predicted points;
  • usedFP: Fixed control points used to infer spatial transformations, in n × 2 n \times 2n×2 is returned in matrix form, without using unmatched points and predicted points;
  • badMP: Moving control points that were eliminated because they were determined to be outliers, in p × 2 p \times 2p×2 is returned in matrix form;
  • badFP: Fixed control points are eliminated because they are determined to be outliers and are represented as p × 2 p \times 2p×2 is returned in matrix form;

Usage example

Create reflectionless similarity transformation structures using control points

%% cp2tform 函数获取图像转换结构
clc; close all; format compact;
I = checkerboard;  % 加载棋盘格图片
J = imrotate(I, 30);  % 逆时针旋转30°
figure; 
subplot(1,2,1); imshow(I, []); title('原始图片');
subplot(1,2,2); imshow(J, []); title('逆时针旋转30°');
fixedPoints = [11 11; 41 71];  % 固定控制点对
movingPoints  = [14 44; 70 81];  % 移动控制点对

cpselect(J, I, movingPoints, fixedPoints);
% 获取非反射相似性变换,使用cp2tform函数返回变换,
% 并将tform的角度和和比例与原始变换的角度和规模进行比较
t = cp2tform(movingPoints, fixedPoints, 'nonreflective similarity');
% 使用一组有效的移动和固定控制点对启动控制点选择工具,可以以交互式添加控制点

% 通过检查平行于x轴的单位向量的旋转和拉伸方式,恢复角度和比例
u = [0 1];
v = [0 0];
[x y] = tformfwd(t, u, v); % 将t中的空间变换作用于u和v,将结果返回给x和y
dx = x(2) - x(1);
dy = y(2) - y(1);
angle = (180 / pi) * atan2(dy, dx)
scale = 1 / sqrt(dx ^2 + dy^2)

Show results:

angle =
   29.9816
scale =
    1.0006

Insert image description here

Guess you like

Origin blog.csdn.net/sinat_41752325/article/details/133013368