问题
Iam currently using the Mex C++ API to take a picture with a Basler camera (Pylon API) and send it back to Matlab. I have some problems with converting an opencv cv::mat data type to a suitable type for Matlab. The solutions provided by Matlab (https://de.mathworks.com/help/vision/ug/opencv-interface.html) for converting opencv data types unfortunately only work with the older mex c api, so I cannot use them.
I have managed the following workaround. This workaround uses two loops two assign the values to a matlab::data::TypedArray. This solution is very slow due to the two loops. Can someone recommend another way which is faster or works without loops? See the extract code from my c++ mex file with ne mex c++ API:
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <pylon/PylonIncludes.h>
#include <pylon/usb/PylonUsbIncludes.h>
#include <pylon/usb/BaslerUsbInstantCamera.h>
#include <pylon/PylonUtilityIncludes.h>
#include "mex.hpp"
#include "mexAdapter.hpp"
#include <chrono>
#include <string>
class MexFunction : public matlab::mex::Function{
public:
MexFunction(){}
void operator()(ArgumentList outputs, ArgumentList inputs) {
CGrabResultPtr ptrGrabResult
... Here I take a picture with the camera and save it to "ptrGrabResult"...
Mat openCvImage;
CImageFormatConverter formatConverter;
CPylonImage pylonImage;
// First convert the "ptrGrabResult" to a pylonImage
formatConverter.Convert(pylonImage, ptrGrabResult);
// Convert it to a openc picture
Mat openCvImage = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC1,(uint8_t *)pylonImage.GetBuffer(), Mat::AUTO_STEP);
const size_t rows = openCvImage.rows;
const size_t cols = openCvImage.cols;
matlab::data::TypedArray<uint8_t> Yp = factory.createArray<uint8_t>({ rows, cols });
for(int i = 0 ;i < openCvImage.rows; ++i){
for(int j = 0; j < openCvImage.cols; ++j){
// [Row][Coloumn] = (row, column)
Yp[i][j] = openCvImage.at<uint8_t>(i,j);;
}
}
outputs[0] = Yp;
}
}
The problem with this solution is that it takes way too long for me. Is there a faster and convenient method to cast the cv:mat type without a loop?? Or maybe it is possible to use pointer in this case, but I was not able to implement this...
Has anyone a suggestion?
来源:https://stackoverflow.com/questions/58267677/how-to-return-a-opencv-image-cvmat-to-matlab-with-the-mex-c-api