Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Movement from 2D to 3D

Tags:

c#

2d

xna

3d

Can anyone give me some advice or suggestions

I need to find how much an object in a photograph has move from one position to another (well actually I need to calculate how much the camera has moved between 2 images but as the object will remain stationary and just be rotated on its Y-axis I think it will be easier to move the image). Pretty much the same as this example but not quite as complicated. enter image description here

So I take the first photo of a rubiks cube and select 4 points on the cube as per the example here enter image description here The image is a Texture2D and the blue circles represents the 4 points of the front face of the cube as selected by the user. These 4 points are stored in the list and the next image is loaded that looks like this enter image description here Again the user has to select the 4 points of the same face as previous (the white face). Then these 4 points are stored to a new list.

So now I have two lists and I need to calculate how much the "whole front face" has moved (rotate/scale/translate) from image 1 to image 2 as shown here enter image description here

But more importantly, I need to calculate this movement in 3D! So for the first image I assume that z-component = 0. For instance I assume the top left corner of image 1 = e.g. (10, 10, 0).

Is there a way that I can "assume" that if the face of image 2 has rotated/scaled/translated in a certain way that this can be moved in 3D space? So if the top left corner of image 2 is to the right of image 1 (starting image) top left corner, that the camera must have moved to the right. And the same would go for up or down of the points? As for rotate, could I maybe calculate the angles between image 1's points and the angles between image 2's points and somehow calculate how much the camera has rotated?

For my code I was thinking something like this?

// Image 1 coordinates for the front face 
// Assume z = 0 
cube1 = new List<Vector3>(); 
cube.Add(new Vector3(10, 10, 0)); 
cube.Add(new Vector3(20, 10, 0)); 
cube.Add(new Vector3(10, 20, 0)); 
cube.Add(new Vector3(20, 20, 0)); 

// Get image 2 coordinates 
cube2 = new List<Vector3>(); 
cube.Add(new Vector3(newX, newY, ?)); // Keep Z = 0?
cube.Add(new Vector3(newX, newY, ?)); 
cube.Add(new Vector3(newX, newY, ?)); 
cube.Add(new Vector3(newX, newY, ?)); 

For movement left or right just calculate how much each point has moved

//Translation 
Matrix translating = Matrix.CreateTranslation(new Vector3(amountMovedX, amountMovedY,   0)); 
List<Vector3> imageAfterTranslating = transformListOfVertices(imageAfterScaling, translating); 

And for skew (Im a bit stuck)....

// Rotation 
Matrix rotation = Matrix.CreateFromAxisAngle( 
Vector3.Normalize(new Vector3(?, ?, ?)), MathHelper.ToRadians(?));   // Not sure here 
List<Vector3> imageAfterRotation = transformListOfVertices(cube, rotation);
like image 904
heyred Avatar asked Apr 04 '12 12:04

heyred


People also ask

Can you make a 3D model from a 2D image?

2D images only have length and width components. There is no software yet that can take a single 2D image (for example, a family photo) and produce a robust 3D model. However, you can use a series of 2D images and make a 3D model through a process called photogrammetry.


1 Answers

world matrix * view matrix (of camera) * projection (of camera) = transform matrix

Assuming that cube1 = initial position.
What happens during render is: cube1 * transformMatrix = output matrix

If I understand you correct: your input allows the user to change camera matrix (view) only,
(and camera = view*projection) And you want to find out the new transformation of the cube after the camera updated.

So you need to do invert operation:

outputmatrix / transformMatrix = cube1 matrix (the new one you will need)

from the new cube1 result matrix you can extract the angles, movement and etc..

So, your problem is more related to "what" to implement - not how to implement.
(as I see you familir with the commands and code you need)

I hope this helps.

like image 54
G.Y Avatar answered Oct 04 '22 23:10

G.Y