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


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

  • CUDA enabled NVIDIA® GPU and compatible driver.


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;

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.

% Read Image
K = imread('cameraman.tif'); % Reference image
refImage = imresize(K,3);

scale = 0.7;  % Scaling the image.
J = imresize(refImage, scale);
theta = 30.0;   % Rotating the image
qryImage = imrotate(J,theta); % Query image

[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 The MathWorks, Inc.


%% 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;
codegen -config cfg -args {refPoints,refDescFeat,qryPoints,qryDescFeat} feature_matching -o feature_matching_gpu_mex
[matchedRefPoints_gpu,matchedQryPoints_gpu] = feature_matching_gpu_mex(refPoints,refDescFeat,qryPoints,qryDescFeat);
Code generation successful.

Display Feature Matches

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

See Also



Related Topics