Main Content

Feature Matching

This example shows how to generate CUDA® MEX from MATLAB® code and perform feature matching between two images. This example uses the matchFeatures (Computer Vision Toolbox) function from the Image Processing Toolbox™ to match the feature descriptors between two images that are rotated and scaled with respect to each other. The feature descriptors of the two images are detected and extracted by using the Speeded-Up Robust Features (SURF) algorithm.

Third-Party Prerequisites

Required

This example generates CUDA MEX and has the following third-party requirements.

  • CUDA enabled NVIDIA® GPU and compatible driver.

Optional

For non-MEX builds such as static, dynamic libraries or executables, this example has the following additional requirements.

Verify GPU Environment

To verify that the compilers and libraries necessary for running this example are set up correctly, use the coder.checkGpuInstall function.

envCfg = coder.gpuEnvConfig('host');
envCfg.BasicCodegen = 1;
envCfg.Quiet = 1;
coder.checkGpuInstall(envCfg);

Feature Detection and Extraction

For this example, feature matching is performed on two images that are rotated and scaled with respect to each other. Before the two images can be matched, feature points for each image must be detected and extracted. The following featureDetectionAndExtraction function uses SURF (detectSURFFeatures (Computer Vision Toolbox)) local feature detector to detect the feature points and extractFeatures (Computer Vision Toolbox) to extract the features.

The function featureDetectionAndExtraction returns refPoints, which contains the feature coordinates of the reference image, qryPoints, containing feature coordinates of query image, refDesc matrix containing reference image feature descriptors and qryDesc matrix containing query image feature descriptors.

  • refPoints = Reference image feature coordinates.

  • qryPoints = Query image feature coordinates.

  • refDescFeat = Reference image feature descriptors.

  • qryDescFeat = Query image feature descriptors.

K = imread('cameraman.tif'); 
refImage = imresize(K,3);
scale = 0.7;                  
J = imresize(refImage,scale);
theta = 30.0;                 
qryImage = imrotate(J,theta); 
[refPoints,refDescFeat,qryPoints,qryDescFeat] = featureDetectionAndExtraction(refImage,...
    qryImage);

The feature_matching Entry-Point Function

The feature_matching function takes feature points and feature descriptors extracted from two images and finds a match between them.

type feature_matching
function [matchedRefPoints,matchedQryPoints] = feature_matching(refPoints,...
    refDesc,qryPoints,qryDesc) 
%#codegen
% Copyright 2018-2021 The MathWorks, Inc.

coder.gpu.kernelfun;
%% Feature Matching
[indexPairs,matchMetric] = matchFeatures(refDesc, qryDesc);
matchedRefPoints = refPoints(indexPairs(:,1),:);
matchedQryPoints = qryPoints(indexPairs(:,2),:);

Feature Matching Code Generation

Because the example runs on the host system, create a MEX-call configuration object with default parameters. To avoid abnormal termination of MATLAB if there are run-time errors in the generated code, select the safe-build option.

cfg = coder.gpuConfig;
cfg.GpuConfig.SafeBuild = 1;
inputs = {refPoints,refDescFeat,qryPoints,qryDescFeat};
codegen -config cfg -args inputs feature_matching
Code generation successful.
[matchedRefPoints_gpu,matchedQryPoints_gpu] = feature_matching_mex(refPoints,...
    refDescFeat,qryPoints,qryDescFeat);

Display Feature Matches

figure;
showMatchedFeatures(refImage, qryImage, matchedRefPoints_gpu, matchedQryPoints_gpu);
title('Putatively Matched Points (Including Outliers)');

See Also

Functions

Objects

Related Topics