Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed extra MxTypes

  • Loading branch information...
commit c1156af1c340e64afbb965fc9f45b310de9063b3 1 parent 05635fc
@kyamagu authored
Showing with 7 additions and 7 deletions.
  1. +5 −5 include/MxArray.hpp
  2. +2 −2 src/MxArray.cpp
View
10 include/MxArray.hpp
@@ -197,7 +197,7 @@ class MxArray
* @param v vector of type T.
*/
template <typename T> explicit MxArray(const std::vector<T>& v) {
- fromVector<T>(v, MxTypes<T>());
+ fromVector<T>(v);
}
/** MxArray constructor from cv::Point_<T>.
* @param p cv::Point_<T> object.
@@ -648,7 +648,7 @@ class MxArray
/** Internal std::vector converter.
*/
template <typename T>
- void fromVector(const std::vector<T>& v, MxTypes<T>);
+ void fromVector(const std::vector<T>& v);
/** mxArray c object.
*/
const mxArray* p_;
@@ -697,7 +697,7 @@ class ConstMap
};
template <typename T>
-void MxArray::fromVector(const std::vector<T>& v, MxTypes<T> type)
+void MxArray::fromVector(const std::vector<T>& v)
{
if (MxTypes<T>::type == mxUNKNOWN_CLASS) {
p_ = mxCreateCellMatrix(1, v.size());
@@ -992,12 +992,12 @@ void MxArray::set(const std::string& fieldName, const T& value, mwIndex index)
/** Specialization for vector<char> construction.
*/
template<>
-void MxArray::fromVector(const std::vector<char>& v, MxTypes<char> type);
+void MxArray::fromVector(const std::vector<char>& v);
/** Specialization for vector<bool> construction.
*/
template<>
-void MxArray::fromVector(const std::vector<bool>& v, MxTypes<bool> type);
+void MxArray::fromVector(const std::vector<bool>& v);
/** MxArray constructor from vector<DMatch>. Make a cell array.
* @param v vector of type DMatch.
View
4 src/MxArray.cpp
@@ -229,7 +229,7 @@ MxArray::MxArray(const cv::KeyPoint& p) :
}
template<>
-void MxArray::fromVector(const std::vector<char>& v, MxTypes<char> type)
+void MxArray::fromVector(const std::vector<char>& v)
{
mwSize size[] = {1, v.size()};
p_ = mxCreateCharArray(2, size);
@@ -239,7 +239,7 @@ void MxArray::fromVector(const std::vector<char>& v, MxTypes<char> type)
}
template<>
-void MxArray::fromVector(const std::vector<bool>& v, MxTypes<bool> type)
+void MxArray::fromVector(const std::vector<bool>& v)
{
p_ = mxCreateLogicalMatrix(1, v.size());
if (!p_)

7 comments on commit c1156af

@sapus

Hi I wannted to present you a new idea of mine. The idea is to put some kind of pixels rappresentation of the values. For this I use the stl arrays container(fixed side arrays). And I add a new trait for that kind of data:

/** array traits
 */
template<typename T,std::wint_t N> struct MxTypes < std::array<T,N> >
{
    static const int type=99;//don t have any good idea for this value this is just random
    typedef T array_type;
};

now I just added the new method fromarray (the same style as fromvector):

    template <typename T, std::size_t Size> 
    void fromArray(const std::array<T,Size>(&a));

and to this the mxarray constructor:

    /** MxArray constructor from array. Make a numeric or cell array.
     * @param v vector of type T.
     */
    template <typename T, std::size_t Size> explicit MxArray(const std::array<T,Size>(&a)) {
        fromArray<T, Size>(a);
    }

fromarray have the the following definition:

template <typename T, std::size_t Size> 
void MxArray::fromArray(const std::array<T,Size>(&a))
{
    if (MxTypes<T>::type == mxUNKNOWN_CLASS) {
        p_ = mxCreateCellMatrix(1, Size);
        if (!p_)
            mexErrMsgIdAndTxt("mexopencv:error", "Allocation error");
        for (int i = 0; i < Size; ++i)
            mxSetCell(const_cast<mxArray*>(p_), i, MxArray(a[i]));
    } else {
        p_ = mxCreateNumericMatrix(1, Size, MxTypes<T>::type, mxREAL);
        if (!p_)
            mexErrMsgIdAndTxt("mexopencv:error", "Allocation error");
        std::copy(&a[0], &a[Size], reinterpret_cast<T*>(mxGetData(p_)));
    }
}

From here everything works I have allready tested it. No difference to vector here.
The main point is now to use std::array to build contiguos arrays in matlab automatically. For this reason I have than changed fromvector this way:

template <typename T>
void MxArray::fromVector(const std::vector<T>& v)
{
    if (MxTypes<T>::type == mxUNKNOWN_CLASS) {
        p_ = mxCreateCellMatrix(1, v.size());
        if (!p_)
            mexErrMsgIdAndTxt("mexopencv:error", "Allocation error");
        for (int i = 0; i < v.size(); ++i)
            mxSetCell(const_cast<mxArray*>(p_), i, MxArray(v[i]));
    } 
    else 
    {
        if (MxTypes<T>::type == 99) //It is vector of array !!!!
        {
            p_ = mxCreateNumericMatrix(v.size(),v[0].size(),MxTypes<MxTypes<T>::array_type>::type,mxREAL);//pixel are save in rows
            std::copy(&v[0][0], &v[v.size()-1][v[0].size()], reinterpret_cast<MxTypes<T>::array_type*>(mxGetData(p_)));
        }
        else
        {
        p_ = mxCreateNumericMatrix(1, v.size(), MxTypes<T>::type, mxREAL);
        if (!p_)
            mexErrMsgIdAndTxt("mexopencv:error", "Allocation error");
        std::copy(v.begin(), v.end(), reinterpret_cast<T*>(mxGetData(p_)));
        }
    }
}

This part of code is not tested. I cannot test it now. But I just wannted to share it.
This should work this way:

<?c++
std::vector<std::array<short,3> > mydata;(4)//pixel with 3 short pixels
MxArray(mydata);//now put them in matlab
?>

and give the following result in matlab

<?matlab
size(mydata)
[3 4]%3 unsigned short  and pixel 4 times
?>

This way you could reshape vector and use the pixel rappresentation you wann. And this without cells.

@kyamagu
Owner

I personally don't think this is particularly useful to the mexopencv use case, since people use cv::Mat rather than STL containers to access pixels or ND array... Please keep in mind that the primary goal of MxArray is to provide conversion between Matlab and OpenCV types, though you are free to modify the code for yourself.

@sapus
@kyamagu
Owner

You have a plenty of OpenCV containers for various needs: cv::Mat for a generic ND array or pixels, cv::Point for coordinates, cv::Vec for a vector, cv::Scalar for an array elements -- and in other cases, OpenCV just uses a vector of primitive types. Again, you're free to modify the code for yourself, but STL containers are not the main data format to develop an OpenCV application.

@sapus
@sapus

Hello I was thinking what you think to add a constructor like this ? :

    /** MxArray test constructor with cv::Vec<T,N>!!!!
     */
    template <typename T,int N> explicit MxArray(const cv::Vec<T,N>& p);

template <typename T,int N> 
MxArray::MxArray(const cv::Vec<T,N>& p):p_(mxCreateNumericMatrix(1, N, mxDOUBLE_CLASS,mxREAL))
{
    if (!p_)
        mexErrMsgIdAndTxt("mexopencv:error", "Allocation error");
    double *x = mxGetPr(p_);
    for (int i =0;i<N;i++)
        x[i]=p[i];

} ```

I can use that like this:
cv::vector<cv::Vec<double,2> > outarray(2);
outarray[0][0] =22;
outarray[0][1] =23;
outarray[1][0] =24;
outarray[1][1] =25;
// Convert cv::Mat to mxArray
plhs[0] = MxArray(outarray);
bye
@kyamagu
Owner

@sapus You are free to customize the code or fork and release your branch of mexopencv.

Please sign in to comment.
Something went wrong with that request. Please try again.