Main Content

Convert Between Simulink Image Type and Matrices

You can import image data into a Simulink® model as matrix data or as an image with the Simulink.ImageType data type. Image To Matrix and Matrix To Image blocks convert between Simulink image data and matrix data. Use these blocks to integrate Simulink image data into an image processing algorithm that contains blocks that do not support Simulink image data.

Copy Example Model to a Writable Location

To access the path to the example folder, at the MATLAB® command line, enter:

OpenCVSimulinkExamples;

Copy the example model ex_imagetypes to a writable folder location.

Example Model

The model ex_imagetypes contains two input images, coins and peppers_bw, specified as matrices. The MATLAB Function block contrast_histeq converts the original images into high-contrast images. The MATLAB Function block operates on matrix data only. The Image To Matrix block converts the image data from the Switch block into a matrix to enable the MATLAB Function block to process it.Model screenshot.

Simulate Model

On the Simulink toolstrip, in the Simulation tab, select Run to simulate the model. The Video Viewer block Video Viewer Original displays the original images imported into the model, and Video Viewer High Contrast displays the high-contrasted images after image processing.

Video Viewer block displays processed image of coins.

Video Viewer block displays processed image of peppers.

Generate C++ Code

To generate C++ code:

  1. On the Apps tab on the Simulink toolstrip, select Simulink Coder. On the C++ Code tab, in the Settings list, select C/C++ Code generation settings to open the Configuration Parameters dialog box and verify these settings:

    • In the Simulation Target pane, Language is set to C++.

    • In the Code Generation pane, under Target selection, Language is set to C++.

    • In the same section, Language standard is set to C++11 (ISO).

  2. Click the Build button and generate code.

  3. To view the generated code, on the Simulink toolstrip, click the Open Report button.

In the ex_imagetype.h file, the code generator declares root-level Outports Out_normmal as a member of the C++ class images::datatypes::Image implemented by The MathWorks® and Out_highContrast as a matrix image:

/* External outputs (root outports fed by signals with default storage) */
struct ExtY_ex_imagetypes_T {
  images::datatypes::Image Out_normal; /* '<Root>/Out_normal' */
  uint8_T Out_highContrast[73800];     /* '<Root>/Out_highContrast' */
};

The code generator initializes signals of the Simulink.ImageType data type in the ex_imagetypes.c file:

/* Model initialize function */
void ex_imagetypes::initialize()
{
  /* Registration code */
  constructImage(&ex_imagetypes_B.toImage, 1U, 246U, 300U, images::datatypes::
                 ColorFormat::Grayscale, images::datatypes::Layout::
                 ColumnMajorPlanar, images::datatypes::ClassUnderlying::Uint8);
  constructImage(&ex_imagetypes_Y.Out_normal, 1U, 246U, 300U, images::datatypes::
                 ColorFormat::Grayscale, images::datatypes::Layout::
                 ColumnMajorPlanar, images::datatypes::ClassUnderlying::Uint8);
  ...
}

This is the code for a Matrix To Image block:

/* ToImage: '<S2>/toImage' incorporates:
   *  Constant: '<Root>/coins'
   *  Outport: '<Root>/Out_normal'
   */
  imgData = imageGetDataFcn(&ex_imagetypes_Y.Out_normal);
  inPtr = &ex_imagetypes_ConstP.coins_Value[0];
  std::memcpy(imgData, inPtr, sizeof(uint8_T) * 73800U);

This is the code for the Image To Matrix block:

/* FromImage: '<S1>/fromImage' incorporates:
   *  Outport: '<Root>/Out_normal'
   */
  tmp_3 = ex_imagetypes_Y.Out_normal;
  imgData = imageGetDataFcn(&tmp_3);
  std::memcpy(&ex_imagetypes_B.fromImage[0], imgData, sizeof(uint8_T) * 73800U);

When a model contains signals of the Simulink.ImageType data type, the code generator produces additional shared utility files. These files declare and define utilities to construct, destruct, and return information about meta attributes of the images:

  • image_type.h

  • image_type.cpp

See Also

| |

Related Topics