图像配准MATLAB-数字图像处理作业
本文分别介绍自动和手动选取基准点的图像配准方法。
手动选取基准点对
读入图片
首先读入图片,由于两个图片都是彩色的,所以读入的是RGB像素。
a = imread('Image A.jpg');
figure(1);
imshow(a)
c = imread('Image B.jpg');
figure(2);
imshow(c)
选取基准点
使用cpselect命令就可以调用工具箱交互选择两幅图片上的配准点。cpselect函数前一个参数为需要配准的图像,后一个参数为基准图像。
cpselect(c,a);
通过该工具箱手动选取7组基准点。开始不知道这个函数用法,于是下图是把基准图像放到了第一个参数。
导出坐标
选择完毕后,在file菜单里的export points to workspace将选择的点的坐标导出。
导出坐标为
905.058394160584 1253.39172749392
1990.00000000000 2034
675 1252
1077 1044
1374 1077
2513 674.000000000000
2388 1713
B图坐标为
1195.87500000000 1694.87500000000
2443 2171
975.000000000000 1754
1307.00000000000 1447
1603 1405.00000000000
2598 726.000000000000
2745 1760
求变换矩阵
根据之前得到的基准点对坐标,利用cp2tform函数可以计算变换的参数。将基准点对作为输入传递给cp2tform,选择一种适当的变换类型,cp2tform函数就能确定该类型变换所需的参数,实际上相当于一种数据拟合。cp2tform函数寻找能够拟合控制点对的变换参数,返回一个TFORM的结构的几何变换结构,其中包括几何变换的类型和参数。
tform = cp2tform(fixedPoints,movingPoints,'affine');
第一个参数为变换前的图像基准点坐标,第二个为变换后的。第三个参数用法如表。
根据算出矩阵,复原图像查看结果
gp = imtransform(c,tform);
figure(1)
imshow(gp)
figure(2)
imshow(a)
figure(3)
imshow(c)
自动选取基准点
读入图片
先读入图片,并将RGB像素转化为灰度值。使用的是rgb2gray函数.
rgb2gray 通过计算 R、G 和 B 分量的加权和,将 RGB 值转换为灰度值:0.2989 * R + 0.5870 * G + 0.1140 * B
- 但是,这个方法对于Gamma校正的图片(平常所见到的24位真彩色图片均为Gamma校正的图片)并不适用,因为Gamma校正后的分量值不是物理上的功率,不能直接相加,因此,提出一种改进的算法来纠正这一问题。
注意这里的2.2次方和2.2次方根,RGB颜色值不能简单直接相加,而是必须用2.2次方换算成物理光功率。因为RGB值与功率并非简单的线性关系,而是幂函数关系,这个函数的指数称为Gamma值,一般为2.2,而这个换算过程,称为Gamma校正。
clear;
clc
a = imread('Image A.jpg');
c = imread('Image B.jpg');
a2 = rgb2gray(a);
c2 = rgb2gray(c);
转换后图像如下图:
检测特征点
clear;
clc
a = imread('Image A.jpg');
c = imread('Image B.jpg');
a2 = rgb2gray(a);
c2 = rgb2gray(c);
%%figure(1)
%%imshow(a2)
basePoints = detectSURFFeatures(a2);
movingPoints = detectSURFFeatures(c2);
figure(1);
imshow(a2);
hold on;
plot(selectStrongest(basePoints, 100));
figure(2);
imshow(c2);
hold on;
plot(selectStrongest(movingPoints, 100));
筛选出100个最好的特征点
basePoints = selectStrongest(basePoints, 100);
movingPoints = selectStrongest(movingPoints, 100);
提取描述子
baseFeatures = extractFeatures(a2,basePoints);
sceneFeatures = extractFeatures(c2,movingPoints);
匹配特征点
Pairs = matchFeatures(baseFeatures,sceneFeatures);
显示匹配结果
matchedbasePoints = basePoints(Pairs(:, 1), :);
matchedmovingPoints = movingPoints(Pairs(:, 2), :);
figure;
showMatchedFeatures(a2, c2, matchedbasePoints, ...
matchedmovingPoints, 'montage');
title('Matched Points (Including Outliers)');
参考链接: https://blog.youkuaiyun.com/qq_42463478/article/details/81132147