diff --git a/modules/matlab/CMakeLists.txt b/modules/matlab/CMakeLists.txt index 0f394c61d..698644f51 100644 --- a/modules/matlab/CMakeLists.txt +++ b/modules/matlab/CMakeLists.txt @@ -1,5 +1,3 @@ -set(BUILD_opencv_matlab_INIT OFF) # Matlab module is broken - # ---------------------------------------------------------------------------- # CMake file for Matlab/Octave support # diff --git a/modules/matlab/include/opencv2/matlab/bridge.hpp b/modules/matlab/include/opencv2/matlab/bridge.hpp index be4ff689c..d3342c16a 100644 --- a/modules/matlab/include/opencv2/matlab/bridge.hpp +++ b/modules/matlab/include/opencv2/matlab/bridge.hpp @@ -249,13 +249,13 @@ public: case mxUINT16_CLASS: deepCopyAndTranspose(ptr_, mat); break; case mxINT32_CLASS: deepCopyAndTranspose(ptr_, mat); break; case mxUINT32_CLASS: deepCopyAndTranspose(ptr_, mat); break; - case mxINT64_CLASS: deepCopyAndTranspose(ptr_, mat); break; - case mxUINT64_CLASS: deepCopyAndTranspose(ptr_, mat); break; + //case mxINT64_CLASS: deepCopyAndTranspose(ptr_, mat); break; + //case mxUINT64_CLASS: deepCopyAndTranspose(ptr_, mat); break; case mxSINGLE_CLASS: deepCopyAndTranspose(ptr_, mat); break; case mxDOUBLE_CLASS: deepCopyAndTranspose(ptr_, mat); break; case mxCHAR_CLASS: deepCopyAndTranspose(ptr_, mat); break; case mxLOGICAL_CLASS: deepCopyAndTranspose(ptr_, mat); break; - default: matlab::error("Attempted to convert from unknown class"); + default: matlab::error("Attempted to convert from unknown/unsupported class"); } return mat; } @@ -576,13 +576,13 @@ cv::Mat Bridge::toMat() const { case mxUINT16_CLASS: return toMat(); case mxINT32_CLASS: return toMat(); case mxUINT32_CLASS: return toMat(); - case mxINT64_CLASS: return toMat(); - case mxUINT64_CLASS: return toMat(); + //case mxINT64_CLASS: return toMat(); + //case mxUINT64_CLASS: return toMat(); case mxSINGLE_CLASS: return toMat(); case mxDOUBLE_CLASS: return toMat(); //NOTE: OpenCV uses float as native type! case mxCHAR_CLASS: return toMat(); case mxLOGICAL_CLASS: return toMat(); - default: matlab::error("Attempted to convert from unknown class"); + default: matlab::error("Attempted to convert from unknown/unsuported class"); } return cv::Mat(); } @@ -598,18 +598,26 @@ cv::Mat Bridge::toMat() const { return toMat(); } template void deepCopyAndTranspose(const cv::Mat& in, matlab::MxArray& out) { + const int cols = out.cols(); + const int rows = out.rows(); matlab::conditionalError(static_cast(in.rows) == out.rows(), "Matrices must have the same number of rows"); matlab::conditionalError(static_cast(in.cols) == out.cols(), "Matrices must have the same number of cols"); matlab::conditionalError(static_cast(in.channels()) == out.channels(), "Matrices must have the same number of channels"); 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); + cv::Mat_ m; + cv::transpose(channels[c], m); + OutputScalar* dst_plane = (OutputScalar*)out.real() + cols*rows*c; + for (int x = 0; x < cols; x++) + { + OutputScalar* dst_col = dst_plane + x * rows; + for (int y = 0; y < rows; y++) + { + dst_col[y] = cv::saturate_cast(m(y, x)); + } + } } - //const InputScalar* inp = in.ptr(0); //OutputScalar* outp = out.real(); //gemt('R', out.rows(), out.cols(), inp, in.step1(), outp, out.rows()); @@ -617,17 +625,25 @@ void deepCopyAndTranspose(const cv::Mat& in, matlab::MxArray& out) { template void deepCopyAndTranspose(const matlab::MxArray& in, cv::Mat& out) { + const int cols = in.cols(); + const int rows = in.rows(); matlab::conditionalError(in.rows() == static_cast(out.rows), "Matrices must have the same number of rows"); matlab::conditionalError(in.cols() == static_cast(out.cols), "Matrices must have the same number of cols"); matlab::conditionalError(in.channels() == static_cast(out.channels()), "Matrices must have the same number of channels"); 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::Mat_ m(cols, rows); + const InputScalar* src_plane = in.real() + cols*rows*c; + for (int x = 0; x < cols; x++) + { + const InputScalar* src_col = src_plane + x * rows; + for (int y = 0; y < rows; y++) + { + m(y, x) = cv::saturate_cast(src_col[y]); + } + } + cv::transpose(m, m); + channels.push_back(m); } cv::merge(channels, out);