This source file includes following definitions.
- CURRENT_MAX_ACCUR
- run
- convertContourType
- mpegTest
- displayMPEGResults
- TEST
- TEST
- TEST
#include "test_precomp.hpp"
using namespace cv;
using namespace std;
template <typename T, typename compute>
class ShapeBaseTest : public cvtest::BaseTest
{
public:
typedef Point_<T> PointType;
ShapeBaseTest(int _NSN, int _NP, float _CURRENT_MAX_ACCUR)
: NSN(_NSN), NP(_NP), CURRENT_MAX_ACCUR(_CURRENT_MAX_ACCUR)
{
vector<string> shapeNames;
shapeNames.push_back("apple");
shapeNames.push_back("children");
shapeNames.push_back("device7");
shapeNames.push_back("Heart");
shapeNames.push_back("teddy");
for (vector<string>::const_iterator i = shapeNames.begin(); i != shapeNames.end(); ++i)
{
for (int j = 0; j < NSN; ++j)
{
stringstream filename;
filename << cvtest::TS::ptr()->get_data_path()
<< "shape/mpeg_test/" << *i << "-" << j + 1 << ".png";
filenames.push_back(filename.str());
}
}
const int totalCount = (int)filenames.size();
distanceMat = Mat::zeros(totalCount, totalCount, CV_32F);
}
protected:
void run(int)
{
mpegTest();
displayMPEGResults();
}
vector<PointType> convertContourType(const Mat& currentQuery) const
{
vector<vector<Point> > _contoursQuery;
findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE);
vector <PointType> contoursQuery;
for (size_t border=0; border<_contoursQuery.size(); border++)
{
for (size_t p=0; p<_contoursQuery[border].size(); p++)
{
contoursQuery.push_back(PointType((T)_contoursQuery[border][p].x,
(T)_contoursQuery[border][p].y));
}
}
for (int add=(int)contoursQuery.size()-1; add<NP; add++)
{
contoursQuery.push_back(contoursQuery[contoursQuery.size()-add+1]);
}
random_shuffle(contoursQuery.begin(), contoursQuery.end());
int nStart=NP;
vector<PointType> cont;
for (int i=0; i<nStart; i++)
{
cont.push_back(contoursQuery[i]);
}
return cont;
}
void mpegTest()
{
vector<PointType> contoursQuery1, contoursQuery2, contoursQuery3, contoursTesting;
for (vector<string>::const_iterator a = filenames.begin(); a != filenames.end(); ++a)
{
int aIndex = (int)(a - filenames.begin());
Mat currentQuery = imread(*a, IMREAD_GRAYSCALE);
Mat flippedHQuery, flippedVQuery;
flip(currentQuery, flippedHQuery, 0);
flip(currentQuery, flippedVQuery, 1);
contoursQuery1=convertContourType(currentQuery);
contoursQuery2=convertContourType(flippedHQuery);
contoursQuery3=convertContourType(flippedVQuery);
for (vector<string>::const_iterator b = filenames.begin(); b != filenames.end(); ++b)
{
int bIndex = (int)(b - filenames.begin());
float distance = 0;
if (a != b)
{
Mat currentTest = imread(*b, IMREAD_GRAYSCALE);
contoursTesting=convertContourType(currentTest);
distance = cmp(contoursQuery1, contoursQuery2,
contoursQuery3, contoursTesting);
}
distanceMat.at<float>(aIndex, bIndex) = distance;
}
}
}
void displayMPEGResults()
{
const int FIRST_MANY=2*NSN;
int corrects=0;
int divi=0;
for (int row=0; row<distanceMat.rows; row++)
{
if (row%NSN==0)
{
divi+=NSN;
}
for (int col=divi-NSN; col<divi; col++)
{
int nsmall=0;
for (int i=0; i<distanceMat.cols; i++)
{
if (distanceMat.at<float>(row,col) > distanceMat.at<float>(row,i))
{
nsmall++;
}
}
if (nsmall<=FIRST_MANY)
{
corrects++;
}
}
}
float porc = 100*float(corrects)/(NSN*distanceMat.rows);
std::cout << "Test result: " << porc << "%" << std::endl;
if (porc >= CURRENT_MAX_ACCUR)
ts->set_failed_test_info(cvtest::TS::OK);
else
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
}
protected:
int NSN;
int NP;
float CURRENT_MAX_ACCUR;
vector<string> filenames;
Mat distanceMat;
compute cmp;
};
class computeShapeDistance_Chi
{
Ptr <ShapeContextDistanceExtractor> mysc;
public:
computeShapeDistance_Chi()
{
const int angularBins=12;
const int radialBins=4;
const float minRad=0.2f;
const float maxRad=2;
mysc = createShapeContextDistanceExtractor(angularBins, radialBins, minRad, maxRad);
mysc->setIterations(1);
mysc->setCostExtractor(createChiHistogramCostExtractor(30,0.15f));
mysc->setTransformAlgorithm( createThinPlateSplineShapeTransformer() );
}
float operator()(vector <Point2f>& query1, vector <Point2f>& query2,
vector <Point2f>& query3, vector <Point2f>& testq)
{
return std::min(mysc->computeDistance(query1, testq),
std::min(mysc->computeDistance(query2, testq),
mysc->computeDistance(query3, testq)));
}
};
TEST(Shape_SCD, regression)
{
const int NSN_val=5;
const int NP_val=120;
const float CURRENT_MAX_ACCUR_val=95;
ShapeBaseTest<float, computeShapeDistance_Chi> test(NSN_val, NP_val, CURRENT_MAX_ACCUR_val);
test.safe_run();
}
class computeShapeDistance_EMD
{
Ptr <ShapeContextDistanceExtractor> mysc;
public:
computeShapeDistance_EMD()
{
const int angularBins=12;
const int radialBins=4;
const float minRad=0.2f;
const float maxRad=2;
mysc = createShapeContextDistanceExtractor(angularBins, radialBins, minRad, maxRad);
mysc->setIterations(1);
mysc->setCostExtractor( createEMDL1HistogramCostExtractor() );
mysc->setTransformAlgorithm( createThinPlateSplineShapeTransformer() );
}
float operator()(vector <Point2f>& query1, vector <Point2f>& query2,
vector <Point2f>& query3, vector <Point2f>& testq)
{
return std::min(mysc->computeDistance(query1, testq),
std::min(mysc->computeDistance(query2, testq),
mysc->computeDistance(query3, testq)));
}
};
TEST(ShapeEMD_SCD, regression)
{
const int NSN_val=5;
const int NP_val=100;
const float CURRENT_MAX_ACCUR_val=95;
ShapeBaseTest<float, computeShapeDistance_EMD> test(NSN_val, NP_val, CURRENT_MAX_ACCUR_val);
test.safe_run();
}
class computeShapeDistance_Haussdorf
{
Ptr <HausdorffDistanceExtractor> haus;
public:
computeShapeDistance_Haussdorf()
{
haus = createHausdorffDistanceExtractor();
}
float operator()(vector<Point> &query1, vector<Point> &query2,
vector<Point> &query3, vector<Point> &testq)
{
return std::min(haus->computeDistance(query1,testq),
std::min(haus->computeDistance(query2,testq),
haus->computeDistance(query3,testq)));
}
};
TEST(Hauss, regression)
{
const int NSN_val=5;
const int NP_val = 180;
const float CURRENT_MAX_ACCUR_val=85;
ShapeBaseTest<int, computeShapeDistance_Haussdorf> test(NSN_val, NP_val, CURRENT_MAX_ACCUR_val);
test.safe_run();
}