From 1ab7af69956491e5965a0429093c2296b91b7428 Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Thu, 20 Sep 2012 18:35:48 +0400 Subject: [PATCH] GPU soft cascade: buffers preallocation --- modules/gpu/include/opencv2/gpu/gpu.hpp | 12 ----- modules/gpu/src/icf.hpp | 16 +++++- modules/gpu/src/softcascade.cpp | 68 +++++++++++++++++++++---- 3 files changed, 72 insertions(+), 24 deletions(-) diff --git a/modules/gpu/include/opencv2/gpu/gpu.hpp b/modules/gpu/include/opencv2/gpu/gpu.hpp index 61f6006c5..5008e1027 100644 --- a/modules/gpu/include/opencv2/gpu/gpu.hpp +++ b/modules/gpu/include/opencv2/gpu/gpu.hpp @@ -1563,18 +1563,6 @@ public: virtual void detectMultiScale(const GpuMat& image, const GpuMat& rois, GpuMat& objects, int rejectfactor = 1, Stream stream = Stream::Null()); -protected: - enum { BOOST = 0 }; - enum - { - FRAME_WIDTH = 640, - FRAME_HEIGHT = 480, - TOTAL_SCALES = 55, - CLASSIFIERS = 5, - ORIG_OBJECT_WIDTH = 64, - ORIG_OBJECT_HEIGHT = 128 - }; - private: struct Filds; Filds* filds; diff --git a/modules/gpu/src/icf.hpp b/modules/gpu/src/icf.hpp index 110890232..49919a79c 100644 --- a/modules/gpu/src/icf.hpp +++ b/modules/gpu/src/icf.hpp @@ -40,6 +40,8 @@ // //M*/ +#include + #ifndef __OPENCV_ICF_HPP__ #define __OPENCV_ICF_HPP__ @@ -54,12 +56,24 @@ namespace icf { struct Cascade { + Cascade() {} + Cascade(const cv::gpu::PtrStepSzb& octs, const cv::gpu::PtrStepSzf& sts, const cv::gpu::PtrStepSzb& nds, + const cv::gpu::PtrStepSzf& lvs, const cv::gpu::PtrStepSzb& fts, const cv::gpu::PtrStepSzb& lls) + : octaves(octs), stages(sts), nodes(nds), leaves(lvs), features(fts), levels(lls) {} + + cv::gpu::PtrStepSzb octaves; + cv::gpu::PtrStepSzf stages; + cv::gpu::PtrStepSzb nodes; + cv::gpu::PtrStepSzf leaves; + cv::gpu::PtrStepSzb features; + + cv::gpu::PtrStepSzb levels; }; struct ChannelStorage { - + ChannelStorage(const cv::gpu::PtrStepSzb& /*f*/, const int /*shrinkage*/) {} }; struct __align__(16) Octave diff --git a/modules/gpu/src/softcascade.cpp b/modules/gpu/src/softcascade.cpp index 18306e04d..8ef1da457 100644 --- a/modules/gpu/src/softcascade.cpp +++ b/modules/gpu/src/softcascade.cpp @@ -72,19 +72,41 @@ struct cv::gpu::SoftCascade::Filds GpuMat nodes; GpuMat leaves; GpuMat features; + GpuMat levels; + + // preallocated buffer 640x480x10 + GpuMat dmem; + // 160x120x10 + GpuMat shrunk; + // 161x121x10 + GpuMat hogluv; std::vector scales; icf::Cascade cascade; bool fill(const FileNode &root, const float mins, const float maxs); + void detect(const icf::ChannelStorage& /*channels*/) const {} + + enum { BOOST = 0 }; + enum + { + FRAME_WIDTH = 640, + FRAME_HEIGHT = 480, + TOTAL_SCALES = 55, + CLASSIFIERS = 5, + ORIG_OBJECT_WIDTH = 64, + ORIG_OBJECT_HEIGHT = 128, + HOG_BINS = 6, + HOG_LUV_BINS = 10 + }; private: void calcLevels(const std::vector& octs, int frameW, int frameH, int nscales); typedef std::vector::const_iterator octIt_t; - int fitOctave(const std::vector& octs, const float& logFactor) + int fitOctave(const std::vector& octs, const float& logFactor) const { float minAbsLog = FLT_MAX; int res = 0; @@ -145,10 +167,10 @@ inline bool cv::gpu::SoftCascade::Filds::fill(const FileNode &root, const float CV_Assert(featureTypeStr == SC_ICF); origObjWidth = (int)root[SC_ORIG_W]; - CV_Assert(origObjWidth == SoftCascade::ORIG_OBJECT_WIDTH); + CV_Assert(origObjWidth == ORIG_OBJECT_WIDTH); origObjHeight = (int)root[SC_ORIG_H]; - CV_Assert(origObjHeight == SoftCascade::ORIG_OBJECT_HEIGHT); + CV_Assert(origObjHeight == ORIG_OBJECT_HEIGHT); FileNode fn = root[SC_OCTAVES]; if (fn.empty()) return false; @@ -165,6 +187,7 @@ inline bool cv::gpu::SoftCascade::Filds::fill(const FileNode &root, const float FileNodeIterator it = fn.begin(), it_end = fn.end(); int feature_offset = 0; ushort octIndex = 0; + ushort shrinkage = 1; for (; it != it_end; ++it) { @@ -173,9 +196,9 @@ inline bool cv::gpu::SoftCascade::Filds::fill(const FileNode &root, const float scales.push_back(scale); ushort nstages = saturate_cast((int)fn[SC_OCT_STAGES]); ushort2 size; - size.x = cvRound(SoftCascade::ORIG_OBJECT_WIDTH * scale); - size.y = cvRound(SoftCascade::ORIG_OBJECT_HEIGHT * scale); - ushort shrinkage = saturate_cast((int)fn[SC_OCT_SHRINKAGE]); + size.x = cvRound(ORIG_OBJECT_WIDTH * scale); + size.y = cvRound(ORIG_OBJECT_HEIGHT * scale); + shrinkage = saturate_cast((int)fn[SC_OCT_SHRINKAGE]); icf::Octave octave(octIndex, nstages, shrinkage, size, scale); CV_Assert(octave.stages > 0); @@ -247,7 +270,16 @@ inline bool cv::gpu::SoftCascade::Filds::fill(const FileNode &root, const float CV_Assert(!features.empty()); // compute levels - calcLevels(voctaves, (int)SoftCascade::FRAME_WIDTH, (int)SoftCascade::FRAME_HEIGHT, (int)SoftCascade::TOTAL_SCALES); + calcLevels(voctaves, FRAME_WIDTH, FRAME_HEIGHT, TOTAL_SCALES); + CV_Assert(!levels.empty()); + + // init Cascade + cascade = icf::Cascade(octaves, stages, nodes, leaves, features, levels); + + // allocate buffers + dmem.create(FRAME_HEIGHT * HOG_LUV_BINS, FRAME_WIDTH, CV_8UC1); + shrunk.create(FRAME_HEIGHT / shrinkage * HOG_LUV_BINS, FRAME_WIDTH / shrinkage, CV_8UC1); + hogluv.create( (FRAME_HEIGHT / shrinkage * HOG_LUV_BINS) + 1, (FRAME_WIDTH / shrinkage) + 1, CV_16UC1); return true; } @@ -291,7 +323,7 @@ inline void cv::gpu::SoftCascade::Filds::calcLevels(const std::vector 1); - std::vector levels; + std::vector vlevels; float logFactor = (::log(maxScale) - ::log(minScale)) / (nscales -1); float scale = minScale; @@ -310,11 +342,13 @@ inline void cv::gpu::SoftCascade::Filds::calcLevels(const std::vector