opencv/samples/cpp/shape_example.cpp

122 lines
3.5 KiB
C++
Raw Normal View History

2013-09-24 01:00:49 +08:00
/*
* shape_context.cpp -- Shape context demo for shape matching
*/
#include "opencv2/shape.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2013-09-24 01:00:49 +08:00
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/core/utility.hpp>
#include <iostream>
#include <string>
using namespace std;
using namespace cv;
static void help()
{
printf("\n"
"This program demonstrates a method for shape comparisson based on Shape Context\n"
2014-09-13 22:28:41 +08:00
"You should run the program providing a number between 1 and 20 for selecting an image in the folder ../data/shape_sample.\n"
2013-09-24 01:00:49 +08:00
"Call\n"
2015-08-01 23:24:23 +08:00
"./shape_example [number between 1 and 20, 1 default]\n\n");
2013-09-24 01:00:49 +08:00
}
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;
2013-10-04 21:03:15 +08:00
for (int add=(int)contoursQuery.size()-1; add<n; add++)
2013-09-24 01:00:49 +08:00
{
contoursQuery.push_back(contoursQuery[dummy++]); //adding dummy values
}
// Uniformly sampling
random_shuffle(contoursQuery.begin(), contoursQuery.end());
vector<Point> cont;
for (int i=0; i<n; i++)
{
cont.push_back(contoursQuery[i]);
}
return cont;
}
int main(int argc, char** argv)
{
2014-09-13 22:28:41 +08:00
string path = "../data/shape_sample/";
2015-08-01 23:24:23 +08:00
cv::CommandLineParser parser(argc, argv, "{help h||}{@input|1|}");
if (parser.has("help"))
2013-09-24 01:00:49 +08:00
{
2015-08-01 23:24:23 +08:00
help();
return 0;
2013-09-24 01:00:49 +08:00
}
2015-08-01 23:24:23 +08:00
int indexQuery = parser.get<int>("@input");
if (!parser.check())
2013-09-24 01:00:49 +08:00
{
2015-08-01 23:24:23 +08:00
parser.printErrors();
help();
return 1;
}
if (indexQuery < 1 || indexQuery > 20)
{
help();
return 1;
2013-09-24 01:00:49 +08:00
}
cv::Ptr <cv::ShapeContextDistanceExtractor> mysc = cv::createShapeContextDistanceExtractor();
Size sz2Sh(300,300);
stringstream queryName;
queryName<<path<<indexQuery<<".png";
Mat query=imread(queryName.str(), IMREAD_GRAYSCALE);
Mat queryToShow;
resize(query, queryToShow, sz2Sh, 0, 0, INTER_LINEAR_EXACT);
2013-09-24 01:00:49 +08:00
imshow("QUERY", queryToShow);
moveWindow("TEST", 0,0);
vector<Point> contQuery = simpleContour(query);
int bestMatch = 0;
2013-09-24 01:00:49 +08:00
float bestDis=FLT_MAX;
for ( int ii=1; ii<=20; ii++ )
{
if (ii==indexQuery) continue;
waitKey(30);
stringstream iiname;
iiname<<path<<ii<<".png";
cout<<"name: "<<iiname.str()<<endl;
Mat iiIm=imread(iiname.str(), 0);
Mat iiToShow;
resize(iiIm, iiToShow, sz2Sh, 0, 0, INTER_LINEAR_EXACT);
2013-09-24 01:00:49 +08:00
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;
}
destroyWindow("TEST");
stringstream bestname;
bestname<<path<<bestMatch<<".png";
Mat iiIm=imread(bestname.str(), 0);
Mat bestToShow;
resize(iiIm, bestToShow, sz2Sh, 0, 0, INTER_LINEAR_EXACT);
2013-09-24 01:00:49 +08:00
imshow("BEST MATCH", bestToShow);
moveWindow("BEST MATCH", sz2Sh.width+50,0);
2017-07-26 13:39:53 +08:00
waitKey();
2013-09-24 01:00:49 +08:00
return 0;
}