Now you can download a copy of these docs so you can use them offline! Download now
BinaryImage.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "BinaryImage.h" 00008 #include "WPIErrors.h" 00009 00011 IMAQ_FUNC int Priv_SetWriteFileAllowed(UINT32 enable); 00012 00013 BinaryImage::BinaryImage() : MonoImage() 00014 { 00015 } 00016 00017 BinaryImage::~BinaryImage() 00018 { 00019 } 00020 00025 int BinaryImage::GetNumberParticles() 00026 { 00027 int numParticles = 0; 00028 int success = imaqCountParticles(m_imaqImage, 1, &numParticles); 00029 wpi_setImaqErrorWithContext(success, "Error counting particles"); 00030 return numParticles; 00031 } 00032 00039 ParticleAnalysisReport BinaryImage::GetParticleAnalysisReport(int particleNumber) 00040 { 00041 ParticleAnalysisReport par; 00042 GetParticleAnalysisReport(particleNumber, &par); 00043 return par; 00044 } 00045 00053 void BinaryImage::GetParticleAnalysisReport(int particleNumber, ParticleAnalysisReport *par) 00054 { 00055 int success; 00056 int numParticles = 0; 00057 00058 success = imaqGetImageSize(m_imaqImage, &par->imageWidth, &par->imageHeight); 00059 wpi_setImaqErrorWithContext(success, "Error getting image size"); 00060 if (StatusIsFatal()) 00061 return; 00062 00063 success = imaqCountParticles(m_imaqImage, 1, &numParticles); 00064 wpi_setImaqErrorWithContext(success, "Error counting particles"); 00065 if (StatusIsFatal()) 00066 return; 00067 00068 if (particleNumber >= numParticles) 00069 { 00070 wpi_setWPIErrorWithContext(ParameterOutOfRange, "particleNumber"); 00071 return; 00072 } 00073 00074 par->particleIndex = particleNumber; 00075 // Don't bother measuring the rest of the particle if one fails 00076 bool good = ParticleMeasurement(particleNumber, IMAQ_MT_CENTER_OF_MASS_X, &par->center_mass_x); 00077 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_CENTER_OF_MASS_Y, &par->center_mass_y); 00078 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_AREA, &par->particleArea); 00079 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_BOUNDING_RECT_TOP, &par->boundingRect.top); 00080 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_BOUNDING_RECT_LEFT, &par->boundingRect.left); 00081 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_BOUNDING_RECT_HEIGHT, &par->boundingRect.height); 00082 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_BOUNDING_RECT_WIDTH, &par->boundingRect.width); 00083 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_AREA_BY_IMAGE_AREA, &par->particleToImagePercent); 00084 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_AREA_BY_PARTICLE_AND_HOLES_AREA, &par->particleQuality); 00085 00086 if (good) 00087 { 00088 /* normalized position (-1 to 1) */ 00089 par->center_mass_x_normalized = NormalizeFromRange(par->center_mass_x, par->imageWidth); 00090 par->center_mass_y_normalized = NormalizeFromRange(par->center_mass_y, par->imageHeight); 00091 } 00092 } 00093 00094 00102 vector<ParticleAnalysisReport>* BinaryImage::GetOrderedParticleAnalysisReports() 00103 { 00104 vector<ParticleAnalysisReport>* particles = new vector<ParticleAnalysisReport>; 00105 int particleCount = GetNumberParticles(); 00106 for(int particleIndex = 0; particleIndex < particleCount; particleIndex++) 00107 { 00108 particles->push_back(GetParticleAnalysisReport(particleIndex)); 00109 } 00110 // TODO: This is pretty inefficient since each compare in the sort copies 00111 // both reports being compared... do it manually instead... while we're 00112 // at it, we should provide a version that allows a preallocated buffer of 00113 // ParticleAnalysisReport structures 00114 sort(particles->begin(), particles->end(), CompareParticleSizes); 00115 return particles; 00116 } 00117 00123 void BinaryImage::Write(const char *fileName) 00124 { 00125 RGBValue colorTable[256]; 00126 Priv_SetWriteFileAllowed(1); 00127 memset(colorTable, 0, sizeof(colorTable)); 00128 colorTable[0].R = 0; 00129 colorTable[1].R = 255; 00130 colorTable[0].G = colorTable[1].G = 0; 00131 colorTable[0].B = colorTable[1].B = 0; 00132 colorTable[0].alpha = colorTable[1].alpha = 0; 00133 imaqWriteFile(m_imaqImage, fileName, colorTable); 00134 } 00135 00145 bool BinaryImage::ParticleMeasurement(int particleNumber, MeasurementType whatToMeasure, int *result) 00146 { 00147 double resultDouble; 00148 bool success = ParticleMeasurement(particleNumber, whatToMeasure, &resultDouble); 00149 *result = (int)resultDouble; 00150 return success; 00151 } 00152 00162 bool BinaryImage::ParticleMeasurement(int particleNumber, MeasurementType whatToMeasure, double *result) 00163 { 00164 int success; 00165 success = imaqMeasureParticle(m_imaqImage, particleNumber, 0, whatToMeasure, result); 00166 wpi_setImaqErrorWithContext(success, "Error measuring particle"); 00167 return !StatusIsFatal(); 00168 } 00169 00170 //Normalizes to [-1,1] 00171 double BinaryImage::NormalizeFromRange(double position, int range) 00172 { 00173 return (position * 2.0 / (double)range) - 1.0; 00174 } 00175 00183 bool BinaryImage::CompareParticleSizes(ParticleAnalysisReport particle1, ParticleAnalysisReport particle2) 00184 { 00185 //we want descending sort order 00186 return particle1.particleToImagePercent > particle2.particleToImagePercent; 00187 } 00188 00189
Generated on Thu Jan 12 2012 22:35:17 for WPILibC++ by
1.7.1