OpenCV自带的行人检测demo,opencv\samples\cpp\shape_example.cpp。shape_example.cpp可以图形形状相似对比。通过判断Hausdorff距离的结果做出最匹配判断,Hausdorff距离越小,匹配度越高。
OpenCV提供20张形状图片,在路径opencv\samples\data\shape_sample
中。
opencv\samples\data\shape_sample
参考opencv01 相对完整的编译opencv3.3.0 win版本,编译opencv3.3.0 vs2015版本。
为修改参数方便,稍微修改下代码,添加path参数:
//string path = "../data/shape_sample/";
string path;
cv::CommandLineParser parser(argc, argv, "{help h||}{@input|1|}{path|../data/shape_sample/|}");
if (parser.has("help"))
{
help();
return 0;
}
int indexQuery = parser.get<int>("@input");
if (!parser.check())
{
parser.printErrors();
help();
return 1;
}
path = parser.get<string>("path");
if (!parser.check())
{
parser.printErrors();
help();
return 2;
}
生成文件:D:\git\opencv\build\v3.3.0\x64\bin\Debug\cpp-example-shape_example.exe
设置shape_example项目为启动项。
# -@input=5,这个@input默认是1,可以选择1-20间的任何一个图片
配置属性==>调试==>命令参数=-@input=5 -path=../../../../../samples/data/shape_sample/
配置属性==>调试==>工作目录=$(OutDir)
5.png和6.png的距离最小:0.0710131。
name: ../../../../../samples/data/shape_sample/1.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/1.png is: 4.05005
name: ../../../../../samples/data/shape_sample/2.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/2.png is: 22.7777
name: ../../../../../samples/data/shape_sample/3.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/3.png is: 8533.25
name: ../../../../../samples/data/shape_sample/4.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/4.png is: 10817.4
name: ../../../../../samples/data/shape_sample/6.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/6.png is: 0.0710131
name: ../../../../../samples/data/shape_sample/7.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/7.png is: 50.1265
name: ../../../../../samples/data/shape_sample/8.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/8.png is: 50.223
name: ../../../../../samples/data/shape_sample/9.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/9.png is: 15.7305
name: ../../../../../samples/data/shape_sample/10.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/10.png is: 15.0504
name: ../../../../../samples/data/shape_sample/11.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/11.png is: 8.31791
name: ../../../../../samples/data/shape_sample/12.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/12.png is: 5.07157
name: ../../../../../samples/data/shape_sample/13.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/13.png is: 6004.73
name: ../../../../../samples/data/shape_sample/14.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/14.png is: 3644.86
name: ../../../../../samples/data/shape_sample/15.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/15.png is: 1.40525
name: ../../../../../samples/data/shape_sample/16.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/16.png is: 3.90065
name: ../../../../../samples/data/shape_sample/17.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/17.png is: 10044.1
name: ../../../../../samples/data/shape_sample/18.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/18.png is: 2120.9
name: ../../../../../samples/data/shape_sample/19.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/19.png is: 8802.19
name: ../../../../../samples/data/shape_sample/20.png
distance between ../../../../../samples/data/shape_sample/5.png and ../../../../../samples/data/shape_sample/20.png is: 2448.39
/*
* shape_context.cpp -- Shape context demo for shape matching
*/
#include "opencv2/shape.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include
#include
#include
using namespace std;
using namespace cv;
static void help()
{
printf("\n"
"This program demonstrates a method for shape comparisson 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"
"Call\n"
"./shape_example [number between 1 and 20, 1 default]\n\n");
}
static vector simpleContour( const Mat& currentQuery, int n=300 )
{
vector<vector > _contoursQuery;
vector 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//adding dummy values
}
// Uniformly sampling
random_shuffle(contoursQuery.begin(), contoursQuery.end());
vector cont;
for (int i=0; ireturn cont;
}
int main(int argc, char** argv)
{
//string path = "../data/shape_sample/";
string path;
cv::CommandLineParser parser(argc, argv, "{help h||}{@input|1|}{path|../data/shape_sample/|}");
if (parser.has("help"))
{
help();
return 0;
}
int indexQuery = parser.get<int>("@input");
if (!parser.check())
{
parser.printErrors();
help();
return 1;
}
path = parser.get<string>("path");
if (!parser.check())
{
parser.printErrors();
help();
return 2;
}
if (indexQuery < 1 || indexQuery > 20)
{
help();
return 1;
}
cv::Ptr mysc = cv::createShapeContextDistanceExtractor();
Size sz2Sh(300,300);
stringstream queryName;
queryName<".png";
Mat query=imread(queryName.str(), IMREAD_GRAYSCALE);
Mat queryToShow;
resize(query, queryToShow, sz2Sh);
imshow("QUERY", queryToShow);
moveWindow("TEST", 0,0);
vector contQuery = simpleContour(query);
int bestMatch = 0;
float bestDis=FLT_MAX;
for ( int ii=1; ii<=20; ii++ )
{
if (ii==indexQuery) continue;
waitKey(30);
stringstream iiname;
iiname<".png";
cout<<"name: "<0);
Mat iiToShow;
resize(iiIm, iiToShow, sz2Sh);
imshow("TEST", iiToShow);
moveWindow("TEST", sz2Sh.width+50,0);
vector contii = simpleContour(iiIm);
float dis = mysc->computeDistance( contQuery, contii );
if ( disstd::cout<<" distance between "<" and "<" is: "<std::endl;
}
destroyWindow("TEST");
stringstream bestname;
bestname<".png";
Mat iiIm=imread(bestname.str(), 0);
Mat bestToShow;
resize(iiIm, bestToShow, sz2Sh);
imshow("BEST MATCH", bestToShow);
moveWindow("BEST MATCH", sz2Sh.width+50,0);
waitKey();
return 0;
}