OpenCV  4.10.0-dev
Open Source Computer Vision
No Matches

An example using shape distance algorithm

* shape_context.cpp -- Shape context demo for shape matching
#include <iostream>
#include <string>
using namespace std;
using namespace cv;
static void help()
"This program demonstrates a method for shape comparison based on Shape Context\n"
"You should run the program providing a number between 1 and 20 for selecting an image in the folder ../data/shape_sample.\n"
"./shape_example [number between 1 and 20, 1 default]\n\n");
static vector<Point> simpleContour( const Mat& currentQuery, int n=300 )
vector<vector<Point> > _contoursQuery;
vector <Point> contoursQuery;
findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE);
for (size_t border=0; border<_contoursQuery.size(); border++)
for (size_t p=0; p<_contoursQuery[border].size(); p++)
contoursQuery.push_back( _contoursQuery[border][p] );
// In case actual number of points is less than n
int dummy=0;
for (int add=(int)contoursQuery.size()-1; add<n; add++)
contoursQuery.push_back(contoursQuery[dummy++]); //adding dummy values
// Uniformly sampling
vector<Point> cont;
for (int i=0; i<n; i++)
return cont;
int main(int argc, char** argv)
string path = "data/shape_sample/";
cv::CommandLineParser parser(argc, argv, "{help h||}{@input|1|}");
if (parser.has("help"))
return 0;
int indexQuery = parser.get<int>("@input");
if (!parser.check())
return 1;
if (indexQuery < 1 || indexQuery > 20)
return 1;
cv::Ptr <cv::ShapeContextDistanceExtractor> mysc = cv::createShapeContextDistanceExtractor();
Size sz2Sh(300,300);
stringstream queryName;
Mat query=imread(queryName.str(), IMREAD_GRAYSCALE);
Mat queryToShow;
resize(query, queryToShow, sz2Sh, 0, 0, INTER_LINEAR_EXACT);
imshow("QUERY", queryToShow);
moveWindow("TEST", 0,0);
vector<Point> contQuery = simpleContour(query);
int bestMatch = 0;
float bestDis=FLT_MAX;
for ( int ii=1; ii<=20; ii++ )
if (ii==indexQuery) continue;
stringstream iiname;
cout<<"name: "<<iiname.str()<<endl;
Mat iiIm=imread(iiname.str(), 0);
Mat iiToShow;
resize(iiIm, iiToShow, sz2Sh, 0, 0, INTER_LINEAR_EXACT);
imshow("TEST", iiToShow);
moveWindow("TEST", sz2Sh.width+50,0);
vector<Point> contii = simpleContour(iiIm);
float dis = mysc->computeDistance( contQuery, contii );
if ( dis<bestDis )
bestMatch = ii;
bestDis = dis;
std::cout<<" distance between "<<queryName.str()<<" and "<<iiname.str()<<" is: "<<dis<<std::endl;
stringstream bestname;
Mat iiIm=imread(bestname.str(), 0);
Mat bestToShow;
resize(iiIm, bestToShow, sz2Sh, 0, 0, INTER_LINEAR_EXACT);
imshow("BEST MATCH", bestToShow);
moveWindow("BEST MATCH", sz2Sh.width+50,0);
return 0;
Designed for command line parsing.
Definition utility.hpp:832
n-dimensional dense array class
Definition mat.hpp:812
Template class for specifying the size of an image or rectangle.
Definition types.hpp:335
void add(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1)
Calculates the per-element sum of two arrays or an array and a scalar.
void randShuffle(InputOutputArray dst, double iterFactor=1., RNG *rng=0)
Shuffles the array elements randomly.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void destroyWindow(const String &winname)
Destroys the specified window.
void moveWindow(const String &winname, int x, int y)
Moves the window to the specified position.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR)
Resizes an image.
Ptr< ShapeContextDistanceExtractor > createShapeContextDistanceExtractor(int nAngularBins=12, int nRadialBins=4, float innerRadius=0.2f, float outerRadius=2, int iterations=3, const Ptr< HistogramCostExtractor > &comparer=createChiHistogramCostExtractor(), const Ptr< ShapeTransformer > &transformer=createThinPlateSplineShapeTransformer())
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
"black box" representation of the file storage associated with a file on disk.
Definition core.hpp:102
STL namespace.