What the you have to do is determine the width of the target in pixels, given a constant value w/ the target centered, and find the hypotenuse, which is your distance
EDIT: Use the height for distance, not the width.The width can vary with angle to the target.
Example:
Note, these are necessary constants
Code:
public static final int IMAGE_WIDTH = 320;
public static final int IMAGE_HEIGHT = 240;
public static final double TARGET_WIDTH = 2.0;
public static final double TARGET_HEIGHT = 1.5;
public static final double RECTANGLE_QUALITY_THRESHOLD = 95.0;
public static final double PARTICLE_TO_IMAGE_THRESHOLD = 0.05;
public static final double PARTICLE_AREA_THRESHOLD = 250.0;
protected double fieldOfViewWidth = 0.0;
/**
* Return distance to target in inches
*
* @param targetHeightInPixels
* @return
*/
public double GetDistanceToTarget(int targetHeightInPixels)
{
double fieldOfViewHeight = TARGET_HEIGHT / targetHeightInPixels * IMAGE_HEIGHT;
distance = (fieldOfViewHeight / 2.0) /
/*
* tan 23.5 degrees
*/
0.4348123749;
return distance * 12.0 * 1.4;
}
I am assuming you can figure out how to filter for targets.