What could alter the behavior of a c++ library in a mex function ?

2 vues (au cours des 30 derniers jours)
Matt
Matt le 14 Sep 2022
Réponse apportée : Matt le 24 Mai 2024
Hello,
I am trying to interface a camera to matlab in a context of development of a microscope. The idea is to control it (change parameters like exposure time/frame rate/etc and of course acquire images) directly from the Matlab environnement.
The camera comes with a developement kit in c++ that works perfectly. I would like to wrap those c++ functions in a mex function that I could call from matlab in order to control the camera directly from Matlab where I intend to manipulate the images/instrument more or less in real time.
So I first wrote some code in c++ using the dedicated camera provider library and exempled functions, compiled it, and tested it. And it works great! For exemple the following code asks the camera on how many bites are coded each pixel of the images the camera stream. The answer is suppose to be 8 or 12 bites/pixel) and indeed when compiling/calling this function I can read "PixelFormat: Mono8,Mono12,Mono12Packed" in the terminal.
#include <EmergentCameraAPIs.h>
#include <iostream>
#define SUCCESS 0
#define MAX_CAMERAS 1
void Camera_connection_routine(Emergent::CEmergentCamera* camera);
int main(int argc, char *argv[])
{
// Camera connection
Emergent::CEmergentCamera camera;
Camera_connection_routine(&camera);
// Using a function from the library to do something with the camera
const unsigned long enumBufferSize = 1000;
char enumBuffer[enumBufferSize];
unsigned long enumBufferSizeReturn = 0;
Emergent::EVT_CameraGetEnumParamRange(&camera, "PixelFormat", enumBuffer, enumBufferSize, &enumBufferSizeReturn);
std::cout << "PixelFormat: " << enumBuffer << "\n";
};
But if I put this code in mex format and compile it in Matlab I get another answer : not "PixelFormat: Mono8,Mono12,Mono12Packed"" as I should but "PixelFormat: Mono8,Mono10,Mono10Packed," !
More generally the majority of functions from the library work as they should in the matlab environnement but some do not work as expected sending different results then the one I get with the cpp function compiled with g++ in the terminal.
What could be the cause of this discrepancy ? What can I be doing that alters the way the library works/is called ?
How I compile the mex function (no error or warnign message during compilation) :
header_path = ['-I' '/private/interface_camera/include'];
lib_path = ['-L' '/stillprivate/interface_camera/lib'];
mex(header_path,lib_path,'-lEmergentCamera','mex_exemple4matlab_community.cpp')
How I wrap in a mex :
#include "mex.hpp"
#include "mexAdapter.hpp"
#include <EmergentCameraAPIs.h>
#define SUCCESS 0
#define MAX_CAMERAS 1
void Camera_connection_routine(Emergent::CEmergentCamera* camera);
class MexFunction : public matlab::mex::Function {
public:
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
// Camera connection
Emergent::CEmergentCamera camera;
Camera_connection_routine(&camera);
// Using a function from the library to do something with the camera
const unsigned long enumBufferSize = 1000;
char enumBuffer[enumBufferSize];
unsigned long enumBufferSizeReturn = 0;
Emergent::EVT_CameraGetEnumParamRange(&camera, "PixelFormat", enumBuffer, enumBufferSize, &enumBufferSizeReturn);
std::cout << "PixelFormat: " << enumBuffer << "\n";
}
};
Thank you very much !
  9 commentaires
Matt
Matt le 20 Sep 2022
Modifié(e) : Matt le 20 Sep 2022
About the idea to compile in C, it does not work as the library declares at some point this :
#include <list>
And I can't find a equivalent in C.
Matt
Matt le 1 Fév 2023
Déplacé(e) : Jan le 1 Fév 2023
I am upping this question.
Even Chatgpt could not help me. Who wants to beat the ai ?

Connectez-vous pour commenter.

Réponse acceptée

Matt
Matt le 24 Mai 2024
This question was almost two years ago, but I came back to it and find out that evaluating my mex function in another process using the function mexhost() and feval() solved the issue.
host_process = mexhost;
[out]=feval(host_process,'my_mex_fun',param);
However I have no clue why this solved the issue.
On this page https://fr.mathworks.com/help/matlab/matlab_external/out-of-process-execution-of-c-mex-functions.html I can see that out of process execution enable "Use some third-party libraries in the C++ MEX function that are not compatible with MATLAB." But it not clear to me why some third party lib could or not compatible with matlab. If somebody knows, I am interested.

Plus de réponses (0)

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by