How to use OpenCV in a C++ S-Function?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I would like to use the OpenCV function cvtColor to change an image from RGB into Grayscale using a S-Function in Simulink. Like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163388/image.jpeg)
In order for the input to be used with OpenCV it needs to be converted into cv::Mat, I found that this can be done with the OpenCV Interface C++ API, but I do not know exactly how to do it.
My knowledge of S-Functions is limited but, as far as I know, the conversion needs to be done inside the mdlOutputs method. Something similar to this:
static void mdlOutputs(SimStruct *S, int_T tid)
{
const uint8_T *u0 = (const uint8_T*) ssGetInputPortSignal(S,0);
uint8_T *y0 = (uint8_T*) ssGetOutputPortRealSignal(S,0);
void ocvMxArrayToImage_uint8(const mxArray *u0, cv::Mat &colourImage);
cv::cvtColor(colourImage,grayImage,CV_RGB2GRAY);
y0 *ocvMxArrayFromImage_uint8(const cv::Mat &grayImage);
}
Am I on the right track? Do you know where can I found some examples of this?
Thank you
1 commentaire
Gonzalo Salinas
le 8 Fév 2018
Hi! I am currently thinking about using OpenCV with C++ inside a S-function in Simulink in order to simulate and generate C code afterwards. Did you managed to do that? Is it possible? Thanks in advance.
Réponses (1)
Prashant Arora
le 28 Avr 2017
Hi Saul,
The OpenCV Interface allows you to call OpenCV functions with mxArray. In a Simulink C-Mex S-function, real_T is basically double and not mxArray. You will need to first create a mxArray using the input from Simulink.
To achieve your use-case in an easier way, I would recommend using the following workflow:
1) Create a MATLAB mex file (not a Simulink S-function) which calls OpenCV Functions in MATLAB. You can refer to the following link for doing that.
2) Use a MATLAB function block to call this mex file in Simulink.
2 commentaires
Michele Mondini
le 23 Fév 2018
Hi Prashant Arora,
I'm struggling with a similar problem. What do you mean with: "Use a MATLAB function block to call this mex file in Simulink"? Can you explain more precisely please?
I tried to do that but I found out an incompatibility between mxArrays data type, required for Mex functions, and standard data in simulink (e.g. double and uint8 image data matrices).
Thanks in advance!
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!