1

I have a m-file in MATLAB.It calulates normxcorr2 between an image and a template as follow:

function norm_res = TMatch(temp,image)
    c = normxcorr2(temp,image);
    norm_res = max(c(:));

when I running this m-file from MATLAB command line with two images as image and template, it gives me 0.8076 as a result. Now I build it as a shared library with following command:

mcc -B cpplib:TMatch TMatch.m

in MATLAB command line and it generates required files for using in C++.

Now I use generated shared library in visual studio as following:

    mclInitializaApplication();
    TMatchInitialize();
    Mat img = imread("image.jpg", IMREAD_GRAYSCALE);
    Mat tmp = imread("temp.jpg", IMREAD_GRAYSCALE);
    // these files are the same as files which are used in MATLAB

    mwArray mwImg(img.rows,img.cols,mxDOUBLE_CLASS,mxREAL);
    mwArray mwTemp(tmp.rows,tmp.cols,mxDOUBLE_CLASS,mxREAL);

    mwArray result;

    mwImg.SetData(img.data,img.cols * img.rows);
    mwTemp.SetData(tmp.data,tmp.cols * tmp.rows);

    TMatch(1,result,mwTemp,mwImg);

    double normed_res;
    result.GetData(&normed_res); //finally result will be 0.198949

As you can see when I run m-file in MATLAB result will be:

0.8076 

and when I generate a shared library and use it in visual studio C++ the result will be:

0.198949

please tell me why this happens?
Thanks in advance.
(@Miki @A.Riazi)

1 Answer 1

1

The reason is that MATLAB and VC++/OpenCV use different memory management strategies for matrices, where MATLAB is column-major while row-major for VC++/OpenCV.

Try to transpose the matrix in MATLAB or in VC++/OpenCV before calling MATLAB.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm so grateful for your answer @herohuyongtao. I add following lines of codes to my VC++ program: Mat t_img, t_tmp; traspose(img,t_img); traspose(tmp,t_temp); and run the previous code except replace "img" and "tmp" with "t_img" and "t_tmp" and run again but nothing changed and I recieve previous answer again (0.198949). I tried my MATLAB code with following lines: temp = transpose(temp); image = transpose(image); and again I do "normxcorr2", in this case again I recieve previous answer (0.8076 ) and nothing changed. Can you explain to me if I do something wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.