Log in

View Full Version : How can angle offset of a shooter be calculated from pixel offset?


simon-andrews
02-04-2016, 16:57
My team's robot has a high goal shooter on a turntable. Using our vision system, I need to figure out how far off-target the shooter is in degrees. I've been working on this for most of today without much luck.

Things I know:

How far the middle of the goal is from the middle of the image (pixels) (http://imgur.com/xYRdRVJ)
How far away the camera is from the goal (feet)
How high off the ground the camera is (inches or feet)


The camera is in the middle of the shooter.

I'm using OpenCV with Python, if that's useful.

Oblarg
02-04-2016, 17:01
My team's robot has a high goal shooter on a turntable. Using our vision system, I need to figure out how far off-target the shooter is in degrees. I've been working on this for most of today without much luck.

Things I know:

How far the middle of the goal is from the middle of the image (pixels) (http://imgur.com/xYRdRVJ)
How far away the camera is from the goal (feet)
How high off the ground the camera is (inches or feet)


The camera is in the middle of the shooter.

I'm using OpenCV with Python, if that's useful.

Pixel displacement should be strictly proportional to angular error, since the camera is effectively a point. To convince yourself of this, draw a picture of the shooter and the target (top view, camera at the origin).

Figure out the lateral FOV of the camera in degrees, and calculate proportional to that using the known width of the image in pixels.

lethc
02-04-2016, 17:03
My team's robot has a high goal shooter on a turntable. Using our vision system, I need to figure out how far off-target the shooter is in degrees. I've been working on this for most of today without much luck.

Things I know:

How far the middle of the goal is from the middle of the image (pixels) (http://imgur.com/xYRdRVJ)
How far away the camera is from the goal (feet)
How high off the ground the camera is (inches or feet)


The camera is in the middle of the shooter.

I'm using OpenCV with Python, if that's useful.

If your camera is mounted solidly then you should be able to interpolate the offset angle of your shooter by using the first option (how many pixels away from the middle the goal is). This isn't necessarily the best way to solve the problem but it works and it's really easy. Is that all you're asking?

kylelanman
02-04-2016, 18:56
Screen steps live 2016 explains how to do this. https://wpilib.screenstepslive.com/s/4485/m/24194/l/288985-identifying-and-processing-the-targets

simon-andrews
02-04-2016, 19:16
Pixel displacement should be strictly proportional to angular error, since the camera is effectively a point. To convince yourself of this, draw a picture of the shooter and the target (top view, camera at the origin).

Figure out the lateral FOV of the camera in degrees, and calculate proportional to that using the known width of the image in pixels.

If your camera is mounted solidly then you should be able to interpolate the offset angle of your shooter by using the first option (how many pixels away from the middle the goal is). This isn't necessarily the best way to solve the problem but it works and it's really easy. Is that all you're asking?

Sorry, I worded my original post poorly and wasn't clear. This method actually is what I was doing originally, with this code:


def get_offsets(bb_leftside_x, bb_width, img_width):
middle = bb_leftside_x + (bb_width / 2)
pixel_offset = middle - img_width / 2
angle_offset = pixel_offset * (26.5 / (img_width / 2)) # Raspberry Pi camera module FOV is 53 degrees
ret = {
"pixel_offset": pixel_offset,
"angle_offset": angle_offset,
}
return ret


What I need to get is an angle that can be fed directly into the turntable as an amount to turn.

https://imgur.com/RelZro4

Arhowk
02-04-2016, 19:30
You clearly calculate angle_offset right in that code...

Are you asking how to make a PID loop to turn the robot?

simon-andrews
02-04-2016, 20:19
You clearly calculate angle_offset right in that code...

Are you asking how to make a PID loop to turn the robot?

No. That function isn't what I need because it doesn't return an angle that can be plugged into the turntable. The program I'm supposed to write should be able to look at an image and figure out "Alright, turntable. You need to turn 45 degrees if you want to be on-target."

If we look at this drawing (https://imgur.com/RelZro4) from an earlier post, both lines (which represent pixel offsets) are 100px long. With the function from before, both would return the same angle offset since the function doesn't account for distance away from the target.

Oblarg
02-04-2016, 20:26
No. That function isn't what I need because it doesn't return an angle that can be plugged into the turntable. The program I'm supposed to write should be able to look at an image and figure out "Alright, turntable. You need to turn 45 degrees if you want to be on-target."

If we look at this drawing (https://imgur.com/RelZro4) from an earlier post, both lines (which represent pixel offsets) are 100px long. With the function from before, both would return the same angle offset since the function doesn't account for distance away from the target.

You're slightly confused.

Distance away from the target doesn't matter, that's precisely why you're measuring in angle (a dimensionless quantity) and not in distance.

Imagine the camera at the origin, pointing in the +y direction. Place your target off to the side somewhere. How far off-center your target will appear in pixels is proportional to the angle between the line from the origin to the target and the y axis, not to the distance from the point to the y axis. To see this, place one target at (1,1). Place another target at (1,2). These are the same distance from the y-axis, which is the centerline of the camera's FOV, but they will clearly appear at different distances from the center when viewed from the camera, because the camera is a point at the origin and only sees along rays that pass through it.

For an even better demonstration: Cover one of your eyes, align your other (open) eye with the center of your computer screen. Place your finger between your face and your computer screen, so that it overlaps with the edge of the screen in your vision. Notice that your finger is closer to the centerline between your eye and the center of the screen than the edge of the monitor is, despite the fact that they appear the same "distance" away in the 2d image that you see.

The fundamental confusion here is that you're thinking in terms of rays parallel to the center-line of the camera. But that's incorrect, because those rays never reach the camera. Think of the camera as a point, not as a screen. All the rays that the camera sees must pass through the point. Thinking this way, it is clear that "distance" in the image the camera sees corresponds to *angular* distance. To preserve apparent "distance" between two objects from the point of view of the camera, you must preserve their relative angular positions.

GeeTwo
02-04-2016, 20:55
If your method described above doesn't seem to give you quite the right answer, perhaps you really want to know what the proportionality constant (pixels to degrees) is for small angles (rather than the full image width). To do this, I suggest:


Position the robot so that the target is reasonably close to the center of the image.
Note exactly where the target center is located.
Rotate by a known small number of degrees (perhaps 5 or 10)
Note where the center of the target is now.
Divide the number of pixels moved from step 2 to 4 by the number of degrees rotated in step 3


This will give you the number of pixels per degree for small angles near the center of the camera image.

Arhowk
02-04-2016, 21:00
If you want to aim to the center of the target than obtain the center x of the target and rotate to it using the formulas postEd.

In the photo posted, the left edge of the target is roughly at x=0 (assuming a graph is overlayed) and the right boundary is at x=x for the "far" target and x=2x for the "near" target, meaning the centers would be at x=1/2x and x=x respectively. The "near" target will request twice as much turning.

Also please stop using the word turntable (http://www.turntablelab.com/pages/beginners-guide-to-turntables)

GreyingJay
04-04-2016, 14:48
Also please stop using the word turntable (http://www.turntablelab.com/pages/beginners-guide-to-turntables)

It's a Lazy Susan!

Chris Hibner
04-04-2016, 21:06
It's fairly simple. It's (pixels from center) / (camera pixel width) * FOV
For example, your camera has a field of view (FOV) of 45 degrees, your camera width is 320 pixels, and your image is 16 pixels off center. Then the angle to aim is:

(320 pixles/16 pixels) * 45 degrees = 2.25 degrees

You can calibrate your exact camera FOV by pointing your camera at a white board and marking two points on the white board at the left and right edge of the camera image. Then measure the distance between the two points on the white board, and the distance between the white board and the camera. Then a little trig will give you the FOV.