Ask Your Question

Revision history [back]

Strange memory exception (access violation) in running DBRIEF

Hi,

This isn't exactly related an existing OpenCV module, but I'm trying to run the DBREIF descriptor that is based on OpenCV. Unfortunately, the code uses the old C api.

Here's where I get the exception:

void Dbrief::getDbriefDescriptor(bitset<DESC_LEN>& desc, cv::KeyPoint kpt, IplImage* img)
{
    //  Calculate the width step of the boxSmoothedImage image
    int inWS = boxSmoothedImage->step / CV_ELEM_SIZE(boxSmoothedImage->type);
    //  Hold the pointer of the data part of boxSmoothedImage image matrix with "iD"
    float* iD = boxSmoothedImage->data.fl;
    // Hold the pointer to the top left corner of the patch to be analysed
    int X = kpt.pt.x - HALF_PATCH_SIZE,
        Y = kpt.pt.y - HALF_PATCH_SIZE;
    cv::Point2i point;
    float result;
    GET_MATRIX_DATA(iD, 488 + 32, 603 + 1, 765); /// HERE IS EXCEPTION
    /***********************************************

Here's the function that calls this function:

void Dbrief::getDbriefDescriptors(vector< bitset<DESC_LEN> >& descriptors,
    vector<cv::KeyPoint>& kpts,
    IplImage* img)
{
    // Make sure that input image contains only one color channel:
    assert(img->nChannels == 1);

    // Check whether all the keypoints are inside the subimage:
    assert(validateKeypoints(kpts, img->width, img->height));

    // If memory allocated in 'descriptors' is not enough, then resize it:
    descriptors.resize(kpts.size());

    // Allocate memory for the box smoothed image
    allocateBoxSmoothedImage(img);

    // Calculate the box smoothed image:
    cvSmooth(img, boxSmoothedImage, CV_BLUR_NO_SCALE, KERNEL_SIZE, KERNEL_SIZE);

    // Iterate over keypoints:
    for (unsigned int i = 0; i < kpts.size(); ++i){
        printf("%d\n", i);
        getDbriefDescriptor(descriptors[i], kpts[i], img);
    }
}

Now, in the first 24 times the function "getDbriefDescriptor(descriptors[i], kpts[i], img);" is called, there is no exception, only in the 24'th time. The size of boxSmoothedImage is width=765 and height = 512.

I'm stumped. Why an exception rises in the exact same command, only in the 24'th time the function is called? It has to do with something related to memory management, but I'm not sure what.

Any help will be greatly appreciated.

Thanks,

Gil

Strange memory exception (access violation) in running DBRIEF

Hi,

This isn't exactly related an existing OpenCV module, but I'm trying to run the DBREIF descriptor that is based on OpenCV. Unfortunately, the code uses the old C api.

Here's where I get the exception:

void Dbrief::getDbriefDescriptor(bitset<DESC_LEN>& desc, cv::KeyPoint kpt, IplImage* img)
{
    //  Calculate the width step of the boxSmoothedImage image
    int inWS = boxSmoothedImage->step / CV_ELEM_SIZE(boxSmoothedImage->type);
    //  Hold the pointer of the data part of boxSmoothedImage image matrix with "iD"
    float* iD = boxSmoothedImage->data.fl;
    // Hold the pointer to the top left corner of the patch to be analysed
    int X = kpt.pt.x - HALF_PATCH_SIZE,
        Y = kpt.pt.y - HALF_PATCH_SIZE;
    cv::Point2i point;
    float result;
    GET_MATRIX_DATA(iD, 488 + 32, 603 + 1, 765); /// HERE IS EXCEPTION
    /***********************************************

Here's the function that calls this function:

void Dbrief::getDbriefDescriptors(vector< bitset<DESC_LEN> >& descriptors,
    vector<cv::KeyPoint>& kpts,
    IplImage* img)
{
    // Make sure that input image contains only one color channel:
    assert(img->nChannels == 1);

    // Check whether all the keypoints are inside the subimage:
    assert(validateKeypoints(kpts, img->width, img->height));

    // If memory allocated in 'descriptors' is not enough, then resize it:
    descriptors.resize(kpts.size());

    // Allocate memory for the box smoothed image
    allocateBoxSmoothedImage(img);

    // Calculate the box smoothed image:
    cvSmooth(img, boxSmoothedImage, CV_BLUR_NO_SCALE, KERNEL_SIZE, KERNEL_SIZE);

    // Iterate over keypoints:
    for (unsigned int i = 0; i < kpts.size(); ++i){
        printf("%d\n", i);
        getDbriefDescriptor(descriptors[i], kpts[i], img);
    }
}

Now, in the first 24 times the function "getDbriefDescriptor(descriptors[i], kpts[i], img);" is called, there is no exception, only in the 24'th time. The size of boxSmoothedImage is width=765 and height = 512.

I'm stumped. Why an exception rises in the exact same command, only in the 24'th time the function is called? It has to do with something related to memory management, but I'm not sure what.

Any help will be greatly appreciated.

Thanks,

Gil

EDIT:

Thanks berak and Mathieu! you're the best.

Here is the original source code:

http://cvlab.epfl.ch/research/detect/dbrief

Now, originally, the source code is:

void Dbrief::getDbriefDescriptor(bitset<DESC_LEN>& desc, cv::KeyPoint kpt, IplImage* img)
{
    //  Calculate the width step of the boxSmoothedImage image
    int inWS = boxSmoothedImage->step / CV_ELEM_SIZE(boxSmoothedImage->type);
    //  Hold the pointer of the data part of boxSmoothedImage image matrix with "iD"
    float* iD = boxSmoothedImage->data.fl;
    // Hold the pointer to the top left corner of the patch to be analysed
    int X = kpt.pt.x - HALF_PATCH_SIZE,
        Y = kpt.pt.y - HALF_PATCH_SIZE;
    cv::Point2i point;
    float result;
    GET_MATRIX_DATA(iD, 488 + 32, 603 + 1, 765);
    /***********************************************
                     Projection: 1

    ************************************************/
    result = 0.f;
    result += GET_MATRIX_DATA(iD, Y + 1, X + 1, inWS) * (0.16649845f);
    result += GET_MATRIX_DATA(iD, Y + 32, X + 2, inWS) * (0.11555521f);
    result += GET_MATRIX_DATA(iD, Y + 1, X + 3, inWS) * (0.07356358f);

followed by many lines of the form:

result += GET_MATRIX_DATA(iD, Y + 1, X + 3, inWS) * (0.07356358f);

with different parameters.

Now, I get an exception in the 24'th time the function "getDbriefDescriptor" is called (not the 24'th projection, the 24'th descriptor) in the line:

result += GET_MATRIX_DATA(iD, Y + 32, X + 2, inWS) * (0.11555521f);

The value of Y is 488 and the value of X is 603, so I just copied the line with those specific values to make it simpler.

Also, the definition of GET_MATRIX_DATA is the following:

// Returns pd[row][column]
  inline int GET_MATRIX_DATA(const float* pD, const int row, int column, const int wS)
  {
   return *(pD + (row * wS) + column);
  }

Again, thanks in advance!

Gil