From 3d41846c390fdbbe7ed85295bc0187a26af03cf6 Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Wed, 24 Oct 2012 23:10:15 +0400 Subject: [PATCH] move frame processing into separate class --- .../include/opencv2/objdetect/objdetect.hpp | 22 +++ modules/objdetect/src/isf.cpp | 155 ++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 modules/objdetect/src/isf.cpp diff --git a/modules/objdetect/include/opencv2/objdetect/objdetect.hpp b/modules/objdetect/include/opencv2/objdetect/objdetect.hpp index f28d683e58..b56d5f1db6 100644 --- a/modules/objdetect/include/opencv2/objdetect/objdetect.hpp +++ b/modules/objdetect/include/opencv2/objdetect/objdetect.hpp @@ -543,6 +543,28 @@ private: Filds* filds; }; +class CV_EXPORTS IntegralChannels +{ +public: + //! constrictor form resizing factor. + //! Param shr is a resizing factor. Resize is applied before integral sum computing + IntegralChannels(const int shr) : shrinkage(shr) {} + + //! Appends specified number of hog first order feature integrals into given vector. + //! Param gray is an input 1-chennel gray image. + //! Param integrals is a vector of integrals. Computed from frame frame hog-channels will be appended to it. + //! Param bins is a number of hog-bins + void createHogBins(const cv::Mat gray, std::vector& integrals, int bins) const; + + //! Converts 3-chennel BGR input frame to Luv and append each channel to the integrals. + //! Param frame is an input 3-chennel BGR colored image. + //! Param integrals is a vector of integrals. Computed from frame frame luv-channels will be appended to it. + void createLuvBins(const cv::Mat frame, std::vector& integrals) const; + +private: + int shrinkage; +}; + //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector ////////////// // struct for detection region of interest (ROI) diff --git a/modules/objdetect/src/isf.cpp b/modules/objdetect/src/isf.cpp new file mode 100644 index 0000000000..48bfc89a8f --- /dev/null +++ b/modules/objdetect/src/isf.cpp @@ -0,0 +1,155 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// 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. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, 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: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * 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. +// +// 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 +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// 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 +#include +#include + + +template +struct Decimate { + int shrinkage; + + Decimate(const int sr) : shrinkage(sr) {} + + void operator()(const cv::Mat& in, cv::Mat& out) const + { + int cols = in.cols / shrinkage; + int rows = in.rows / shrinkage; + out.create(rows, cols, in.type()); + + CV_Assert(cols * shrinkage == in.cols); + CV_Assert(rows * shrinkage == in.rows); + + for (int outIdx_y = 0; outIdx_y < rows; ++outIdx_y) + { + T* outPtr = out.ptr(outIdx_y); + for (int outIdx_x = 0; outIdx_x < cols; ++outIdx_x) + { + // do desimate + int inIdx_y = outIdx_y * shrinkage; + int inIdx_x = outIdx_x * shrinkage; + int sum = 0; + + for (int y = inIdx_y; y < inIdx_y + shrinkage; ++y) + for (int x = inIdx_x; x < inIdx_x + shrinkage; ++x) + sum += in.at(y, x); + + sum /= shrinkage * shrinkage; + outPtr[outIdx_x] = cv::saturate_cast(sum); + } + } + } + +}; + +void cv::IntegralChannels::createHogBins(const cv::Mat gray, std::vector& integrals, int bins) const +{ + CV_Assert(gray.type() == CV_8UC1); + int h = gray.rows; + int w = gray.cols; + CV_Assert(!(w % shrinkage) && !(h % shrinkage)); + + Decimate decimate(shrinkage); + + cv::Mat df_dx, df_dy, mag, angle; + cv::Sobel(gray, df_dx, CV_32F, 1, 0, 3, 0.125); + cv::Sobel(gray, df_dy, CV_32F, 0, 1, 3, 0.125); + + cv::cartToPolar(df_dx, df_dy, mag, angle, true); + mag *= (1.f / sqrt(2)); + + cv::Mat nmag; + mag.convertTo(nmag, CV_8UC1); + + angle /= 60.f; + + std::vector hist; + for (int bin = 0; bin < bins; ++bin) + hist.push_back(cv::Mat::zeros(h, w, CV_8UC1)); + + for (int y = 0; y < h; ++y) + { + uchar* magnitude = nmag.ptr(y); + float* ang = angle.ptr(y); + + for (int x = 0; x < w; ++x) + { + hist[ (int)ang[x] ].ptr(y)[x] = magnitude[x]; + } + } + + for(int i = 0; i < bins; ++i) + { + cv::Mat shrunk, sum; + decimate(hist[i], shrunk); + cv::integral(shrunk, sum, cv::noArray(), CV_32S); + integrals.push_back(sum); + } + + cv::Mat shrMag; + decimate(nmag, shrMag); + cv::integral(shrMag, mag, cv::noArray(), CV_32S); + integrals.push_back(mag); +} + +void cv::IntegralChannels::createLuvBins(const cv::Mat frame, std::vector& integrals) const +{ + CV_Assert(frame.type() == CV_8UC3); + CV_Assert(!(frame.cols % shrinkage) && !(frame.rows % shrinkage)); + + Decimate decimate(shrinkage); + + cv::Mat luv; + cv::cvtColor(frame, luv, CV_BGR2Luv); + + std::vector splited; + split(luv, splited); + + for (size_t i = 0; i < splited.size(); ++i) + { + cv::Mat shrunk, sum; + decimate(splited[i], shrunk); + cv::integral(shrunk, sum, cv::noArray(), CV_32S); + integrals.push_back(sum); + } +} \ No newline at end of file