Sinclair Sprockets
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Current Camera Code

Go down

Current Camera Code Empty Current Camera Code

Post  William Surmak Tue Dec 04, 2012 10:00 pm

The current 2 functions that deal with camera targeting and their corresponding macros.

// Camera HSL Image Thresholds
#define HUEMAX 255
#define HUEMIN 0
#define SATURATIONMAX 15
#define SATURATIONMIN 0
#define LUMINANCEMAX 255
#define LUMINANCEMIN 200 // 160
#define REDMIN 250
#define REDMAX 255
#define BLUEMIN 30
#define BLUEMAX 170
#define GREENMIN 30
#define GREENMAX 170
#define MINIMUMAREA 220

/* Take the camera image and process it, returning the x-position of the largest particle. This should represent the
horizontal centre of the most in-focus target. Comparing this value to the image width (320) should allow us to
centre the image by rotating the robot. If the targets are out of view, -1 is returned. */
int GetProcessedImage(AxisCamera &camera)
{
register int i;
int numberOfParticles, largestParticle;
double maxArea = 0, width, height, horizontalPosition;

// Save the image as a BinaryImage Object
cameraImage = camera.GetImage();
//binaryImage = cameraImage->ThresholdHSLHUEMIN, HUEMAX, SATURATIONMIN, SATURATIONMAX, LUMINANCEMIN, LUMINANCEMAX);
binaryImage = cameraImage->ThresholdRGB(REDMIN, REDMAX, GREENMIN, GREENMAX, BLUEMIN, BLUEMAX);
imaqImage = binaryImage->GetImaqImage();

// Perform the Convex Hull operation
imaqConvexHull(imaqImage, imaqImage, 1);

// Count the number of distinct sections remaining
imaqCountParticles(imaqImage, 2, &numberOfParticles);

// Save image to the cRIO's memory (enable for debugging purposes only)
//cameraImage->Write("camIm4.jpeg");
//binaryImage->Write("binIm4.bmp");

// For each particle found
for(i = 0; i < numberOfParticles; i++)
{
// Get the width and height of the given particle
imaqMeasureParticle(imaqImage, i, 0, IMAQ_MT_BOUNDING_RECT_WIDTH, &width);
imaqMeasureParticle(imaqImage, i, 0, IMAQ_MT_BOUNDING_RECT_HEIGHT, &height);

// If the area of this particle is the largest area so far
if(width * height > maxArea)
{
// Store the dimensions and area of the particle, along with the index of said particle
maxTargetWidth = width;
maxTargetHeight = height;
maxArea = width * height;
largestParticle = i;
}
}

// Find the x-position of the centre of the largest particle, assumed to be the nearest target
imaqMeasureParticle(imaqImage, largestParticle, 0, IMAQ_MT_CENTER_OF_MASS_X, &horizontalPosition);

delete binaryImage;
delete cameraImage;

// If the largest particle is outside of the threshold, return -1 to indicate that the targets aren't visible
if(maxArea < MINIMUMAREA) return -1;

// Return this x-position
return (int)horizontalPosition;
}

// Converts the width of the rectangle to a distance in feet
double WidthToDistance(double x)
{
double result; // The resut of the calculations

result = GOALWIDTH * IMAGEWIDTH / 2 / x / 2;
result = result / tan(HALFCAMERAVIEWINGANGLE * Pi / 180);
result = result * LINEARDISTANCEK + CONSTANTDISTANCEK;

return result;
}
William Surmak
William Surmak

Posts : 104
Join date : 2011-10-12

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum