From 603d94935463bf630b11d5729e8bf0521bdc95c8 Mon Sep 17 00:00:00 2001 From: hbristow Date: Fri, 12 Jul 2013 15:48:03 +1000 Subject: [PATCH] Improved string handling. Swapped transpose interface for slow but correct implementation. Will come back to optimized implementation later. imshow now works correctly --- modules/matlab/include/mxarray.hpp | 45 ++++++++++++++++++++-------- modules/matlab/include/transpose.hpp | 1 + 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/modules/matlab/include/mxarray.hpp b/modules/matlab/include/mxarray.hpp index f34c2f6d38..655286b096 100644 --- a/modules/matlab/include/mxarray.hpp +++ b/modules/matlab/include/mxarray.hpp @@ -411,20 +411,19 @@ public: const Scalar* imag() const { return static_cast(mxGetData(ptr_)); } template - Scalar scalar() const { return static_cast(mxGetData(ptr_))[0]; } + Scalar scalar() const { return static_cast(mxGetData(ptr_))[0]; } std::string toString() const { conditionalError(isString(), "Attempted to convert non-string type to string"); - std::string str; - str.reserve(size()+1); + std::string str(size()+1, '\0'); mxGetString(ptr_, const_cast(str.data()), str.size()); - mexPrintf(str.c_str()); + mexPrintf("string: %s\n", str.c_str()); return str; } size_t size() const { return mxGetNumberOfElements(ptr_); } - size_t rows() const { return mxGetM(ptr_); } - size_t cols() const { return mxGetN(ptr_); } + size_t rows() const { return mxGetDimensions(ptr_)[0]; } + size_t cols() const { return mxGetDimensions(ptr_)[1]; } size_t channels() const { return (mxGetNumberOfDimensions(ptr_) > 2) ? mxGetDimensions(ptr_)[2] : 1; } bool isComplex() const { return mxIsComplex(ptr_); } bool isNumeric() const { return mxIsNumeric(ptr_); } @@ -475,7 +474,7 @@ template <> cv::Mat MxArray::toMat() const { switch (ID()) { case mxINT8_CLASS: return toMat(); - case mxUINT8_CLASS: return toMat();; + case mxUINT8_CLASS: return toMat(); case mxINT16_CLASS: return toMat(); case mxUINT16_CLASS: return toMat(); case mxINT32_CLASS: return toMat(); @@ -503,9 +502,18 @@ void deepCopyAndTranspose(const cv::Mat& in, MxArray& out) { conditionalError(static_cast(in.rows) == out.rows(), "Matrices must have the same number of rows"); conditionalError(static_cast(in.cols) == out.cols(), "Matrices must have the same number of cols"); conditionalError(static_cast(in.channels()) == out.channels(), "Matrices must have the same number of channels"); - const InputScalar* inp = in.ptr(0); - OutputScalar* outp = out.real(); - gemt('R', out.rows(), out.cols(), inp, in.step1(), outp, out.rows()); + std::vector channels; + cv::split(in, channels); + for (size_t c = 0; c < out.channels(); ++c) { + cv::transpose(channels[c], channels[c]); + cv::Mat outmat(out.cols(), out.rows(), cv::DataType::type, + static_cast(out.real() + out.cols()*out.rows()*c)); + channels[c].convertTo(outmat, cv::DataType::type); + } + + //const InputScalar* inp = in.ptr(0); + //OutputScalar* outp = out.real(); + //gemt('R', out.rows(), out.cols(), inp, in.step1(), outp, out.rows()); } template @@ -513,9 +521,20 @@ void deepCopyAndTranspose(const MxArray& in, cv::Mat& out) { conditionalError(in.rows() == static_cast(out.rows), "Matrices must have the same number of rows"); conditionalError(in.cols() == static_cast(out.cols), "Matrices must have the same number of cols"); conditionalError(in.channels() == static_cast(out.channels()), "Matrices must have the same number of channels"); - const InputScalar* inp = in.real(); - OutputScalar* outp = out.ptr(0); - gemt('C', in.rows(), in.cols(), inp, in.rows(), outp, out.step1()); + std::vector channels; + for (size_t c = 0; c < in.channels(); ++c) { + cv::Mat outmat; + cv::Mat inmat(in.cols(), in.rows(), cv::DataType::type, + static_cast(const_cast(in.real() + in.cols()*in.rows()*c))); + inmat.convertTo(outmat, cv::DataType::type); + cv::transpose(outmat, outmat); + channels.push_back(outmat); + } + cv::merge(channels, out); + + //const InputScalar* inp = in.real(); + //OutputScalar* outp = out.ptr(0); + //gemt('C', in.rows(), in.cols(), inp, in.rows(), outp, out.step1()); } diff --git a/modules/matlab/include/transpose.hpp b/modules/matlab/include/transpose.hpp index 2a820f43f9..14647984bd 100644 --- a/modules/matlab/include/transpose.hpp +++ b/modules/matlab/include/transpose.hpp @@ -35,6 +35,7 @@ void transpose4x4(const InputScalar* src, size_t lda, OutputScalar* dst, size_t */ template void gemt(const char major, const size_t M, const size_t N, const InputScalar* a, size_t lda, OutputScalar* b, size_t ldb) { + // 1x1 transpose is just copy if (M == 1 && N == 1) { *b = *a; return; }