'Converting camera screen to pixel coordinates?

I have been trying to convert my 2D camera coordinates into pixel coordinates but my problem is that I know where the points should lie on the image and my calculated point is nowhere close to it.

What I've done: Used rotation and translation matrices to calculate world-to-screen 2D points. The "world (0,0,0)" maps to the camera (0,0) on the top left side of the screen.

  • x and y have been projected by dividing by their respective z-coordinate, and multiplied by focal length.

What I need to do: When the image is superimposed, the actual world (0,0,0) and the calculated world (0,0) are not on the same position. The camera screen itself is infinite, I have not really defined a screen. The image size is 640 x 480.

What I've tried:

xnew = xnew * 640
ynew = (1 - ynew) * 480

But this does not work either. My resource: https://www.scratchapixel.com/lessons/3d-basic-rendering/computing-pixel-coordinates-of-3d-point/mathematics-computing-2d-coordinates-of-3d-points

Even if I set the camera screen to be 70 by 70, and do the following:

xnew = (xnew + 35)/70
ynew = (ynew + 35)/70
xnew = xnew * 640
ynew = (1 - ynew) * 480

This still does not work.

Could someone please help me with this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source