soft cascade become Algorithm
This commit is contained in:
parent
ff8417db00
commit
4a1c4a9862
@ -488,52 +488,52 @@ protected:
|
|||||||
Ptr<MaskGenerator> maskGenerator;
|
Ptr<MaskGenerator> maskGenerator;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* \class SoftCascade
|
// Implementation of soft (stageless) cascaded detector.
|
||||||
* \brief Implement soft (stageless) cascade.
|
class CV_EXPORTS SCascade : public Algorithm
|
||||||
*/
|
|
||||||
class CV_EXPORTS SoftCascade
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
// Representation of detectors result.
|
||||||
* \class Detection
|
|
||||||
* \brief Soft cascade detector result represintation.
|
|
||||||
*/
|
|
||||||
struct CV_EXPORTS Detection
|
struct CV_EXPORTS Detection
|
||||||
{
|
{
|
||||||
|
// Default object type.
|
||||||
enum {PEDESTRIAN = 1};
|
enum {PEDESTRIAN = 1};
|
||||||
|
|
||||||
//! Create detection from an object bounding rectangle and confidence. Only PEDESTRIAN type carrently supported.
|
// Creates Detection from an object bounding box and confidence.
|
||||||
//! Param r is a boundinf rectangle
|
// Param b is a bounding box
|
||||||
//! param c is a confidence that object belongs to class k
|
// Param c is a confidence that object belongs to class k
|
||||||
//! Paral k is an object class
|
// Paral k is an object class
|
||||||
|
Detection(const cv::Rect& b, const float c, int k = PEDESTRIAN) : bb(b), confidence(c), kind(k) {}
|
||||||
|
|
||||||
Detection(const cv::Rect& r, const float c, int k = PEDESTRIAN) : rect(r), confidence(c), kind(k) {}
|
cv::Rect bb;
|
||||||
cv::Rect rect;
|
|
||||||
float confidence;
|
float confidence;
|
||||||
int kind;
|
int kind;
|
||||||
};
|
};
|
||||||
|
|
||||||
//! An empty cascade will be created.
|
// An empty cascade will be created.
|
||||||
//! Param minScale is a minimum scale relative to the original size of the image on which cascade will be applyed.
|
// Param minScale is a minimum scale relative to the original size of the image on which cascade will be applyed.
|
||||||
//! Param minScale is a maximum scale relative to the original size of the image on which cascade will be applyed.
|
// Param minScale is a maximum scale relative to the original size of the image on which cascade will be applyed.
|
||||||
//! Param scales is a number of scales from minScale to maxScale.
|
// Param scales is a number of scales from minScale to maxScale.
|
||||||
SoftCascade( const float minScale = 0.4f, const float maxScale = 5.f, const int scales = 55);
|
// Param rejfactor is used for NMS.
|
||||||
|
SCascade(const float minScale = 0.4f, const float maxScale = 5.f, const int scales = 55, const int rejfactor = 1);
|
||||||
|
|
||||||
//! Cascade will be created for scales from minScale to maxScale.
|
virtual ~SCascade();
|
||||||
//! Param fs is a serialized sacsade.
|
|
||||||
SoftCascade( const cv::FileStorage& fs);
|
|
||||||
|
|
||||||
//! cascade will be loaded. The previous cascade will be destroyed.
|
cv::AlgorithmInfo* info() const;
|
||||||
//! Param fs is a serialized sacsade.
|
|
||||||
bool read( const cv::FileStorage& fs);
|
|
||||||
|
|
||||||
virtual ~SoftCascade();
|
// Load cascade from FileNode.
|
||||||
|
// Param fn is a root node for cascade. Should be <cascade>.
|
||||||
|
virtual bool load(const FileNode& fn);
|
||||||
|
|
||||||
//! return vector of bounding boxes. Each box contains one detected object
|
// Load cascade config.
|
||||||
virtual void detectMultiScale(const Mat& image, const std::vector<cv::Rect>& rois, std::vector<Detection>& objects,
|
virtual void read(const FileNode& fn);
|
||||||
int rejectfactor = 1) const;
|
|
||||||
|
// Return the vector of Decection objcts.
|
||||||
|
// Param image is a frame on which detector will be applied.
|
||||||
|
// Param rois is a vector of regions of interest. Only the objects that fall into one of the regions will be returned.
|
||||||
|
// Param objects is an output array of Detections
|
||||||
|
virtual void detect(const Mat& image, const std::vector<cv::Rect>& rois, std::vector<Detection>& objects) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Filds;
|
struct Filds;
|
||||||
@ -542,8 +542,11 @@ private:
|
|||||||
float minScale;
|
float minScale;
|
||||||
float maxScale;
|
float maxScale;
|
||||||
int scales;
|
int scales;
|
||||||
|
int rejfactor;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
CV_EXPORTS bool initModule_objdetect(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \class IntegralChannels
|
* \class IntegralChannels
|
||||||
* \brief Create channel integrals for Soft Cascade detector.
|
* \brief Create channel integrals for Soft Cascade detector.
|
||||||
|
@ -58,35 +58,36 @@ typedef perf::TestBaseWithParam<fixture> detect;
|
|||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
typedef cv::SoftCascade::Detection detection_t;
|
typedef cv::SCascade::Detection detection_t;
|
||||||
|
|
||||||
void extractRacts(std::vector<detection_t> objectBoxes, vector<Rect> rects)
|
void extractRacts(std::vector<detection_t> objectBoxes, vector<Rect> rects)
|
||||||
{
|
{
|
||||||
rects.clear();
|
rects.clear();
|
||||||
for (int i = 0; i < (int)objectBoxes.size(); ++i)
|
for (int i = 0; i < (int)objectBoxes.size(); ++i)
|
||||||
rects.push_back(objectBoxes[i].rect);
|
rects.push_back(objectBoxes[i].bb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PERF_TEST_P(detect, SoftCascade,
|
PERF_TEST_P(detect, SCascade,
|
||||||
testing::Combine(testing::Values(std::string("cv/cascadeandhog/sc_cvpr_2012_to_opencv.xml")),
|
testing::Combine(testing::Values(std::string("cv/cascadeandhog/sc_cvpr_2012_to_opencv.xml")),
|
||||||
testing::Values(std::string("cv/cascadeandhog/bahnhof/image_00000000_0.png"))))
|
testing::Values(std::string("cv/cascadeandhog/bahnhof/image_00000000_0.png"))))
|
||||||
{
|
{
|
||||||
typedef cv::SoftCascade::Detection detection_t;
|
typedef cv::SCascade::Detection Detection;
|
||||||
cv::Mat colored = imread(getDataPath(get<1>(GetParam())));
|
cv::Mat colored = imread(getDataPath(get<1>(GetParam())));
|
||||||
ASSERT_FALSE(colored.empty());
|
ASSERT_FALSE(colored.empty());
|
||||||
|
|
||||||
cv::SoftCascade cascade;
|
cv::SCascade cascade;
|
||||||
cv::FileStorage fs(getDataPath(get<0>(GetParam())), cv::FileStorage::READ);
|
cv::FileStorage fs(getDataPath(get<0>(GetParam())), cv::FileStorage::READ);
|
||||||
ASSERT_TRUE(cascade.read(fs));
|
ASSERT_TRUE(fs.isOpened());
|
||||||
|
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
|
||||||
|
|
||||||
std::vector<cv::Rect> rois;
|
std::vector<cv::Rect> rois;
|
||||||
std::vector<detection_t> objectBoxes;
|
std::vector<detection_t> objectBoxes;
|
||||||
cascade.detectMultiScale(colored, rois, objectBoxes);
|
cascade.detect(colored, rois, objectBoxes);
|
||||||
|
|
||||||
TEST_CYCLE()
|
TEST_CYCLE()
|
||||||
{
|
{
|
||||||
cascade.detectMultiScale(colored, rois, objectBoxes);
|
cascade.detect(colored, rois, objectBoxes);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<Rect> rects;
|
vector<Rect> rects;
|
||||||
|
@ -7,11 +7,11 @@
|
|||||||
// copy or use the software.
|
// copy or use the software.
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// License Agreement
|
// License Agreement
|
||||||
// For Open Source Computer Vision Library
|
// For Open Source Computer Vision Library
|
||||||
//
|
//
|
||||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
// Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved.
|
||||||
// Third party copyrights are property of their respective owners.
|
// Third party copyrights are property of their respective owners.
|
||||||
//
|
//
|
||||||
// Redistribution and use in source and binary forms, with or without modification,
|
// Redistribution and use in source and binary forms, with or without modification,
|
||||||
@ -40,4 +40,21 @@
|
|||||||
//
|
//
|
||||||
//M*/
|
//M*/
|
||||||
|
|
||||||
#include "precomp.hpp"
|
#include <precomp.hpp>
|
||||||
|
|
||||||
|
namespace cv
|
||||||
|
{
|
||||||
|
|
||||||
|
CV_INIT_ALGORITHM(SCascade, "CascadeDetector.SCascade",
|
||||||
|
obj.info()->addParam(obj, "minScale", obj.minScale));
|
||||||
|
// obj.info()->addParam(obj, "maxScale", obj.maxScale);
|
||||||
|
// obj.info()->addParam(obj, "scales", obj.scales);
|
||||||
|
// obj.info()->addParam(obj, "rejfactor", obj.rejfactor));
|
||||||
|
|
||||||
|
bool initModule_objdetect(void)
|
||||||
|
{
|
||||||
|
Ptr<Algorithm> sc = createSCascade();
|
||||||
|
return sc->info() != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -175,7 +175,7 @@ struct Level
|
|||||||
enum { R_SHIFT = 1 << 15 };
|
enum { R_SHIFT = 1 << 15 };
|
||||||
|
|
||||||
float scaling[2];
|
float scaling[2];
|
||||||
typedef cv::SoftCascade::Detection detection_t;
|
typedef cv::SCascade::Detection detection_t;
|
||||||
|
|
||||||
Level(const Octave& oct, const float scale, const int shrinkage, const int w, const int h)
|
Level(const Octave& oct, const float scale, const int shrinkage, const int w, const int h)
|
||||||
: octave(&oct), origScale(scale), relScale(scale / oct.scale),
|
: octave(&oct), origScale(scale), relScale(scale / oct.scale),
|
||||||
@ -252,7 +252,7 @@ struct ChannelStorage
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cv::SoftCascade::Filds
|
struct cv::SCascade::Filds
|
||||||
{
|
{
|
||||||
float minScale;
|
float minScale;
|
||||||
float maxScale;
|
float maxScale;
|
||||||
@ -491,33 +491,25 @@ struct cv::SoftCascade::Filds
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
cv::SoftCascade::SoftCascade(const float mins, const float maxs, const int nsc)
|
cv::SCascade::SCascade(const float mins, const float maxs, const int nsc, const int rej)
|
||||||
: filds(0), minScale(mins), maxScale(maxs), scales(nsc) {}
|
: filds(0), minScale(mins), maxScale(maxs), scales(nsc), rejfactor(rej) {}
|
||||||
|
|
||||||
cv::SoftCascade::SoftCascade(const cv::FileStorage& fs) : filds(0)
|
cv::SCascade::~SCascade() { delete filds;}
|
||||||
|
|
||||||
|
void cv::SCascade::read(const FileNode& fn)
|
||||||
{
|
{
|
||||||
read(fs);
|
Algorithm::read(fn);
|
||||||
}
|
|
||||||
cv::SoftCascade::~SoftCascade()
|
|
||||||
{
|
|
||||||
delete filds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool cv::SoftCascade::read( const cv::FileStorage& fs)
|
bool cv::SCascade::load(const FileNode& fn)
|
||||||
{
|
{
|
||||||
if (!fs.isOpened()) return false;
|
if (filds) delete filds;
|
||||||
|
|
||||||
if (filds)
|
|
||||||
delete filds;
|
|
||||||
filds = 0;
|
|
||||||
|
|
||||||
filds = new Filds;
|
filds = new Filds;
|
||||||
Filds& flds = *filds;
|
return filds->fill(fn, minScale, maxScale);
|
||||||
return flds.fill(fs.getFirstTopLevelNode(), minScale, maxScale);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cv::SoftCascade::detectMultiScale(const Mat& image, const std::vector<cv::Rect>& /*rois*/,
|
void cv::SCascade::detect(const Mat& image, const std::vector<cv::Rect>& /*rois*/, std::vector<Detection>& objects) const
|
||||||
std::vector<Detection>& objects, const int /*rejectfactor*/) const
|
|
||||||
{
|
{
|
||||||
// only color images are supperted
|
// only color images are supperted
|
||||||
CV_Assert(image.type() == CV_8UC3);
|
CV_Assert(image.type() == CV_8UC3);
|
||||||
|
@ -1,31 +1,31 @@
|
|||||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||||
//
|
//
|
||||||
// By downloading, copying, installing or using the software you agree to this license.
|
// By downloading, copying, installing or using the software you agree to this license.
|
||||||
// If you do not agree to this license, do not download, install,
|
// If you do not agree to this license, do not download, install,
|
||||||
// copy or use the software.
|
// copy or use the software.
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// License Agreement
|
// License Agreement
|
||||||
// For Open Source Computer Vision Library
|
// For Open Source Computer Vision Library
|
||||||
//
|
//
|
||||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||||
// Copyright (C) 2008-2011, Willow Garage Inc., all rights reserved.
|
// Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved.
|
||||||
// Third party copyrights are property of their respective owners.
|
// Third party copyrights are property of their respective owners.
|
||||||
//
|
//
|
||||||
// Redistribution and use in source and binary forms, with or without modification,
|
// Redistribution and use in source and binary forms, with or without modification,
|
||||||
// are permitted provided that the following conditions are met:
|
// are permitted provided that the following conditions are met:
|
||||||
//
|
//
|
||||||
// * Redistributions of source code must retain the above copyright notice,
|
// * Redistribution's of source code must retain the above copyright notice,
|
||||||
// this list of conditions and the following disclaimer.
|
// this list of conditions and the following disclaimer.
|
||||||
//
|
//
|
||||||
// * Redistributions in binary form must reproduce the above copyright notice,
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||||
// this list of conditions and the following disclaimer in the documentation
|
// this list of conditions and the following disclaimer in the documentation
|
||||||
// and/or other materials provided with the distribution.
|
// and/or other materials provided with the distribution.
|
||||||
//
|
//
|
||||||
// * The name of the copyright holders may not be used to endorse or promote products
|
// * The name of the copyright holders may not be used to endorse or promote products
|
||||||
// derived from this software without specific prior written permission.
|
// derived from this software without specific prior written permission.
|
||||||
//
|
//
|
||||||
// This software is provided by the copyright holders and contributors "as is" and
|
// This software is provided by the copyright holders and contributors "as is" and
|
||||||
// any express or implied warranties, including, but not limited to, the implied
|
// any express or implied warranties, including, but not limited to, the implied
|
||||||
@ -37,60 +37,62 @@
|
|||||||
// and on any theory of liability, whether in contract, strict liability,
|
// and on any theory of liability, whether in contract, strict liability,
|
||||||
// or tort (including negligence or otherwise) arising in any way out of
|
// or tort (including negligence or otherwise) arising in any way out of
|
||||||
// the use of this software, even if advised of the possibility of such damage.
|
// the use of this software, even if advised of the possibility of such damage.
|
||||||
|
//
|
||||||
//M*/
|
//M*/
|
||||||
|
|
||||||
#include "test_precomp.hpp"
|
#include "test_precomp.hpp"
|
||||||
|
|
||||||
TEST(SoftCascade, readCascade)
|
TEST(SCascade, readCascade)
|
||||||
{
|
{
|
||||||
std::string xml = cvtest::TS::ptr()->get_data_path() + "cascadeandhog/icf-template.xml";
|
std::string xml = cvtest::TS::ptr()->get_data_path() + "cascadeandhog/icf-template.xml";
|
||||||
cv::SoftCascade cascade;
|
cv::SCascade cascade;
|
||||||
cv::FileStorage fs(xml, cv::FileStorage::READ);
|
cv::FileStorage fs(xml, cv::FileStorage::READ);
|
||||||
ASSERT_TRUE(cascade.read(fs));
|
ASSERT_TRUE(fs.isOpened());
|
||||||
|
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SoftCascade, detect)
|
TEST(SCascade, detect)
|
||||||
{
|
{
|
||||||
typedef cv::SoftCascade::Detection detection_t;
|
typedef cv::SCascade::Detection Detection;
|
||||||
std::string xml = cvtest::TS::ptr()->get_data_path() + "cascadeandhog/sc_cvpr_2012_to_opencv.xml";
|
std::string xml = cvtest::TS::ptr()->get_data_path() + "cascadeandhog/sc_cvpr_2012_to_opencv.xml";
|
||||||
cv::SoftCascade cascade;
|
cv::SCascade cascade;
|
||||||
cv::FileStorage fs(xml, cv::FileStorage::READ);
|
cv::FileStorage fs(xml, cv::FileStorage::READ);
|
||||||
ASSERT_TRUE(cascade.read(fs));
|
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
|
||||||
|
|
||||||
cv::Mat colored = cv::imread(cvtest::TS::ptr()->get_data_path() + "cascadeandhog/bahnhof/image_00000000_0.png");
|
cv::Mat colored = cv::imread(cvtest::TS::ptr()->get_data_path() + "cascadeandhog/bahnhof/image_00000000_0.png");
|
||||||
ASSERT_FALSE(colored.empty());
|
ASSERT_FALSE(colored.empty());
|
||||||
|
|
||||||
std::vector<detection_t> objects;
|
std::vector<Detection> objects;
|
||||||
std::vector<cv::Rect> rois;
|
std::vector<cv::Rect> rois;
|
||||||
rois.push_back(cv::Rect(0, 0, 640, 480));
|
rois.push_back(cv::Rect(0, 0, 640, 480));
|
||||||
|
|
||||||
cascade.detectMultiScale(colored, rois, objects);
|
cascade.detect(colored, rois, objects);
|
||||||
|
|
||||||
|
|
||||||
// cv::Mat out = colored.clone();
|
cv::Mat out = colored.clone();
|
||||||
// int level = 0, total = 0;
|
int level = 0, total = 0;
|
||||||
// int levelWidth = objects[0].rect.width;
|
int levelWidth = objects[0].bb.width;
|
||||||
|
|
||||||
// for(int i = 0 ; i < (int)objects.size(); ++i)
|
for(int i = 0 ; i < (int)objects.size(); ++i)
|
||||||
// {
|
{
|
||||||
// if (objects[i].rect.width != levelWidth)
|
if (objects[i].bb.width != levelWidth)
|
||||||
// {
|
{
|
||||||
// std::cout << "Level: " << level << " total " << total << std::endl;
|
std::cout << "Level: " << level << " total " << total << std::endl;
|
||||||
// cv::imshow("out", out);
|
cv::imshow("out", out);
|
||||||
// cv::waitKey(0);
|
cv::waitKey(0);
|
||||||
// out = colored.clone();
|
out = colored.clone();
|
||||||
// levelWidth = objects[i].rect.width;
|
levelWidth = objects[i].bb.width;
|
||||||
// total = 0;
|
total = 0;
|
||||||
// level++;
|
level++;
|
||||||
// }
|
}
|
||||||
// cv::rectangle(out, objects[i].rect, cv::Scalar(255, 0, 0, 255), 1);
|
cv::rectangle(out, objects[i].bb, cv::Scalar(255, 0, 0, 255), 1);
|
||||||
// std::cout << "detection: " << objects[i].rect.x
|
std::cout << "detection: " << objects[i].bb.x
|
||||||
// << " " << objects[i].rect.y
|
<< " " << objects[i].bb.y
|
||||||
// << " " << objects[i].rect.width
|
<< " " << objects[i].bb.width
|
||||||
// << " " << objects[i].rect.height << std::endl;
|
<< " " << objects[i].bb.height << std::endl;
|
||||||
// total++;
|
total++;
|
||||||
// }
|
}
|
||||||
// std::cout << "detected: " << (int)objects.size() << std::endl;
|
std::cout << "detected: " << (int)objects.size() << std::endl;
|
||||||
ASSERT_EQ((int)objects.size(), 3668);
|
ASSERT_EQ((int)objects.size(), 3668);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user