# Image Processing - Getting an Angle

I’ve been scouring the forums all season for all image processing questions and answers and I appreciate everyone who has contributed to any of those forums. That being said, I now have a question of my own: is it possible to calculate an angle (camera to target) from the default available fields to Java users using the ParticleAnalysisReport?

I have reviewed the various simpler calculations to get an angle but they all require information that I don’t think I am able to get through the currently wrapped NIVision methods. We were planning to compare the left and right side heights of the quadrilateral but I can’t find an easy way to do this through the current Particle Report. I can’t come up with any clever ways to manipulate the bounding rectangle information to come up with an angle to the target.

Any help would be appreciated! Thank you in advance.

This problem is actually one I tackled fairly early on in the build season - but its solution isn’t exactly obvious.

ParticleAnalysisReport literally gives you everything you need in order to determine a vision target’s horizontal angle, vertical angle, and even distance away; the only difficulty comes from finding the “calibration values” required to calculate each of these from what is given.

For the horizontal angle, you need one piece of data not provided in the particle report: the horizontal field of view of the camera. Every camera has a certain angle range at which it can see, and you can think of the image you receive as what you see when you look through a piece of paper rolled into a conic shape, with the field of view being the angle between the two sides of the paper.
This terrible ascii art of the camera’s view might help you understand:

``````
\        /
\  FOV /
\    /
\  /
\/
camera
``````

So, onto the math… which is actually pretty simple. To find the absolute angle (from 0 to the field of view of the camera) from any given x value, you simply do this:

``````double absAngle = x/imgW*FOV;
``````

where imgW is the width of the image (available in the particle report), FOV is the field of view, and x is whatever x value you want (the particle’s center x, its left/right bounding box edges, etc.).

Now, having the absolute angle isn’t actually that useful on its own. Your camera is looking forward, so why should you care about how many degrees from its left boundary anything is? What you do care about is how many degrees are between the camera’s center (ie. the direction the robot is facing), and the angle you want to be facing (ie. the target). To do this, you calculate the relative angle, which is an even simpler calculation:

``````double relAngle = absAngle - FOV/2;
``````

This maps it from a range of [0,FOV] to a range of -FOV/2,FOV/2], with the direction the camera’s facing at 0, right in the center.

You can apply this formula to different bits of data from the report to get different bits of information. For example, if you find the angles of the left and right sides of the target, you know a range of angles, all of which are technically on-target, instead of just having the angle of the center.

Just a couple notes of fields of view: first, they’re usually available on a camera’s data sheet, but I happen to know that the M1011’s horizontal fov is 47 degrees and (I’m not as sure on this one) the 206’s fov is 54 degrees. Secondly, cameras also have a vertical field of view. Although I’m fairly certain that it’s proportional to the horizontal field of view by the same ratio as width to height, I haven’t done all the math and can’t guarantee that, so I’d suggest that you determine that empirically if you need it (there’s a method to do that, but it’s a bit wordy so I won’t go into any more depth unless you ask me to).