Ask Your Question
2

Allocation error with 'cvCreateGLCM' method

asked 2012-06-27 15:09:25 -0600

WordBreaker gravatar image

Hi, I am trying to compute GLCM for an input image. I load the image, convert it to grayscale, then I invoke cvCreateGLCM method on the grayscale image. OpenCV Version: 2.4.1, vc10, x64.

const char* filePath = "C:\\input.jpg";
IplImage* inputIm = cvLoadImage(filePath);

IplImage* grayIm = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
cvCvtColor(inputIm, grayIm, CV_RGB2GRAY);

CvGLCM* glcm = cvCreateGLCM(resizedGray, 0);  // ERROR

I get the following error message image description

when I press 'Break', it shows where the error took place:

__forceinline void * __cdecl _heap_alloc (size_t size)

{

    if (_crtheap == 0) {
        _FF_MSGBANNER();    /* write run-time error banner */
        _NMSG_WRITE(_RT_CRT_NOTINIT);  /* write message */
        __crtExitProcess(255);  /* normally _exit(255) */
    }

    return HeapAlloc(_crtheap, 0, size ? size : 1);  // <<< The error
}

Any ideas please?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2012-06-28 16:42:04 -0600

AlexanderShishkov gravatar image

There is error with cvAlloc in this code. See the following page: http://tech.dir.groups.yahoo.com/group/OpenCV/message/67611?var=1 You should change /opencv/modules/legacy/src/texture.cpp file to the next:

/*******************************************************************************\
*********\

Calculation of a texture descriptors from GLCM (Grey Level Co-occurrence
Matrix'es)
The code was submitted by Daniel Eaton [danieljameseaton@...]

\*******************************************************************************\
*********/

//#include "_cvaux.h"

//#include "legacy.h"
#include "precomp.hpp"

#define CV_MAX_NUM_GREY_LEVELS_8U 256
struct CvGLCM
{
int matrixSideLength;
int numMatrices;
double*** matrices;

int numLookupTableElements;
int forwardLookupTable[CV_MAX_NUM_GREY_LEVELS_8U];
int reverseLookupTable[CV_MAX_NUM_GREY_LEVELS_8U];

double** descriptors;
int numDescriptors;
int descriptorOptimizationType;
int optimizationType;
};

static void icv_CreateGLCM_LookupTable_8u_C1R( const uchar* srcImageData, int
srcImageStep, CvSize srcImageSize,
CvGLCM* destGLCM, int* steps, int numSteps, int* memorySteps );

static void icv_CreateGLCMDescriptors_AllowDoubleNest( CvGLCM* destGLCM, int
matrixIndex );

CvGLCM* cvCreateGLCM( const IplImage* srcImage, int stepMagnitude, const int* srcStepDirections,/* should be static array..or if not the user should handle de-allocation */ int numStepDirections, int optimizationType )
{
static const int defaultStepDirections[] = { 0,1,-1,1,-1,0,-1,-1 };

int* memorySteps = 0;
CvGLCM* newGLCM = 0;
int* stepDirections = 0;

CV_FUNCNAME( "cvCreateGLCM" );

__BEGIN__;

uchar* srcImageData = 0;
CvSize srcImageSize;
int srcImageStep;
int stepLoop;
const int maxNumGreyLevels8u = CV_MAX_NUM_GREY_LEVELS_8U;

if( !srcImage )
CV_ERROR( CV_StsNullPtr, "" );

if( srcImage->nChannels != 1 )
CV_ERROR( CV_BadNumChannels, "Number of channels must be 1");

if( srcImage->depth != IPL_DEPTH_8U )
CV_ERROR( CV_BadDepth, "Depth must be equal IPL_DEPTH_8U");

// no Directions provided, use the default ones - 0 deg, 45, 90, 135
if( !srcStepDirections )
{
srcStepDirections = defaultStepDirections;
}

//CV_CALL( stepDirections = (int*)cvAlloc(numStepDirections*2*sizeof(stepDirections[0])));
CV_CALL( stepDirections = new int
[numStepDirections*2*sizeof(stepDirections[0])]);
memcpy( stepDirections, srcStepDirections,
numStepDirections*2*sizeof(stepDirections[0]));

cvGetImageRawData( srcImage, &srcImageData, &srcImageStep, &srcImageSize );

// roll together Directions and magnitudes together with knowledge of image (step)
// CV_CALL( memorySteps = (int*)cvAlloc(numStepDirections*sizeof(memorySteps[0])));
CV_CALL( memorySteps = new int [ numStepDirections*sizeof(memorySteps[0]) ]);

for( stepLoop = 0; stepLoop < numStepDirections; stepLoop++ )
{
stepDirections[stepLoop*2 + 0] *= stepMagnitude;
stepDirections[stepLoop*2 + 1] *= stepMagnitude;

memorySteps[stepLoop] = stepDirections[stepLoop*2 + 0]*srcImageStep +
stepDirections[stepLoop*2 + 1];
}

//CV_CALL( newGLCM = (CvGLCM*)cvAlloc(sizeof(newGLCM)));
CV_CALL( newGLCM = new CvGLCM [sizeof(newGLCM)] );
memset( newGLCM, 0, sizeof(newGLCM) );

newGLCM->matrices = 0;
newGLCM->numMatrices = numStepDirections;
newGLCM->optimizationType = optimizationType;

if( optimizationType <= CV_GLCM_OPTIMIZATION_LUT )
{
int lookupTableLoop, imageColLoop, imageRowLoop, lineOffset = 0;

// if optimization type is set to lut, then make one for the image
if( optimizationType == CV_GLCM_OPTIMIZATION_LUT )
{
for( imageRowLoop = 0; imageRowLoop < srcImageSize.height;
imageRowLoop++, lineOffset += srcImageStep )
{
for( imageColLoop = 0; imageColLoop < srcImageSize.width;
imageColLoop++ )
{

newGLCM->forwardLookupTable[srcImageData[lineOffset+imageColLoop]]=1;
}
}

newGLCM->numLookupTableElements = 0;

for( lookupTableLoop = 0; lookupTableLoop < maxNumGreyLevels8u;
lookupTableLoop++ )
{
if( newGLCM->forwardLookupTable[ lookupTableLoop ] != 0 )
{
newGLCM->forwardLookupTable[ lookupTableLoop ] =
newGLCM->numLookupTableElements;
newGLCM->reverseLookupTable[ newGLCM->numLookupTableElements
] =
lookupTableLoop;

newGLCM->numLookupTableElements++;
}
}
}
// otherwise make a "LUT" which contains all the gray-levels (forcode-reuse)
else if( optimizationType == CV_GLCM_OPTIMIZATION_NONE )
{
for( lookupTableLoop = 0; lookupTableLoop <maxNumGreyLevels8u;
lookupTableLoop++ )
{
newGLCM->forwardLookupTable[ lookupTableLoop ] =
lookupTableLoop;
newGLCM->reverseLookupTable[ lookupTableLoop ] =
lookupTableLoop;
}
newGLCM->numLookupTableElements = maxNumGreyLevels8u;
}

newGLCM->matrixSideLength = newGLCM->numLookupTableElements;
icv_CreateGLCM_LookupTable_8u_C1R( srcImageData, srcImageStep,srcImageSize,newGLCM, stepDirections,numStepDirections, memorySteps );
}
else if( optimizationType == CV_GLCM_OPTIMIZATION_HISTOGRAM )
{
CV_ERROR( CV_StsBadFlag, "Histogram-based method is not implemented" );

/* newGLCM->numMatrices *= 2;
newGLCM->matrixSideLength = maxNumGreyLevels8u*2;

icv_CreateGLCM_Histogram_8uC1R( srcImageStep, srcImageSize,
srcImageData,
newGLCM, numStepDirections,
stepDirections, memorySteps );
*/
}

__END__;

delete[] memorySteps;
delete[] stepDirections;

if( cvGetErrStatus() < 0 )
{
cvFree( &newGLCM );
}

return newGLCM;
}


void
cv_ReleaseGLCM( CvGLCM** GLCM, int flag )
{
CV_FUNCNAME( "cvReleaseGLCM" );

__BEGIN__;

int matrixLoop ...
(more)
edit flag offensive delete link more

Comments

Hey Alexander,

Thanks for your workaround. It works fine, but ends up in a memory-leak. I tried to fix it during the last 2 days, could handle the memory-leak but ended up in a wrong result. It seems that the cvReleaseGLCM doesn't work. I have no idea how to fix it and would be pleased if you could help.

Regards Robert.

rokos gravatar imagerokos ( 2014-11-25 12:50:40 -0600 )edit

I've tried to get entropy value from cvCreateGLCMDescritptors but since the cvCreateGLCM had a bug I can't resolve this. Solution from Alexander Shishkov is really working for my code. Thank you so much.

new camp gravatar imagenew camp ( 2015-02-13 23:59:46 -0600 )edit

Hey Alexander, I changed my texture.cpp file and I used your main function code. It giving the error as malloc(): memory corruption: 0x0000000000e0c320 * Aborted (core dumped) please can you help to fix this error

manjunath gravatar imagemanjunath ( 2015-08-06 12:21:32 -0600 )edit
0

answered 2014-09-01 15:46:09 -0600

Skuenstler gravatar image

Thank you very much - your answer works quite well ! Except that with your proposed solution I end up getting memory-leaks using "cvCreateGLCM" !!! What could be the mistake ? My C-skills are not sufficiant to find out more.

Reading link1 , link2 or link3, they all mention 3 additional bugs inside /modules/legacy/src/texture.cpp But none of the three links work for me neither without errors (maybe since they keep using cvAlloc - their code does not go through without an exception...).

Do you have any suggestion on how to get glcm running without memory leaks ? Please consult the three links in order to compare your solution with the 3 suggested bugs. Are they relevant ?

Many thanks for any help on this.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2012-06-27 15:09:25 -0600

Seen: 5,233 times

Last updated: Sep 01 '14