int GetLargestParticle(Image* binaryImage, int* largestParticleIndex, Rect rect) { *largestParticleIndex = 0; // points to caller-provided variable /* determine number of particles in thresholded image */ int numParticles; int success = frcCountParticles(binaryImage, &numParticles); if ( !success ) { return success; } /* if no particles found we can quit here */ if (numParticles == 0) { return 0; } // unsuccessful if zero particles found // find the largest particle double largestParticleArea = 0; double particleArea; for (int i = 0; i < numParticles; ++i) { success = imaqMeasureParticle(binaryImage, i, 0, IMAQ_MT_AREA, &particleArea); if ( !success ) { return success; } if (particleArea > largestParticleArea) { // see if is in the right area if ( InArea(binaryImage, i, rect) ) { largestParticleArea = particleArea; *largestParticleIndex = i; // return index to caller } } } return success; }
/** * @brief Find the largest particles that meet a criteria * @param binaryImage Image to inspect * @param hitReport structure containing arry of hits - first hit is largest * @param rect area to search * @param numParticles Number of particles in array * @return 0 = error */ int GetLargestParticles(Image* binaryImage, ImageHits *hitReport, Rect rect, int numberHitsRequested) { //char funcName[]="GetLargestParticles"; HitList *hitsInArea = new HitList(); // list to contain all particles within area sorted by size int i; /* determine number of particles in thresholded image */ int numParticles = -1; int success = frcCountParticles(binaryImage, &numParticles); if ( !success ) { return success; } //DPRINTF(LOG_DEBUG, "particles requested = %i ; particles found in image = %i", numberHitsRequested, numParticles); /* if no particles found we can quit here */ if (numParticles <= 0) { return 1; } // successful, but zero particles found /* get areas of each particle and insert into list */ double particleArea; int count = 0; for (i = 0; i < numParticles; ++i) { success = imaqMeasureParticle(binaryImage, i, 0, IMAQ_MT_AREA, &particleArea); if ( !success ) { return success; } //DPRINTF (LOG_INFO, "size of particle %i = %g", i, particleArea); // see if is in the right area and large enough to be considered a possible target //TODO: Call InArea & delete TempInArea when new WPILib is released //if (InArea(binaryImage, i, rect)) { //if ( InArea(binaryImage, i, rect) && (particleArea >= FRC_MINIMUM_PIXELS_FOR_TARGET) ) { if ( TempInArea(binaryImage, i, rect) && (particleArea >= FRC_MINIMUM_PIXELS_FOR_TARGET) ) { hitsInArea->AddNode(i,particleArea); count++; } } // see what's in the list hitsInArea->debugDump(); // fill in return structure - number of hits if (numParticles < numberHitsRequested) { hitReport->numberOfHits = numParticles; } else { hitReport->numberOfHits = numberHitsRequested; } // fill in return structure - indices & areas of largest hits HitNode *hitPtr = hitsInArea->head; for (i = 0; i < hitReport->numberOfHits; ++i) { if (hitPtr == NULL) { break; } hitReport->indices[i] = hitPtr->nodeIndex; hitReport->areas[i] = hitPtr->nodeArea; //DPRINTF (LOG_INFO, "put index %i in hitReport %i", hitPtr->nodeIndex, hitReport->indices[i]); hitPtr = hitPtr->next; } // dispose of HitList delete hitsInArea; return 1; // success }