How to calculate the rotation of this egg?

조회 수: 2 (최근 30일)
Ashfaq Ahmed
Ashfaq Ahmed 2022년 8월 9일
댓글: Matt J 2022년 8월 9일
Hi all!
Suppose I have this egg in figure1, standing still. But when it rotates a bit in figure 2, how can I calculate the rotation angle using MATLAB coding? Can anyone please give an idea on it?
I have attached the images in the question. (egg1, and egg2). Any suggestion from you will be much appreciated ^_^

채택된 답변

John D'Errico
John D'Errico 2022년 8월 9일
  1. Find a bounding box, where the box can be rotated, not a box that is aligned with the axes.
  2. Find the orientation (angle of inclination) of the long sides of the bounding box. atan2 will help you there.
  3. Nothing more needed, since atan2 did all the work.
  댓글 수: 2
Ashfaq Ahmed
Ashfaq Ahmed 2022년 8월 9일
Hi! Thank you for your reply. How can I find a bounding box? I don't understand it when you say, "where the box can be rotated, not a box that is aligned with the axes". Can you please explain?
John D'Errico
John D'Errico 2022년 8월 9일
편집: John D'Errico 2022년 8월 9일
I'm sure the Image processing toolbox has a bounding box utility. (I'll find it in a minute or so, and post the link too.) But you can also extract the points as a set of pixel locations, and then use my minboundrect utility.
Ok. A quick search tells me the tool is regionprops. It looks like you need to do something like:
regionprops(egg2,'boundingbox')
Lacking the IPT, I've never used it, but one of those tools will get you there.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt J
Matt J 2022년 8월 9일
편집: Matt J 2022년 8월 9일
First, crop the black corders from your image fiels. Then, use ellipticalFit() from,
getEgg=@(file) imbinarize(im2gray(imread(file)));
[Y1,X1]=find(edge(getEgg('egg1.png')));
[Y2,X2]=find(edge(getEgg('egg2.png')));
fobj1=ellipticalFit([X1(:),Y1(:)]');
fobj2=ellipticalFit([X2(:),Y2(:)]');
rotation = fobj2.angle-fobj1.angle
rotation =
137.9371
function out=getEgg(file)
out=imbinarize(im2gray(imread(file)));
end
  댓글 수: 3
Matt J
Matt J 2022년 8월 9일
편집: Matt J 2022년 8월 9일
Yes, it can. But be sure to clean the boundaries of the image box.
Ashfaq Ahmed
Ashfaq Ahmed 2022년 8월 9일
This code works perfectly! Thank you so much!!

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2022년 8월 9일
BW1=~getEgg('egg1.png');
BW2=~getEgg('egg2.png');
S1=regionprops(BW1,'Orientation');
S2=regionprops(BW2,'Orientation');
rotation = S2.Orientation-S1.Orientation
rotation = -137.9439
function out=getEgg(file)
out=imbinarize(im2gray(imread(file)));
end
  댓글 수: 2
Ashfaq Ahmed
Ashfaq Ahmed 2022년 8월 9일
Your previous code gives the perfect angle! But this code (where you are using regionprops), is not giving the proper angle.
Matt J
Matt J 2022년 8월 9일
The difference is 0.007 degrees. I'm surprised that it would be considered significant.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by