Search code examples
c++opencvstructobjective-c++surf

OpenCV / SURF tracking for iPhone: Wrong values when creating struct


I'm using the OpenCV library in Objective-C (cross-compiled), which works fine basically.

Unfortunately, when using the example which can be found in find_obj.cpp, I get strange values for the CvSURFParams that has to be passed as an argument to the cvExtractSURF function.

features2d.hpp (part of openCV):

typedef struct CvSURFParams
{
    int    extended;
    double hessianThreshold;

    int    nOctaves;
    int    nOctaveLayers;

} CvSURFParams;

surf.cpp (part of openCV):

CvSURFParams cvSURFParams(double threshold, int extended)
{
    CvSURFParams params;
    params.hessianThreshold = threshold;
    params.extended = extended;
    params.upright = 0;
    params.nOctaves = 4;
    params.nOctaveLayers = 2;
    return params;
}

ViewController.mm (my main view controller):

CvSURFParams params = cvSURFParams(500, 1);

After calling the cvSURFParams function in ViewController.mm, the valus of params are:

extended         = (int)    1
hessianThreshold = (double) 0
nOctaves         = (int)    1082081280
nOctaveLayers    = (int)    4

Correcting the values for hessianThreshold, nOctaves, .. in ViewController.mm doesn't help, as soon as I pass params to my cvExtractSURF function, the wrong values appear when debugging this function in surf.cpp:

extended         = (int)    1
upright          = (int)    0
hessianThreshold = (double) 500
nOctaves         = (int)    4
nOctaveLayers    = (int)    0

Can anyone help please?

Thanks in advance,

-- Stephan


Solution

  • It sounds like the representation of the structure in OpenCV is different from how your ViewController.mm represents it. Either hessianThreshold is a different size, or else there is an alignment problem. Some things to check:

    1. Output sizeof(CvSURFParams) in your ViewController.mm as some debug code. Also do this from the OpenCV library. Most likely they are not the same.
    2. You can also output sizeof(double) in both projects. They should be the same, if not then you found the problem and you'll need to instruct the compiler to use the same sizeof(double) in both projects.
    3. If sizeof(double) is the same in both projects, then the problem appears to be that the compiler is aligning the structures differently in each project. Again, this is most likely compiler settings, although typically there are #pragma instructions that can be used to specify a particular alignment scheme if you don't want the default.