Use Prebuilt MATLAB Interface to OpenCV
The Computer Vision Toolbox™ Interface for OpenCV provides a prebuilt MATLAB® interface to the OpenCV library that you can use to directly call OpenCV functions from MATLAB without writing a C /C++ code.
Note
The Computer Vision Toolbox Interface for OpenCV in MATLAB supports OpenCV version 4.2.0 and 4.5.0. The interface does not provide GPU support.
The Computer Vision Toolbox Interface for OpenCV also provides MATLAB functions to:
Create MATLAB objects that represent
Mat,UMat, OpenCV base classes pointed by a smart pointer, and OpenCV arrays.MATLAB Function Description createMatReturns Matobjects that represent the OpenCVcv::Matdata structure, and the associated input, output, or input-output array class.createUMatReturns UMatobjects that represent the OpenCVcv::UMatdata structure, and the associated input, output, or input-output array class.clibArrayReturns an object that represents OpenCV native arrays or std::vectortypes.getBasePtrReturns an object that represents an OpenCV base class pointed by a smart pointer. Export OpenCV outputs to MATLAB supported formats.
MATLAB Function Description getImageReads and exports images stored in OpenCV MatandUMatobjects to a matrix or 3-D array in the MATLAB workspace.keyPointsToStructExports the keypoints returned by an OpenCV keypoint detector as a MATLAB structure. rectToBboxExports the parameters returned by an OpenCV rectangle class to a vector in the MATLAB workspace. The parameters are exported as bounding box coordinates in one-based indexing. underlyingValueReturns the underlying numeric values for OpenCV enumeration objects.
The OpenCV functions in the prebuilt library do not return errors except during the run-time.
Call MATLAB Functions
To call a MATLAB function in the Computer Vision Toolbox Interface for OpenCV support package, add the package name
vision.opencv.util to the import list and then call the
MATLAB function. For
example:
import vision.opencv.util.*
[ocvMat,ocvArray] = createMat;vision.opencv to the import
list and call the MATLAB function by prepending util to the function name. For
example:import vision.opencv.*
[ocvMat,ocvArray] = util.createMat;Alternatively, you can also call the MATLAB function by prefixing it with the full package name. Use this syntax to import a specific function without importing every function in the package.
[ocvMat,ocvArray] = vision.opencv.util.createMat;
Call Functions in OpenCV Library
To call a function or class in the prebuilt MATLAB interface to the OpenCV library, add the library name
clib.opencv to the import list. Then call an OpenCV function by
prefixing the function name with a
namespace.
import clib.opencv.*
retVal = namespace.funcname(arg1,arg2,…,argN)namespace
is the namespace of the function, funcname is the OpenCV function
name, and arg1,arg2,...,argN
represents the input arguments for the OpenCV function. retVal is the
output argument.Alternatively, you can also call an OpenCV function by adding the package name as a prefix as shown here.
retVal = clib.opencv.namespace.funcname(arg1,arg2,…,argN)
Note
The interface supports only the cv and
cvflann namespaces. For example, you can call a function in
the cv namespace by using this
syntax:
retVal = clib.opencv.cv.funcname(arg1,arg2,…,argN)
Display Help for MATLAB Functions
To view information about the MATLAB functions in the support package, use doc or help.
Type one of these commands in MATLAB command window.
doc vision.opencv.util.or
help vision.opencv.util.Then press Tab. This command loads the package, and MATLAB displays the list of functions in the package.
To view information about the package in the Help browser, type this command in the MATLAB command window, and then press Enter.
doc vision.opencv.utilDisplay Help for MATLAB Interface to OpenCV Library
Use these MATLAB functions to view information about the classes and the functions in the Prebuilt MATLAB interface to OpenCV library.
docandhelp— View classes and functions in the OpenCV library.methods— View constructor, method, and package function names for a class.methodswith'-full'option — View constructor, method, and package function signatures.methodsview— View a table representation of method signatures. Themethodsviewwindow enables you to find information without scrolling through the Command Window.
Display the classes and package functions by entering this command and then pressing Tab.
doc clib.opencv.This command loads the package, and MATLAB displays a list of the package members. To view the package, press Backspace to remove the period, then press Enter. MATLAB displays all the classes and functions in the OpenCV library.
To display class methods, call the methods function for that class. For example, to see the methods of the
Affine3d class, enter this command.
methods clib.opencv.cv.Affine3dMethods for class clib.opencv.cv.Affine3d: Affine3d eq gt le ne rotation translate concatenate ge inv lt rotate rvec translation Static methods: Identity Methods of clib.opencv.cv.Affine3d inherited from handle.
To display method signatures for a class, call the methodsview function for that class. For example:
methodsview clib.opencv.cv.Affine3dThis command opens a window that displays the methods and information about their arguments and returned values.
Limitations
The prebuilt MATLAB interface to the OpenCV library does not support functionalities that contain these following data types or language features.
Any type with a size greater than 64 bits, such as
long doubleArrays of character types (
wchar_t,char16_t,char32_t)References to a pointer, such as
int*&Pointers or arrays of type
std::stringPointers or references to enumerations
Reference data members
void*data membersstdsmart pointers other thanstd::shared_ptrStatic data members
**pointers, except:MATLAB supports
char**MATLAB supports
**pointers to custom classes used as function or method parameter types.
Multilevel pointers, such as
type***C function pointers and
std::functionas function return types or data members. You also cannot pass a MATLAB function as input to C function pointers or anstd::functionparameter.Class templates with incomplete or no instantiations
User-defined data type
unionArrays of type
std::vectorTypes defined in the
stdnamespace, except these supported types:std::stringstd::wstringstd::u16stringstd::u32stringstd::vectorstd::shared_ptrstd::function