How to use OpenCV in a C++ S-Function?
이전 댓글 표시
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:

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
Gonzalo Salinas
2018년 2월 8일
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.
답변 (1개)
Prashant Arora
2017년 4월 28일
1 개 추천
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
Saul Armendariz
2017년 5월 8일
Michele Mondini
2018년 2월 23일
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!
카테고리
도움말 센터 및 File Exchange에서 OpenCV Support에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!