shape: move sample to opencv_contrib
@@ -53,7 +53,7 @@ namespace cv
|
|||||||
//! @addtogroup shape
|
//! @addtogroup shape
|
||||||
//! @{
|
//! @{
|
||||||
|
|
||||||
/** @example samples/cpp/shape_example.cpp
|
/** @example modules/shape/samples/shape_example.cpp
|
||||||
An example using shape distance algorithm
|
An example using shape distance algorithm
|
||||||
*/
|
*/
|
||||||
/** @brief Abstract base class for shape distance algorithms.
|
/** @brief Abstract base class for shape distance algorithms.
|
||||||
|
BIN
modules/shape/samples/data/shape_sample/1.png
Normal file
After Width: | Height: | Size: 705 B |
BIN
modules/shape/samples/data/shape_sample/10.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
modules/shape/samples/data/shape_sample/11.png
Normal file
After Width: | Height: | Size: 722 B |
BIN
modules/shape/samples/data/shape_sample/12.png
Normal file
After Width: | Height: | Size: 437 B |
BIN
modules/shape/samples/data/shape_sample/13.png
Normal file
After Width: | Height: | Size: 443 B |
BIN
modules/shape/samples/data/shape_sample/14.png
Normal file
After Width: | Height: | Size: 1.8 KiB |
BIN
modules/shape/samples/data/shape_sample/15.png
Normal file
After Width: | Height: | Size: 803 B |
BIN
modules/shape/samples/data/shape_sample/16.png
Normal file
After Width: | Height: | Size: 830 B |
BIN
modules/shape/samples/data/shape_sample/17.png
Normal file
After Width: | Height: | Size: 3.0 KiB |
BIN
modules/shape/samples/data/shape_sample/18.png
Normal file
After Width: | Height: | Size: 3.2 KiB |
BIN
modules/shape/samples/data/shape_sample/19.png
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
modules/shape/samples/data/shape_sample/2.png
Normal file
After Width: | Height: | Size: 813 B |
BIN
modules/shape/samples/data/shape_sample/20.png
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
modules/shape/samples/data/shape_sample/3.png
Normal file
After Width: | Height: | Size: 2.2 KiB |
BIN
modules/shape/samples/data/shape_sample/4.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
modules/shape/samples/data/shape_sample/5.png
Normal file
After Width: | Height: | Size: 852 B |
BIN
modules/shape/samples/data/shape_sample/6.png
Normal file
After Width: | Height: | Size: 969 B |
BIN
modules/shape/samples/data/shape_sample/7.png
Normal file
After Width: | Height: | Size: 874 B |
BIN
modules/shape/samples/data/shape_sample/8.png
Normal file
After Width: | Height: | Size: 851 B |
BIN
modules/shape/samples/data/shape_sample/9.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
121
modules/shape/samples/shape_example.cpp
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* 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 <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 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"
|
||||||
|
"Call\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
|
||||||
|
cv::randShuffle(contoursQuery);
|
||||||
|
vector<Point> cont;
|
||||||
|
for (int i=0; i<n; i++)
|
||||||
|
{
|
||||||
|
cont.push_back(contoursQuery[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"))
|
||||||
|
{
|
||||||
|
help();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int indexQuery = parser.get<int>("@input");
|
||||||
|
if (!parser.check())
|
||||||
|
{
|
||||||
|
parser.printErrors();
|
||||||
|
help();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (indexQuery < 1 || indexQuery > 20)
|
||||||
|
{
|
||||||
|
help();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
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;
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
imshow("BEST MATCH", bestToShow);
|
||||||
|
moveWindow("BEST MATCH", sz2Sh.width+50,0);
|
||||||
|
waitKey();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
26
modules/shape/test_python/test_shape.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
import cv2 as cv
|
||||||
|
|
||||||
|
from tests_common import NewOpenCVTests
|
||||||
|
|
||||||
|
class shape_test(NewOpenCVTests):
|
||||||
|
|
||||||
|
def test_computeDistance(self):
|
||||||
|
|
||||||
|
a = self.get_sample('samples/data/shape_sample/1.png', cv.IMREAD_GRAYSCALE)
|
||||||
|
b = self.get_sample('samples/data/shape_sample/2.png', cv.IMREAD_GRAYSCALE)
|
||||||
|
|
||||||
|
ca, _ = cv.findContours(a, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS)
|
||||||
|
cb, _ = cv.findContours(b, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS)
|
||||||
|
|
||||||
|
hd = cv.createHausdorffDistanceExtractor()
|
||||||
|
sd = cv.createShapeContextDistanceExtractor()
|
||||||
|
|
||||||
|
d1 = hd.computeDistance(ca[0], cb[0])
|
||||||
|
d2 = sd.computeDistance(ca[0], cb[0])
|
||||||
|
|
||||||
|
self.assertAlmostEqual(d1, 26.4196891785, 3, "HausdorffDistanceExtractor")
|
||||||
|
self.assertAlmostEqual(d2, 0.25804194808, 3, "ShapeContextDistanceExtractor")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
NewOpenCVTests.bootstrap()
|