Excuse me everyone! I have point cloud as shown in the figure. I want to rotate to the horizontal plane. The rotation point is the middle. How can I do it ? Thank you very much.

 Réponse acceptée

Matt J
Matt J le 25 Déc 2021
Modifié(e) : Matt J le 26 Déc 2021
Since your point cloud is is not perfectly coplanar, you will need to fit a plane to it in order to determine the necessary rotation. You can do that with planarFit() from this downloadable package:
pfit=planarFit(plyRoi.Location.');
Having done this, you can also use some of the Hidden methods of the pfit object to do the rotation:
XYZ=plyRoi.Location;
x0=mean(XYZ);
R=pfit.vecrot(pfit.normal,[0,0,1]); %compute 3x3 rotation matrix
result=pointCloud((XYZ-x0)*R.'+x0); %rotated point cloud

14 commentaires

Lyhour Chhay
Lyhour Chhay le 25 Déc 2021
Thank you sir very much for your suggestion. I cannot run the code it is error as show in the figure. Noted the parameter 'vnorm' is change from 'vecnorm' (original coding) due to my version of matlab so I change it. the below figure is the error.
Matt J
Matt J le 25 Déc 2021
Modifié(e) : Matt J le 25 Déc 2021
Even if you have too early a Matlab version to support vecnorm, why modify conicFit.m? Why don't you just create a function named vecnorm, which it expects, instead of vnorm?
In any case, the error message is saying that the function vnorm() that you've provided does not have the same syntax as Matlab's built-in vecnorm command. In particular, vnorm doesn't accept 3 input arguments. You must address that.
Lyhour Chhay
Lyhour Chhay le 25 Déc 2021
Thank you sir. when I use the vecnorm​ it said that it undefine funtion or variable. Then I search about that I do the example from the Matlab documentation, I notice that vnorm give the same result as vecnorm. Therefore, what should I do sir if the vecnorm is not available.
Lyhour Chhay
Lyhour Chhay le 25 Déc 2021
This is the reason that I change to vnorm as show in the figure
Matt J
Matt J le 25 Déc 2021
Modifié(e) : Matt J le 25 Déc 2021
If your Matlab version is less than 2017b, provide the following function. Also, undo any changes you made to the conicFit.m or other files from the File Exchange.
function out=vecnorm(in,p,dim)
if nargin<2, p==2; end
sz=size(in);
if nargin<3,
dim=find(sz>1,1);
if isempty(dim), dim=1; end
end
out=sum(abs(in).^p,dim).^(1/p);
end
Lyhour Chhay
Lyhour Chhay le 26 Déc 2021
Dear Matt J,
I really appreciate your kindness and thank you very much for your guidance. please apology me for the late response due different time zone. Anyway, I put the function that you provide already. Now, the vecnorm has been solve due to my matlab version is 2017a. However, I get one more error related to maximu array size. How can I solve this problem sir. I will show you the error.
Image Analyst
Image Analyst le 26 Déc 2021
I might suggest taking a subset of your data, like a thousand points taken from random locations. You probably don't need all 11.8 million points to do the fit.
Lyhour Chhay
Lyhour Chhay le 26 Déc 2021
thank you very much sir for your suggestion. I will try it.
Matt J
Matt J le 26 Déc 2021
You're welcome, but if it does what you need, please do Accept-click the answer.
@Lyhour Chhay did taking a subset work? But I forgot to add that you should probably plot the points first to make sure you see the planar shape. When I plotted just the first 1000 points, it was in no way planar at all, as you can see from the plot in a comment under my answer. So you could use randperm() to get a list of random indexes from which to pull the training data.
numTrainingPoints = 1000; % Whatever.
% Get a list of indexes randomly chosen over the whole length.
randomIndexes = randperm(length(x), numTrainingPoints);
% Extract subset from random locations.
% Important: Use the same randomIndexes for each vector!
xt = x(randomIndexes);
yt = y(randomIndexes);
zt = z(randomIndexes);
plot3(xt, yt, zt, '.'); % Visualize to make sure it's planar.
grid on;
Thanks for Accepting the Answer to award Matt "reputation points". 🙂
Lyhour Chhay
Lyhour Chhay le 26 Déc 2021
Dear sir,
I appreciate your kindness. I am thinking about it as well but I am stuck. Thank you so much for your suggest me this coding. I will try it.
Image Analyst
Image Analyst le 26 Déc 2021
We thought you had it all working now since you accepted the answer. Since it's not working, just attach all your data and all your m-file(s) with the paperclip icon and Matt willl probably take a look at it when he returns.
Matt J
Matt J le 26 Déc 2021
Modifié(e) : Matt J le 26 Déc 2021
Here is the code I used, and the results I obtained,
XYZ=plyRoi.Location;
pfit=planarFit(XYZ');
x0=mean(XYZ);
R=pfit.vecrot(pfit.normal,[0,0,1]); %compute 3x3 rotation matrix
result=num2cell((XYZ-x0)*R'+x0,1); %rotated point cloud
[hF,h1]=plot(pfit); hold on
h2=scatter3(result{:},'filled','MarkerFaceColor','g'); hold off
axis(0.07*[-1,1,-1,1,-1,0])
view(-25,3)
legend([h1,hF,h2],'Old Cloud','Plane Fit','New Cloud',...
'location','north')
Lyhour Chhay
Lyhour Chhay le 27 Déc 2021
Dear Image Analyst and Matt J,
I really apprecicate your kindness and thank you very much. Please apology for my late response due to it is night time for me while you were commented. Really big thank to Matt j for your coding, now I can run it and solve it. the problem is about this statement:
  • pfit=planarFit(plyRoi.Location); --> this statement is request big size ram
  • XYZ=plyRoi.Location; pfit=planarFit(XYZ'); --> working !!!! I really suprise that it is worked.
Again, thank you all for helping me to solve this problem. Have nice day and wish you the best for coming new year.
Best Regards,
CHHAY LYHOUR

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 25 Déc 2021
You forgot to attach your data, which would have made it easier.
So I'd guess at something like
% Fit a line through the data.
coefficients = polyfit(x, y, 1);
% Then get the mean y
meany = mean(y);
% Get a fitted y
fittedy = polyval(coefficients, x);
% Then subtract the fitted values and add the vertical offset.
rotatedy = y - fittedy + meany;
plot(x, rotatedy, '.', 'MarkerSize', 10);

9 commentaires

Lyhour Chhay
Lyhour Chhay le 25 Déc 2021
Modifié(e) : Lyhour Chhay le 25 Déc 2021
thank you very much for your kindly response and your code, sir. please apology for my forget. My data is *.ply data contain x,y,z coordinate. I want to align horizontal plane. Then return the point cloud x,y,z corresponding to the rotation.
I'm not going to type all that in. Did you try my code? If not, why not?
You can attach the variable in a .mat file
save('answers.mat', 'plyRoi');
if you still need help.
Lyhour Chhay
Lyhour Chhay le 25 Déc 2021
I appreciate your kindness sir. I run your code already. However, it is not corresponding to my purpose. It plot only 2D condition. I will you the result of ploting. Due to my data is point cloud contatint the x,y,z is working for 3D condition. I attach the link for download my data here. Thank you very much
Image Analyst
Image Analyst le 25 Déc 2021
Modifié(e) : Image Analyst le 25 Déc 2021
In that data, which N-by-3 matrix is actually the xyz data? Location? Normal?
Lyhour Chhay
Lyhour Chhay le 25 Déc 2021
The data is in the Location sir.
Image Analyst
Image Analyst le 25 Déc 2021
Once all the data is plotted, I can see it's more like a plane. I think you're best off fitting it to a plane like Matt says, then find the normal to the fitted plane values and use that to rotate the point cloud to flatten/level it.
Lyhour Chhay
Lyhour Chhay le 25 Déc 2021
Thank you very much sir. I appreciate your kindness for show me the tips. I am doing the suggesiton from Matt J. However, I am not achieve it yet due to some error of coding. Again, thank you very much.
Image Analyst
Image Analyst le 25 Déc 2021
Well give him time - it is Christmas Day after all. 🎅
Lyhour Chhay
Lyhour Chhay le 26 Déc 2021
Dear Sir (Image Analyst)
Thank you very much sir for your suggestion and guidance. I am sorry for my late response due to different time zone. Merry Christmas wish you the best. Thank you sir.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by