From 4a1c4a98621577b02b43cff9afe33dab50faccf2 Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Mon, 5 Nov 2012 22:42:08 +0400 Subject: [PATCH] soft cascade become Algorithm --- .../include/opencv2/objdetect/objdetect.hpp | 63 +++++++------ .../objdetect/perf/perf_cascadeclassifier.cpp | 17 ++-- modules/objdetect/src/objdetect_init.cpp | 23 ++++- modules/objdetect/src/softcascade.cpp | 32 +++---- modules/objdetect/test/test_softcascade.cpp | 94 ++++++++++--------- 5 files changed, 122 insertions(+), 107 deletions(-) diff --git a/modules/objdetect/include/opencv2/objdetect/objdetect.hpp b/modules/objdetect/include/opencv2/objdetect/objdetect.hpp index e0799f9b15..84f41e1490 100644 --- a/modules/objdetect/include/opencv2/objdetect/objdetect.hpp +++ b/modules/objdetect/include/opencv2/objdetect/objdetect.hpp @@ -488,52 +488,52 @@ protected: Ptr maskGenerator; }; -/** - * \class SoftCascade - * \brief Implement soft (stageless) cascade. - */ -class CV_EXPORTS SoftCascade + +// Implementation of soft (stageless) cascaded detector. +class CV_EXPORTS SCascade : public Algorithm { public: - /** - * \class Detection - * \brief Soft cascade detector result represintation. - */ + // Representation of detectors result. struct CV_EXPORTS Detection { + // Default object type. enum {PEDESTRIAN = 1}; - //! Create detection from an object bounding rectangle and confidence. Only PEDESTRIAN type carrently supported. - //! Param r is a boundinf rectangle - //! param c is a confidence that object belongs to class k - //! Paral k is an object class + // Creates Detection from an object bounding box and confidence. + // Param b is a bounding box + // Param c is a confidence that object belongs to class k + // 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 rect; + cv::Rect bb; float confidence; int kind; }; - //! 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 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. - SoftCascade( const float minScale = 0.4f, const float maxScale = 5.f, const int scales = 55); + // 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 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 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. - //! Param fs is a serialized sacsade. - SoftCascade( const cv::FileStorage& fs); + virtual ~SCascade(); - //! cascade will be loaded. The previous cascade will be destroyed. - //! Param fs is a serialized sacsade. - bool read( const cv::FileStorage& fs); + cv::AlgorithmInfo* info() const; - virtual ~SoftCascade(); + // Load cascade from FileNode. + // Param fn is a root node for cascade. Should be . + virtual bool load(const FileNode& fn); - //! return vector of bounding boxes. Each box contains one detected object - virtual void detectMultiScale(const Mat& image, const std::vector& rois, std::vector& objects, - int rejectfactor = 1) const; + // Load cascade config. + virtual void read(const FileNode& fn); + + // 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& rois, std::vector& objects) const; private: struct Filds; @@ -542,8 +542,11 @@ private: float minScale; float maxScale; int scales; + int rejfactor; }; +CV_EXPORTS bool initModule_objdetect(void); + /** * \class IntegralChannels * \brief Create channel integrals for Soft Cascade detector. diff --git a/modules/objdetect/perf/perf_cascadeclassifier.cpp b/modules/objdetect/perf/perf_cascadeclassifier.cpp index f91f2f8ec7..cc068e8ea3 100644 --- a/modules/objdetect/perf/perf_cascadeclassifier.cpp +++ b/modules/objdetect/perf/perf_cascadeclassifier.cpp @@ -58,35 +58,36 @@ typedef perf::TestBaseWithParam detect; namespace { - typedef cv::SoftCascade::Detection detection_t; + typedef cv::SCascade::Detection detection_t; void extractRacts(std::vector objectBoxes, vector rects) { rects.clear(); 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::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()))); ASSERT_FALSE(colored.empty()); - cv::SoftCascade cascade; + cv::SCascade cascade; 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 rois; std::vector objectBoxes; - cascade.detectMultiScale(colored, rois, objectBoxes); + cascade.detect(colored, rois, objectBoxes); TEST_CYCLE() { - cascade.detectMultiScale(colored, rois, objectBoxes); + cascade.detect(colored, rois, objectBoxes); } vector rects; diff --git a/modules/objdetect/src/objdetect_init.cpp b/modules/objdetect/src/objdetect_init.cpp index 317867a59f..a6ce81e809 100644 --- a/modules/objdetect/src/objdetect_init.cpp +++ b/modules/objdetect/src/objdetect_init.cpp @@ -7,11 +7,11 @@ // copy or use the software. // // -// License Agreement +// License Agreement // For Open Source Computer Vision Library // // 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. // // Redistribution and use in source and binary forms, with or without modification, @@ -40,4 +40,21 @@ // //M*/ -#include "precomp.hpp" +#include + +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 sc = createSCascade(); + return sc->info() != 0; +} + +} \ No newline at end of file diff --git a/modules/objdetect/src/softcascade.cpp b/modules/objdetect/src/softcascade.cpp index ec1899a4b7..5b4b031697 100644 --- a/modules/objdetect/src/softcascade.cpp +++ b/modules/objdetect/src/softcascade.cpp @@ -175,7 +175,7 @@ struct Level enum { R_SHIFT = 1 << 15 }; 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) : 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 maxScale; @@ -491,33 +491,25 @@ struct cv::SoftCascade::Filds } }; -cv::SoftCascade::SoftCascade(const float mins, const float maxs, const int nsc) -: filds(0), minScale(mins), maxScale(maxs), scales(nsc) {} +cv::SCascade::SCascade(const float mins, const float maxs, const int nsc, const int rej) +: 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); -} -cv::SoftCascade::~SoftCascade() -{ - delete filds; + Algorithm::read(fn); } -bool cv::SoftCascade::read( const cv::FileStorage& fs) +bool cv::SCascade::load(const FileNode& fn) { - if (!fs.isOpened()) return false; - - if (filds) - delete filds; - filds = 0; + if (filds) delete filds; filds = new Filds; - Filds& flds = *filds; - return flds.fill(fs.getFirstTopLevelNode(), minScale, maxScale); + return filds->fill(fn, minScale, maxScale); } -void cv::SoftCascade::detectMultiScale(const Mat& image, const std::vector& /*rois*/, - std::vector& objects, const int /*rejectfactor*/) const +void cv::SCascade::detect(const Mat& image, const std::vector& /*rois*/, std::vector& objects) const { // only color images are supperted CV_Assert(image.type() == CV_8UC3); diff --git a/modules/objdetect/test/test_softcascade.cpp b/modules/objdetect/test/test_softcascade.cpp index 4711c3ca09..b1f8b2da46 100644 --- a/modules/objdetect/test/test_softcascade.cpp +++ b/modules/objdetect/test/test_softcascade.cpp @@ -1,31 +1,31 @@ /*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. -// If you do not agree to this license, do not download, install, -// copy or use the software. +// 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, +// copy or use the software. // // -// License Agreement -// For Open Source Computer Vision Library +// License Agreement +// For Open Source Computer Vision Library // // 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. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // -// * Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. // -// * Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. // -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. // // 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 @@ -37,60 +37,62 @@ // and on any theory of liability, whether in contract, strict liability, // 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. +// //M*/ #include "test_precomp.hpp" -TEST(SoftCascade, readCascade) +TEST(SCascade, readCascade) { 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); - 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"; - cv::SoftCascade cascade; + cv::SCascade cascade; 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"); ASSERT_FALSE(colored.empty()); - std::vector objects; + std::vector objects; std::vector rois; rois.push_back(cv::Rect(0, 0, 640, 480)); - cascade.detectMultiScale(colored, rois, objects); + cascade.detect(colored, rois, objects); - // cv::Mat out = colored.clone(); - // int level = 0, total = 0; - // int levelWidth = objects[0].rect.width; + cv::Mat out = colored.clone(); + int level = 0, total = 0; + int levelWidth = objects[0].bb.width; - // for(int i = 0 ; i < (int)objects.size(); ++i) - // { - // if (objects[i].rect.width != levelWidth) - // { - // std::cout << "Level: " << level << " total " << total << std::endl; - // cv::imshow("out", out); - // cv::waitKey(0); - // out = colored.clone(); - // levelWidth = objects[i].rect.width; - // total = 0; - // level++; - // } - // cv::rectangle(out, objects[i].rect, cv::Scalar(255, 0, 0, 255), 1); - // std::cout << "detection: " << objects[i].rect.x - // << " " << objects[i].rect.y - // << " " << objects[i].rect.width - // << " " << objects[i].rect.height << std::endl; - // total++; - // } - // std::cout << "detected: " << (int)objects.size() << std::endl; + for(int i = 0 ; i < (int)objects.size(); ++i) + { + if (objects[i].bb.width != levelWidth) + { + std::cout << "Level: " << level << " total " << total << std::endl; + cv::imshow("out", out); + cv::waitKey(0); + out = colored.clone(); + levelWidth = objects[i].bb.width; + total = 0; + level++; + } + cv::rectangle(out, objects[i].bb, cv::Scalar(255, 0, 0, 255), 1); + std::cout << "detection: " << objects[i].bb.x + << " " << objects[i].bb.y + << " " << objects[i].bb.width + << " " << objects[i].bb.height << std::endl; + total++; + } + std::cout << "detected: " << (int)objects.size() << std::endl; ASSERT_EQ((int)objects.size(), 3668); } \ No newline at end of file