Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

cv::Point_<T> always gets converted to mxDOUBLE_CLASS mxArray #66

Closed
amroamroamro opened this issue Mar 22, 2013 · 2 comments
Closed

cv::Point_<T> always gets converted to mxDOUBLE_CLASS mxArray #66

amroamroamro opened this issue Mar 22, 2013 · 2 comments
Labels

Comments

@amroamroamro
Copy link
Collaborator

In the current implementation of MxArray, when passing some of the templated OpenCV data types back to MATLAB from a MEX file, the conversion ignores the template type and creates a double array no matter what. Example:

create_point.cpp

#include "mexopencv.hpp"
using namespace std;
using namespace cv;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    Point_<int> p(1,2);
    plhs[0] = MxArray(p);
}

MATLAB

>> p = create_point();
>> class(p)      % this is double

This implicit conversion can show up in unexpected places.. For example cv.approxPolyDP that takes a vector of int points, returns the approximated points as type double.

From what I can tell, this applies to Point_<T>, Point3_<T>, Size_<T>, Rect_<T>, Scalar_<T>, and possibly other places...

Do you think this should be fixed, or is this by design (possibly because some code depends on it somewhere else)?

Here is what I had in mind to fix the constructor:

template <typename T>
MxArray::MxArray(const cv::Point_<T>& p)
{
    p_ = mxCreateNumericMatrix(1, 2, ClassIDOf[DataType<T>::type], mxREAL);
    T *x = static_cast<T*>(mxGetData(p_));
    x[0] = p.x;
    x[1] = p.y;
}

where ClassIDOf is the same map defined in MxArray.cpp that translates opencv types to matlab types

@kyamagu
Copy link
Owner

kyamagu commented Mar 22, 2013

Yes, this is by design. The reason is that the default numeric type is double in matlab, and also integer arithmetic in matlab sucks. In my opinion, returning integer types is benefitial only for large arrays, such as pixels. Other than that, it's probably better to return values as double.

@amroamroamro
Copy link
Collaborator Author

alright got it, thanks Kota

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants