%% Create MATLAB Interface Object for OpenCV Mat and Associated Array Classes
% Add the prebuilt MATLAB interface to OpenCV package name to the import list.

import vision.opencv.util.*

%
%% Create MATLAB Interface Object for OpenCV Mat and InputArray Class
% Read an image into the MATLAB workspace.

img = imread("cameraman.tif");

% Create a MATLAB interface object for the OpenCV Mat class and store the image data. The function also creates an interface object for the OpenCV InputArray class that is constructed from the Mat class.

[ocvMat,ocvArray] = createMat(img);

% Display and inspect the properties of the MATLAB interface object for the OpenCV Mat class.

ocvMat

% Inspect the type of array class returned by the function.

ocvArray

% Use the getImage utility function to read the image stored in the InputArray class. Display the image.

outImg = getImage(ocvArray);
figure
imshow(outImg)

%
%% Create MATLAB Interface Object for OpenCV Mat and OutputArray Classes
% Add the prebuilt MATLAB interface to OpenCV package name to the import list.

import vision.opencv.util.*

% Create a MATLAB interface object for the OpenCV Mat class. The function also creates an interface object for the OpenCV OutputArray class that is constructed from the Mat class.

[ocvMat,ocvArray] = createMat;

% Display and inspect the properties of the MATLAB interface object for the OpenCV Mat class.

ocvMat

% Inspect the type of array class returned by the function.

ocvArray

%
%% Specify Array Class to Construct from OpenCV Mat Class
% Add the prebuilt MATLAB interface to OpenCV package name to the import list.

import vision.opencv.util.*

% Read an image into the MATLAB workspace.

img = imread("cameraman.tif");

% Create a MATLAB interface object for the OpenCV Mat class. Specify the array type as "InputOutput" for the function to construct the OpenCV InputOutputArray class from the Mat class.

arrayType = "InputOutput";
[ocvMat,ocvArray] = createMat(img,arrayType);

% Display and inspect the properties of the MATLAB interface object for the OpenCV Mat class.

ocvMat

% Inspect the type of array class returned by the function.

ocvArray

% Copyright 2021 The MathWorks, Inc.