Now you can download a copy of these docs so you can use them offline! Download now
VisionAPI.cpp
00001 /******************************************************************************** 00002 * Project : FIRST Motor Controller 00003 * File Name : VisionAPI.cpp 00004 * Contributors : ELF, EMF 00005 * Creation Date : June 26, 2008 00006 * Revision History : Source code & revision history maintained at sourceforge.WPI.edu 00007 * File Description : C Routines for FIRST Vision API. Open source API developed 00008 * by BAE Systems to interface with the National Instruments vision C library 00009 * in the nivision.out module. The published interface to nivision.out is in 00010 * the header file nivision.h and documented in the NIVisionCVI.chm help file. 00011 */ 00012 /*----------------------------------------------------------------------------*/ 00013 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00014 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00015 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00016 /*----------------------------------------------------------------------------*/ 00017 00018 #include "stdioLib.h" 00019 #include "vxWorks.h" 00020 00021 #include "BaeUtilities.h" 00022 #include "FrcError.h" 00023 #include "VisionAPI.h" 00024 00025 int VisionAPI_debugFlag = 1; 00026 #define DPRINTF if(VisionAPI_debugFlag)dprintf 00027 00028 /* Image Management functions */ 00029 00040 Image* frcCreateImage(ImageType type) { return imaqCreateImage(type, DEFAULT_BORDER_SIZE); } 00041 00048 int frcDispose(void* object) { return imaqDispose(object); } 00058 int frcDispose( const char* functionName, ... ) /* Variable argument list */ 00059 { 00060 va_list disposalPtrList; /* Input argument list */ 00061 void* disposalPtr; /* For iteration */ 00062 int success, returnValue = 1; 00063 00064 va_start( disposalPtrList, functionName ); /* start of variable list */ 00065 disposalPtr = va_arg( disposalPtrList, void* ); 00066 while( disposalPtr != NULL ) { 00067 success = imaqDispose(disposalPtr); 00068 if (!success) {returnValue = 0;} 00069 disposalPtr = va_arg( disposalPtrList, void* ); 00070 } 00071 return returnValue; 00072 } 00073 00084 int frcCopyImage(Image* dest, const Image* source) { return imaqDuplicate(dest, source); } 00085 00096 int frcCrop(Image* dest, const Image* source, Rect rect) 00097 { 00098 return imaqScale(dest, source, 1, 1, IMAQ_SCALE_LARGER, rect); 00099 } 00100 00101 00114 int frcScale(Image* dest, const Image* source, int xScale, int yScale, ScalingMode scaleMode) 00115 { 00116 Rect rect = IMAQ_NO_RECT; 00117 return imaqScale(dest, source, xScale, yScale, scaleMode, rect); 00118 } 00119 00130 int frcReadImage(Image* image, const char* fileName) 00131 { 00132 return imaqReadFile(image, fileName, NULL, NULL); 00133 } 00134 00135 00163 int frcWriteImage(const Image* image, const char* fileName) 00164 { 00165 RGBValue* colorTable = NULL; 00166 return imaqWriteFile(image, fileName, colorTable); 00167 } 00168 00169 00170 /* Measure Intensity functions */ 00171 00200 HistogramReport* frcHistogram(const Image* image, int numClasses, float min, float max) 00201 { 00202 Rect rect = IMAQ_NO_RECT; 00203 return frcHistogram(image, numClasses, min, max, rect); 00204 } 00205 HistogramReport* frcHistogram(const Image* image, int numClasses, float min, float max, Rect rect) 00206 { 00207 int success; 00208 int fillValue = 1; 00209 00210 /* create the region of interest */ 00211 ROI* pRoi = imaqCreateROI(); 00212 success = imaqAddRectContour(pRoi, rect); 00213 if ( !success ) { GetLastVisionError(); return NULL; } 00214 00215 /* make a mask from the ROI */ 00216 Image* pMask = frcCreateImage(IMAQ_IMAGE_U8); 00217 success = imaqROIToMask(pMask, pRoi, fillValue, NULL, NULL); 00218 if ( !success ) { 00219 GetLastVisionError(); 00220 frcDispose(__FUNCTION__, pRoi, NULL); 00221 return NULL; 00222 } 00223 00224 /* get a histogram report */ 00225 HistogramReport* pHr = NULL; 00226 pHr = imaqHistogram(image, numClasses, min, max, pMask); 00227 00228 /* clean up */ 00229 frcDispose(__FUNCTION__, pRoi, pMask, NULL); 00230 00231 return pHr; 00232 } 00233 00253 ColorHistogramReport* frcColorHistogram(const Image* image, int numClasses, ColorMode mode) 00254 { 00255 return frcColorHistogram(image, numClasses, mode, NULL); 00256 } 00257 00258 ColorHistogramReport* frcColorHistogram(const Image* image, int numClasses, ColorMode mode, Image* mask) 00259 { 00260 return imaqColorHistogram2((Image*)image, numClasses, mode, NULL, mask); 00261 } 00262 00263 00275 int frcGetPixelValue(const Image* image, Point pixel, PixelValue* value) 00276 { 00277 return imaqGetPixel(image, pixel, value); 00278 } 00279 00280 00281 /* Particle Analysis functions */ 00282 00297 int frcParticleFilter(Image* dest, Image* source, const ParticleFilterCriteria2* criteria, 00298 int criteriaCount, const ParticleFilterOptions* options, int* numParticles) 00299 { 00300 Rect rect = IMAQ_NO_RECT; 00301 return frcParticleFilter(dest, source, criteria, criteriaCount, options, rect, numParticles); 00302 } 00303 00304 int frcParticleFilter(Image* dest, Image* source, const ParticleFilterCriteria2* criteria, 00305 int criteriaCount, const ParticleFilterOptions* options, Rect rect, int* numParticles) 00306 { 00307 ROI* roi = imaqCreateROI(); 00308 imaqAddRectContour(roi, rect); 00309 return imaqParticleFilter3(dest, source, criteria, criteriaCount, options, roi, numParticles); 00310 } 00311 00312 00327 int frcMorphology(Image* dest, Image* source, MorphologyMethod method) 00328 { 00329 return imaqMorphology(dest, source, method, NULL); 00330 } 00331 00332 int frcMorphology(Image* dest, Image* source, MorphologyMethod method, const StructuringElement* structuringElement) 00333 { 00334 return imaqMorphology(dest, source, method, structuringElement); 00335 } 00336 00350 int frcRejectBorder(Image* dest, Image* source) 00351 { return imaqRejectBorder(dest, source, TRUE); } 00352 00353 int frcRejectBorder(Image* dest, Image* source, int connectivity8) 00354 { 00355 return imaqRejectBorder(dest, source, connectivity8); 00356 } 00357 00358 00366 int frcCountParticles(Image* image, int* numParticles) 00367 { 00368 return imaqCountParticles(image, 1, numParticles); 00369 } 00370 00371 00382 int frcParticleAnalysis(Image* image, int particleNumber, ParticleAnalysisReport* par) 00383 { 00384 int success = 0; 00385 00386 /* image information */ 00387 int height, width; 00388 if ( ! imaqGetImageSize(image, &width, &height) ) { return success; } 00389 par->imageWidth = width; 00390 par->imageHeight = height; 00391 par->particleIndex = particleNumber; 00392 00393 /* center of mass point of the largest particle */ 00394 double returnDouble; 00395 success = imaqMeasureParticle(image, particleNumber, 0, IMAQ_MT_CENTER_OF_MASS_X, &returnDouble); 00396 if ( !success ) { return success; } 00397 par->center_mass_x = (int)returnDouble; // pixel 00398 00399 success = imaqMeasureParticle(image, particleNumber, 0, IMAQ_MT_CENTER_OF_MASS_Y, &returnDouble); 00400 if ( !success ) { return success; } 00401 par->center_mass_y = (int)returnDouble; // pixel 00402 00403 /* particle size statistics */ 00404 success = imaqMeasureParticle(image, particleNumber, 0, IMAQ_MT_AREA, &returnDouble); 00405 if ( !success ) { return success; } 00406 par->particleArea = returnDouble; 00407 00408 success = imaqMeasureParticle(image, particleNumber, 0, IMAQ_MT_BOUNDING_RECT_TOP, &returnDouble); 00409 if ( !success ) { return success; } 00410 par->boundingRect.top = (int)returnDouble; 00411 00412 success = imaqMeasureParticle(image, particleNumber, 0, IMAQ_MT_BOUNDING_RECT_LEFT, &returnDouble); 00413 if ( !success ) { return success; } 00414 par->boundingRect.left = (int)returnDouble; 00415 00416 success = imaqMeasureParticle(image, particleNumber, 0, IMAQ_MT_BOUNDING_RECT_HEIGHT, &returnDouble); 00417 if ( !success ) { return success; } 00418 par->boundingRect.height = (int)returnDouble; 00419 00420 success = imaqMeasureParticle(image, particleNumber, 0, IMAQ_MT_BOUNDING_RECT_WIDTH, &returnDouble); 00421 if ( !success ) { return success; } 00422 par->boundingRect.width = (int)returnDouble; 00423 00424 /* particle quality statistics */ 00425 success = imaqMeasureParticle(image, particleNumber, 0, IMAQ_MT_AREA_BY_IMAGE_AREA, &returnDouble); 00426 if ( !success ) { return success; } 00427 par->particleToImagePercent = returnDouble; 00428 00429 success = imaqMeasureParticle(image, particleNumber, 0, IMAQ_MT_AREA_BY_PARTICLE_AND_HOLES_AREA, &returnDouble); 00430 if ( !success ) { return success; } 00431 par->particleQuality = returnDouble; 00432 00433 /* normalized position (-1 to 1) */ 00434 par->center_mass_x_normalized = RangeToNormalized(par->center_mass_x, width); 00435 par->center_mass_y_normalized = RangeToNormalized(par->center_mass_y, height); 00436 00437 return success; 00438 } 00439 00440 00441 /* Image Enhancement functions */ 00442 00457 int frcEqualize(Image* dest, const Image* source, float min, float max) 00458 { return frcEqualize(dest, source, min, max, NULL); } 00459 00460 int frcEqualize(Image* dest, const Image* source, float min, float max, const Image* mask) 00461 { 00462 return imaqEqualize(dest, source, min, max, mask); 00463 } 00464 00475 int frcColorEqualize(Image* dest, const Image* source) 00476 { 00477 return imaqColorEqualize(dest, source, TRUE); 00478 } 00479 00480 int frcColorEqualize(Image* dest, const Image* source, int colorEqualization) 00481 { 00482 return imaqColorEqualize(dest, source, TRUE); 00483 } 00484 00485 /* Image Conversion functions */ 00486 00510 int frcSmartThreshold(Image* dest, const Image* source, 00511 unsigned int windowWidth, unsigned int windowHeight, LocalThresholdMethod method, 00512 double deviationWeight, ObjectType type) 00513 { 00514 float replaceValue = 1.0; 00515 return imaqLocalThreshold(dest, source, windowWidth, windowHeight, method, 00516 deviationWeight, type, replaceValue); 00517 } 00518 00519 int frcSmartThreshold(Image* dest, const Image* source, 00520 unsigned int windowWidth, unsigned int windowHeight, LocalThresholdMethod method, 00521 double deviationWeight, ObjectType type, float replaceValue) 00522 { 00523 return imaqLocalThreshold(dest, source, windowWidth, windowHeight, method, 00524 deviationWeight, type, replaceValue); 00525 } 00526 00541 int frcSimpleThreshold(Image* dest, const Image* source, float rangeMin, float rangeMax) 00542 { 00543 int newValue = 255; 00544 return frcSimpleThreshold(dest, source, rangeMin, rangeMax, newValue); 00545 } 00546 00561 int frcSimpleThreshold(Image* dest, const Image* source, float rangeMin, float rangeMax, float newValue) 00562 { 00563 int useNewValue = TRUE; 00564 return imaqThreshold(dest, source, rangeMin, rangeMax, useNewValue, newValue); 00565 } 00566 00582 int frcColorThreshold(Image* dest, const Image* source, ColorMode mode, 00583 const Range* plane1Range, const Range* plane2Range, const Range* plane3Range) 00584 { 00585 int replaceValue = 1; 00586 return imaqColorThreshold(dest, source, replaceValue, mode, plane1Range, plane2Range, plane3Range); 00587 } 00588 00605 int frcColorThreshold(Image* dest, const Image* source, int replaceValue, ColorMode mode, 00606 const Range* plane1Range, const Range* plane2Range, const Range* plane3Range) 00607 { return imaqColorThreshold(dest, source, replaceValue, mode, plane1Range, plane2Range, plane3Range);} 00608 00609 00619 int frcHueThreshold(Image* dest, const Image* source, const Range* hueRange) 00620 { return frcHueThreshold(dest, source, hueRange, DEFAULT_SATURATION_THRESHOLD); } 00621 00622 int frcHueThreshold(Image* dest, const Image* source, const Range* hueRange, int minSaturation) 00623 { 00624 // assume HSL mode 00625 ColorMode mode = IMAQ_HSL; 00626 // Set saturation 100 - 255 00627 Range satRange; satRange.minValue = minSaturation; satRange.maxValue = 255; 00628 // Set luminance 100 - 255 00629 Range lumRange; lumRange.minValue = 100; lumRange.maxValue = 255; 00630 // Replace pixels with 1 if pass threshold filter 00631 int replaceValue = 1; 00632 return imaqColorThreshold(dest, source, replaceValue, mode, hueRange, &satRange, &lumRange); 00633 } 00634 00647 int frcExtractColorPlanes(const Image* image, ColorMode mode, 00648 Image* plane1, Image* plane2, Image* plane3) 00649 { return imaqExtractColorPlanes(image, mode, plane1, plane2, plane3); } 00650 00660 int frcExtractHuePlane(const Image* image, Image* huePlane) 00661 { 00662 return frcExtractHuePlane(image, huePlane, DEFAULT_SATURATION_THRESHOLD); 00663 } 00664 00665 int frcExtractHuePlane(const Image* image, Image* huePlane, int minSaturation) 00666 { 00667 return frcExtractColorPlanes(image, IMAQ_HSL, huePlane, NULL, NULL); 00668 } 00669 00670 00671 00672 00673 00674 00675 00676
Generated on Thu Jan 12 2012 22:35:25 for WPILibC++ by
1.7.1