Main Content

keyPointsToStruct

Convert OpenCVKeyPointsobject toMATLABstructure

    Description

    example

    mlstruct= keyPointsToStruct(keypoints)converts an OpenCVKeyPointsobject to a MATLAB®structure.

    Examples

    collapse all

    This example shows how to use the prebuilt MATLAB interface for the OpenCV functioncv::Fastin MATLAB to detect keypoints in an image. Additionally, use thekeyPointsToStructutility function to write the keypoints returned by the OpenCVcv::Fastfunction to a MATLAB structure.

    Add the MATLAB interface to OpenCV package names to the import list.

    importclib.opencv.*; importvision.opencv.util.*;

    Read an image into the MATLAB workspace.

    img = imread("elephant.jpg");

    Create MATLAB interface objects for the OpenCVMatNDandInputArrayclasses to store the input image.

    [inputMat,inputArray] = createMat(img);

    Create a MATLAB interface object for the OpenCVKeyPointvector by using theclibArrayfunction.

    keyPointsVec = clibArray("clib.opencv.cv.KeyPoint",0);

    Specify the parameters for computing keypoints using the FAST detector.

    threshold = 100; nonmaxSuppression = true;

    Compute keypoints in the image by calling the OpenCV functioncv::FASTin MATLAB.

    cv.FAST(inputArray,keyPointsVec,threshold,nonmaxSuppression);

    Convert theKeyPointsobject returned by the OpenCV function into a MATLAB structure.

    mlstruct = keyPointsToStruct(keyPointsVec);

    Inspect the fields in the output MATLAB structure.

    mlstruct
    mlstruct =struct with fields:Location: [48x2 double] Scale: [48x1 double] Metric: [48x1 double] Misc: [48x1 double] Orientation: [48x1 double]

    Display the input image and plot the detected keypoints.

    figure imshow(img) holdonplot(mlstruct.Location(:,1),mlstruct.Location(:,2),"*r") holdoff

    Figure contains an axes object. The axes object contains 2 objects of type image, line.

    Input Arguments

    collapse all

    OpenCVKeyPointsclass, specified as a MATLAB interface object. This interface object is a representation of theKeyPointsclass returned by any of the OpenCV functions for keypoint detection.

    Output Arguments

    collapse all

    Keypoints detected using the OpenCV function, returned as a MATLAB structure with fieldsLocation,Scale,Metric,Misc, andOrientation.

    Fields Description
    Location xandy-coordinates of the keypoints.
    Scale Diameter of the neighborhood region around the keypoints.
    Metric 力量的要点。
    Orientation Orientation of the keypoints.

    Version History

    Introduced in R2021b