Matlab 用3D-RANSAC估计三维仿射变换

Matlab 用3D-RANSAC估计三维仿射变换,matlab,image-processing,affinetransform,ransac,image-registration,Matlab,Image Processing,Affinetransform,Ransac,Image Registration,我正在尝试注册两个体积测量图像(img1和img2)。img1的大小为40x40x24。img2的大小为64 x64x11。 到目前为止,我已经提取了它们的特征(vol1和vol2,与图像大小相同),然后匹配它们 现在,我在两个特征体中有一组对应的点,它们成对存储,这是一个大小为100x6的矩阵(每一行对是[x,y,z,x,y,z],其中(x,y,z)是vol1和[x,y,z]中体素的坐标是vol2中相应体素的坐标 现在,我正在尝试使用RANSAC算法来估计3D仿射变换T。我已经编写了下面的代码

我正在尝试注册两个体积测量图像(
img1
img2
)。
img1
的大小为
40x40x24
img2
的大小为
64 x64x11
。 到目前为止,我已经提取了它们的特征(
vol1
vol2
,与图像大小相同),然后匹配它们

现在,我在两个特征体中有一组对应的点,它们成对存储,这是一个大小为100x6的矩阵(每一行对是
[x,y,z,x,y,z]
,其中
(x,y,z)
vol1
[x,y,z]中体素的坐标
vol2
中相应体素的坐标

现在,我正在尝试使用RANSAC算法来估计3D仿射变换T。我已经编写了下面的代码,但我认为它有一个问题,因为当我得到输出变换T并将其乘以vol1中的样本体素坐标时,我得到了一些负坐标

下面是我对3D RANSAC算法的实现。我在中使用了2D RANSAC实现。如果你发现任何问题,请告诉我

function [bp] = ransac(data,bpI,iter,num,distThresh) 
 % data: a nx6 dataset with #n data points
 % num: the minimum number of points. Here num=4. 
 % iter: the number of iterations
 % distThresh: the threshold of the distances between points and the fitting line
 % inlierRatio: the threshold of the number of inliers 
 % bpI : Initialized affine transform model

  number = size(data,1); % Total number of points
 bestInNum = 0; % Best fitting line with largest number of inliers

 % Initial parameters for best model (affine transform)
 % Affine transform : T = [bp1, bp2, bp3, bp4; bp5, bp6, bp7, bp8; bp9, bp10, bp11, bp12;] 
 bp1 = bpI(1,1); bp2 = bpI(1,2); bp3 = bpI(1,3); bp4 = bpI(1,4); 
 bp5 = bpI(1,5); bp6 = bpI(1,6); bp7 = bpI(1,7); bp8 = bpI(1,8);
 bp9 = bpI(1,9); bp10 = bpI(1,10); bp11 = bpI(1,11); bp12 = bpI(1,12);

 for i=1:iter
 % Randomly select 4 points
     idx = randperm(number,num); sample = data(idx,:); 

     % Creating others which is the data that does not contain data in sample
     idxs =  sort(idx, 'descend'); % Sorting idx   
     others = data;
     for n = 1:num
         others(idxs(1,n), :) = []; 
     end

     x1 = sample(1,1); y1 = sample(1,2); z1 = sample(1,3);
     x2 = sample(2,1); y2 = sample(2,2); z2 = sample(2,3);
     x3 = sample(3,1); y3 = sample(3,2); z3 = sample(3,3);
     x4 = sample(4,1); y4 = sample(4,2); z4 = sample(4,3);

     X1 = sample(1,4); Y1 = sample(1,5); Z1 = sample(1,6);
     X2 = sample(2,4); Y2 = sample(2,5); Z2 = sample(2,6);
     X3 = sample(3,4); Y3 = sample(3,5); Z3 = sample(3,6);
     X4 = sample(4,4); Y4 = sample(4,5); Z4 = sample(4,6);

     B = [X1; Y1; Z1; X2; Y2; Z2; X3; Y3; Z3; X4; Y4; Z4];

     A = [
         x1, y1, z1, 1, 0 , 0 , 0, 0, 0, 0, 0, 0;
         0 , 0 , 0, 0, x1, y1, z1, 1, 0, 0 ,0, 0;
         0 , 0 , 0, 0, 0 , 0 , 0, 0, x1, y1, z1, 1;
         x2, y2, z1, 1, 0 , 0 , 0, 0, 0, 0, 0, 0;
         0 , 0 , 0, 0, x2, y2, z2, 1, 0 , 0 ,0, 0;
         0 , 0 , 0, 0, 0 , 0 , 0, 0, x2, y2, z2, 1;
         x3, y3, z3, 1, 0 , 0 , 0, 0, 0, 0, 0, 0;
         0 , 0 , 0, 0, x3, y3, z3, 1, 0 , 0 ,0, 0;
         0 , 0 , 0, 0, 0 , 0 , 0, 0, x3, y3, z3, 1;
         x4, y4, z4, 1, 0 , 0 , 0, 0, 0, 0, 0, 0;
         0 , 0 , 0, 0, x4, y4, z4, 1, 0 , 0 ,0, 0;
         0 , 0 , 0, 0, 0 , 0 , 0, 0, x4, y4, z4, 1
         ];

     cbp =  A\B; % calculating best parameters of the model (affine transform)
     T = [reshape(cbp',[4,3])'; 0, 0, 0, 1]; % Current affine transform matrix

     % Computing other points in the dataset that their distance from the
     % calculated model is less than the threshold.
     numOthers = size(others,1);
     inliers = [];
     for j = 1:numOthers
         % b = T a     
         d = others(j,:); % Explanation: d = [ax, ay, az, bx, by, bz]
         a = [d(1,1:3), 1]'; % Explanation a = [ax, ay, az]'
         b = [d(1,4:6),1]'; % b = [bx, by, bz]'
         cb = T*a; % Calculated b
         dist = sum((cb-b).^2);
         if(dist<=distThresh)
                  inliers = [inliers; d];        
         end
     end

     numinliers = size(inliers,1);
     % Update the number of inliers and fitting model if better model is found     
     if (numinliers >=  bestInNum)
         % Better model is estimated
          bestInNum = numinliers;
          bp1 = cbp(1,1); bp2 = cbp(2,1); bp3 = cbp(3,1); bp4 = cbp(4,1);
          bp5 = cbp(5,1); bp6 = cbp(6,1); bp7 = cbp(7,1); bp8 = cbp(8,1);
          bp9 = cbp(9,1); bp10 = cbp(10,1); bp11 = cbp(11,1); bp12 = cbp(12,1); 
          bp = [bp1, bp2, bp3, bp4, bp5, bp6, bp7, bp8, bp9, bp10, bp11, bp12];
     end
 end

 bp
end
function[bp]=ransac(数据、bpI、iter、num、distThresh)
%数据:具有#n个数据点的nx6数据集
%num:最小点数。这里num=4。
%iter:迭代次数
%distThresh:点与拟合线之间距离的阈值
%inlierRatio:入口数的阈值
%bpI:初始化仿射变换模型
数字=大小(数据,1);%总分
bestInNum=0;%具有最大入口数的最佳拟合线
%最佳模型的初始参数(仿射变换)
%仿射变换:T=[bp1,bp2,bp3,bp4;bp5,bp6,bp7,bp8;bp9,bp10,bp11,bp12;]
bp1=bpI(1,1);bp2=bpI(1,2);bp3=bpI(1,3);bp4=bpI(1,4);
bp5=bpI(1,5);bp6=bpI(1,6);bp7=bpI(1,7);bp8=bpI(1,8);
bp9=bpI(1,9);bp10=bpI(1,10);bp11=bpI(1,11);bp12=bpI(1,12);
对于i=1:iter
%随机选择4个点
idx=randperm(数字,num);样本=数据(idx,:);
%创建样本中不包含数据的其他数据
idxs=排序(idx,'下降');%排序idx
其他=数据;
对于n=1:num
其他(idxs(1,n),:)=[];
结束
x1=样品(1,1);y1=样品(1,2);z1=样品(1,3);
x2=样品(2,1);y2=样品(2,2);z2=样品(2,3);
x3=样品(3,1);y3=样品(3,2);z3=样品(3,3);
x4=样品(4,1);y4=样品(4,2);z4=样品(4,3);
X1=样品(1,4);Y1=样品(1,5);Z1=样品(1,6);
X2=样品(2,4);Y2=样品(2,5);Z2=样品(2,6);
X3=样品(3,4);Y3=样品(3,5);Z3=样品(3,6);
X4=样品(4,4);Y4=样品(4,5);Z4=样品(4,6);
B=[X1;Y1;Z1;X2;Y2;Z2;X3;Y3;Z3;X4;Y4;Z4];
A=[
x1,y1,z1,1,0,0,0,0,0,0,0,0,0;
0,0,0,0,x1,y1,z1,1,0,0,0,0;
0,0,0,0,0,0,0,0,0,x1,y1,z1,1;
x2,y2,z1,1,0,0,0,0,0,0,0,0;
0,0,0,0,x2,y2,z2,1,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,x2,y2,z2,1;
x3,y3,z3,1,0,0,0,0,0,0,0,0,0;
0,0,0,0,x3,y3,z3,1,0,0,0,0;
0,0,0,0,0,0,0,0,0,x3,y3,z3,1;
x4,y4,z4,1,0,0,0,0,0,0,0,0,0;
0,0,0,0,x4,y4,z4,1,0,0,0,0;
0,0,0,0,0,0,0,0,0,x4,y4,z4,1
];
cbp=A\B;%计算模型的最佳参数(仿射变换)
T=[重塑(cbp',[4,3]);0,0,0,1];%当前仿射变换矩阵
%计算数据集中的其他点,这些点与
%计算的模型小于阈值。
numOthers=尺寸(其他,1);
内插层=[];
对于j=1:n
%b=TA
d=其他(j:);%说明:d=[ax,ay,az,bx,by,bz]
a=[d(1,1:3),1]';%解释a=[ax,ay,az]'
b=[d(1,4:6),1]';%b=[bx,by,bz]'
cb=T*a;%计算b
dist=总和((cb-b)。^2);
if(dist=bestInNum)
%估计了较好的模型
贝斯汀努姆=努米利尔;
bp1=cbp(1,1);bp2=cbp(2,1);bp3=cbp(3,1);bp4=cbp(4,1);
bp5=美国海关与边境保护局(5,1);bp6=美国海关与边境保护局(6,1);bp7=美国海关与边境保护局(7,1);bp8=美国海关与边境保护局(8,1);
bp9=cbp(9,1);bp10=美国海关与边境保护局(10,1);bp11=美国海关与边境保护局(11,1);bp12=美国海关与边境保护局(12,1);
bp=[bp1,bp2,bp3,bp4,bp5,bp6,bp7,bp8,bp9,bp10,bp11,bp12];
结束
结束
英国石油公司
结束

我建议提供一个小样本案例(甚至随机数据)进行测试。对于刚性转换估计,我建议提供一个小样本案例(甚至随机数据)进行测试。对于刚性转换估计,我建议提供一个小样本案例(甚至随机数据)。