Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   GetParticleAnalysisReport causes crash (http://www.chiefdelphi.com/forums/showthread.php?t=101512)

Sinani201 29-01-2012 23:39

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:
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.

basicxman 29-01-2012 23:57

Re: GetParticleAnalysisReport causes crash
 
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.

mikets 30-01-2012 03:54

Re: GetParticleAnalysisReport causes crash
 
If you take a look at the source code of GetParticleAnalysisReport:
Code:

/**
 * 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.

mikets 30-01-2012 04:08

Re: GetParticleAnalysisReport causes crash
 
Quote:

Originally Posted by Sinani201 (Post 1116267)
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;


It looks like the code should have been:
Code:

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;


bob.wolff68 30-01-2012 12:00

Re: GetParticleAnalysisReport causes crash
 
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...

Code:

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

Sinani201 30-01-2012 18:16

Re: GetParticleAnalysisReport causes crash
 
Thank you all very much. It's working now.

mikets 30-01-2012 18:17

Re: GetParticleAnalysisReport causes crash
 
Would you share how you solved your problem so other people can learn from it?


All times are GMT -5. The time now is 13:38.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi