點雲粗配準:主成分分析法PCA(matlab)

QQ羣招募中646258285(招募中,沒幾個人說話),
需要交流的朋友可以直接加我微信( DntBeliv )或QQ( 1121864253 )


在這裏插入圖片描述
在這裏插入圖片描述

function plot_3d( data_source, color )
    x1=data_source(:,1);
    y1=data_source(:,2);
    z1=data_source(:,3);
    scatter3(x1,y1,z1,color);
    xlabel('x軸');
    ylabel('y軸');
    zlabel('z軸');
end
%% 對關鍵點進行粗配準
fprintf('\n粗配準--------------------------------------------------\n')
[R1,T1] = pca_function(data_source',data_target');
figure;hold on;
plot_3d( (R1*data_source'+repmat(T1,1,size(data_source',2)))', 'b');
plot_3d( data_target, 'r');
title('粗配準點雲對比圖');xlabel('x軸');ylabel('y軸');zlabel('z軸');
hold off;
function [R,T] = pca_function(P,Q)
	point1=size(P,2);
	point2=size(Q,2);
	
	pc = mean(P,2);
	qc = mean(Q,2); 
	
	x1 = P - repmat(pc,1,point1); 
	Mx =x1 * x1';
	y1 = Q - repmat(qc,1,point2);
	My = y1 * y1';
	
	[Vx,Dx] = eig(Mx,'nobalance'); %Vx特徵向量,Dx特徵值
	[Vy,Dy] = eig(My,'nobalance');
	
	[~,index]=max(sum(x1.*x1));
	xm=x1(:,index);
	xm(3,1)=-abs(xm(3,1));
	p3 = Vx(:,3);
	if dot(xm,p3)<0
	    p3=-p3;
	end
	p2 = Vx(:,2);
	if dot(xm,p2)<0
	    p2=-p2;
	end
	p1=cross(p3,p2);
	
	[~,index]=max(sum(y1.*y1));
	ym=y1(:,index);
	ym(3,1)=-abs(ym(3,1));
	q3 = Vy(:,3);
	if dot(ym,q3)<0
	    q3=-q3;
	end
	q2 = Vy(:,2);
	if dot(ym,q2)<0
	    q2=-q2;
	end
	q1=cross(q3,q2);
	
	R = [q1,q2,q3]/[p1,p2,p3];
	xc2 = R*pc;
	T = (qc - xc2);
end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章