GetParticleAnalysisReport causes crash

Whenever I try to use the command GetParticleAnalysisReport, the robot stops working and I have to reboot it. Not sure why this is happening. Here is the code:


ColorImage* camerain = camera->GetImage();
BinaryImage* binImage = camerain->ThresholdHSL(0,9,0,5,250,255);
int numParticles = binImage->GetNumberParticles();
delete camerain;
ParticleAnalysisReport* report;
for(int i = 1; i<=numParticles; i++)
{
	binImage->GetParticleAnalysisReport(i,report);
	// Other processing stuff
}
delete binImage;

This happens the very first time GetParticleAnalysisReport is called, so it doesn’t have to so with overwriting the report variable (though I don’t know if this will end up being a problem later).

Any help would be greatly appreciated.

I’m not sure if this would actually cause a crash, but the function you’re using ends up calling imaqMeasureParticle which likely takes a zero-indexed particle number. You’re starting at 1.

If you take a look at the source code of GetParticleAnalysisReport:


/**
 * Get a single particle analysis report.
 * Get one (of possibly many) particle analysis reports for an image.
 * This version could be more efficient when copying many reports.
 * @param particleNumber Which particle analysis report to return.
 * @param par the selected particle analysis report
 */
void BinaryImage::GetParticleAnalysisReport(int particleNumber, ParticleAnalysisReport *par)
{
 int success;
 int numParticles = 0;
 success = imaqGetImageSize(m_imaqImage, &par->imageWidth, &par->imageHeight);
 wpi_setImaqErrorWithContext(success, "Error getting image size");
 if (StatusIsFatal())
  return;
 success = imaqCountParticles(m_imaqImage, 1, &numParticles);
 wpi_setImaqErrorWithContext(success, "Error counting particles");
 if (StatusIsFatal())
  return;
 if (particleNumber >= numParticles)
 {
  wpi_setWPIErrorWithContext(ParameterOutOfRange, "particleNumber");
  return;
 }
 par->particleIndex = particleNumber;
 // Don't bother measuring the rest of the particle if one fails
 bool good = ParticleMeasurement(particleNumber, IMAQ_MT_CENTER_OF_MASS_X, &par->center_mass_x);
 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_CENTER_OF_MASS_Y, &par->center_mass_y);
 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_AREA, &par->particleArea);
 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_BOUNDING_RECT_TOP, &par->boundingRect.top);
 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_BOUNDING_RECT_LEFT, &par->boundingRect.left);
 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_BOUNDING_RECT_HEIGHT, &par->boundingRect.height);
 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_BOUNDING_RECT_WIDTH, &par->boundingRect.width);
 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_AREA_BY_IMAGE_AREA, &par->particleToImagePercent);
 good = good && ParticleMeasurement(particleNumber, IMAQ_MT_AREA_BY_PARTICLE_AND_HOLES_AREA, &par->particleQuality);
 if (good)
 {
  /* normalized position (-1 to 1) */
  par->center_mass_x_normalized = NormalizeFromRange(par->center_mass_x, par->imageWidth);
  par->center_mass_y_normalized = NormalizeFromRange(par->center_mass_y, par->imageHeight);
 }
}

You will see that the parameter par is a pointer to the particle analysis report. The code is expecting the pointer is pointing to something. But the code that called the function passed in an uninitialized ParticleAnalysisReport pointer. That will caused an exception for sure. Where did you copy this code from? It should have a line allocating/initializing the ParticleAnalysisReport before passing it to the function.

It looks like the code should have been:


ColorImage* camerain = camera->GetImage();
BinaryImage* binImage = camerain->ThresholdHSL(0,9,0,5,250,255);
int numParticles = binImage->GetNumberParticles();
delete camerain;
for(int i = 0; i<numParticles; i++)
{
    ParticleAnalysisReport = binImage->GetParticleAnalysisReport(i);
    // Other processing stuff
}
delete binImage;

The prior post is fine as well, but if you’re still wondering the subtlety of why yours didn’t work originally, see the // CHANGE line here…

ColorImage* camerain = camera->GetImage();
BinaryImage* binImage = camerain->ThresholdHSL(0,9,0,5,250,255);
int numParticles = binImage->GetNumberParticles();
delete camerain;
ParticleAnalysisReport report; // **CHANGE** - instantiates the real object and not just a pointer to it...(and then below...)
for(int i = 1; i<=numParticles; i++)
{
    binImage->GetParticleAnalysisReport(i,&report);  // **CHANGE** and now pass the address of the real object to the function so it can make modifications to your real object 'report' from above.
    // Other processing stuff
}
delete binImage;

The only other concern is it should be “for (i=0 ; i<numParticles; i++)” or you’ll crash at the tail end of the for-loop for going out of bounds. Arrays in C always start with the ‘0’ element … not ‘1’.

bob

Thank you all very much. It’s working now.

Would you share how you solved your problem so other people can learn from it?