From 2d0fc80c9510fb75556e785f01840b6ee8293172 Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Thu, 25 Oct 2012 01:56:22 +0400 Subject: [PATCH] use IntegralChannels class --- modules/objdetect/src/softcascade.cpp | 157 +------------------- modules/objdetect/test/test_softcascade.cpp | 3 +- 2 files changed, 6 insertions(+), 154 deletions(-) diff --git a/modules/objdetect/src/softcascade.cpp b/modules/objdetect/src/softcascade.cpp index cec7741f2..8d79b85e8 100644 --- a/modules/objdetect/src/softcascade.cpp +++ b/modules/objdetect/src/softcascade.cpp @@ -50,21 +50,6 @@ #include #include -// use previous stored integrals for regression testing -// #define USE_REFERENCE_VALUES - -#if defined USE_REFERENCE_VALUES - -namespace { - -char *itoa(long i, char* s, int /*dummy_radix*/) -{ - sprintf(s, "%ld", i); - return s; -} - -#endif - // used for noisy printfs // #define WITH_DEBUG_OUT @@ -235,47 +220,10 @@ struct Level float sarea = (scaledRect.width - scaledRect.x) * (scaledRect.height - scaledRect.y); // compensation areas rounding - return (threshold * scaling[idx] * sarea); + return (sarea == 0.0f)? threshold : (threshold * scaling[idx] * sarea); } }; -template< typename T> -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); - } - } - } - -}; - struct ChannelStorage { std::vector hog; @@ -289,111 +237,16 @@ struct ChannelStorage ChannelStorage(const cv::Mat& colored, int shr) : shrinkage(shr) { hog.clear(); - Decimate decimate(shr); - -#if defined USE_REFERENCE_VALUES - char buff[33]; - cv::FileStorage imgs("/home/kellan/testInts.xml", cv::FileStorage::READ); - - for(int i = 0; i < HOG_LUV_BINS; ++i) - { - cv::Mat channel; - imgs[std::string("channel") + itoa(i, buff, 10)] >> channel; - hog.push_back(channel); - } -#else - // add gauss - cv::Mat gauss; - cv::GaussianBlur(colored, gauss, cv::Size(3,3), 0 ,0); - - // convert to luv - cv::Mat luv; - cv::cvtColor(colored, luv, CV_BGR2Luv); - - // split to 3 one channel matrix - std::vector splited, luvs; - split(luv, splited); - - // shrink and integrate - for (int i = 0; i < (int)splited.size(); i++) - { - cv::Mat shrunk, sum; - decimate(splited[i], shrunk); - cv::integral(shrunk, sum, cv::noArray(), CV_32S); - luvs.push_back(sum); - } + cv::IntegralChannels ints(shr); // convert to grey cv::Mat grey; cv::cvtColor(colored, grey, CV_BGR2GRAY); - // get derivative - cv::Mat df_dx, df_dy, mag, angle; - cv::Sobel(grey, df_dx, CV_32F, 1, 0); - cv::Sobel(grey, df_dy, CV_32F, 0, 1); - - // normalize - df_dx /= 4; - df_dy /= 4; - - // calculate magnitude - cv::cartToPolar(df_dx, df_dy, mag, angle, true); - - // normalize to avoid uchar overflow - static const float magnitudeScaling = 1.f / sqrt(2); - mag *= magnitudeScaling; - - // convert to uchar - cv::Mat saturatedMag(grey.rows, grey.cols, CV_8UC1), shrMag; - for (int y = 0; y < grey.rows; ++y) - { - float* rm = mag.ptr(y); - uchar* mg = saturatedMag.ptr(y); - for (int x = 0; x < grey.cols; ++x) - { - mg[x] = cv::saturate_cast(rm[x]); - } - } - - // srink and integrate - decimate(saturatedMag, shrMag); - cv::integral(shrMag, mag, cv::noArray(), CV_32S); - - // create hog channels - angle /= 60.f; - - std::vector hist; - for (int bin = 0; bin < HOG_BINS; ++bin) - { - hist.push_back(cv::Mat::zeros(saturatedMag.rows, saturatedMag.cols, CV_8UC1)); - } - - for (int y = 0; y < saturatedMag.rows; ++y) - { - uchar* magnitude = saturatedMag.ptr(y); - float* ang = angle.ptr(y); - - for (int x = 0; x < saturatedMag.cols; ++x) - { - hist[ (int)ang[x] ].ptr(y)[x] = magnitude[x]; - } - } - - for(int i = 0; i < HOG_BINS; ++i) - { - cv::Mat shrunk, sum; - decimate(hist[i], shrunk); - cv::integral(shrunk, sum, cv::noArray(), CV_32S); - hog.push_back(sum); - } - - hog.push_back(mag); - hog.insert(hog.end(), luvs.begin(), luvs.end()); + ints.createHogBins(grey, hog, 6); + ints.createLuvBins(colored, hog); step = hog[0].cols; - - // CV_Assert(hog.size() == 10); -#endif } float get(const int channel, const cv::Rect& area) const @@ -441,7 +294,7 @@ struct cv::SoftCascade::Filds float detectionScore = 0.f; const Octave& octave = *(level.octave); - int stBegin = octave.index * octave.stages, stEnd = stBegin + octave.stages; + int stBegin = octave.index * octave.stages, stEnd = stBegin + 1024;//octave.stages; dprintf(" octave stages: %d to %d index %d %f level %f\n", stBegin, stEnd, octave.index, octave.scale, level.origScale); diff --git a/modules/objdetect/test/test_softcascade.cpp b/modules/objdetect/test/test_softcascade.cpp index 59428ea5d..811ad2c5e 100644 --- a/modules/objdetect/test/test_softcascade.cpp +++ b/modules/objdetect/test/test_softcascade.cpp @@ -65,7 +65,6 @@ TEST(SoftCascade, detect) cascade.detectMultiScale(colored, rois, objects); - std::cout << "detected: " << (int)objects.size() << std::endl; cv::Mat out = colored.clone(); int level = 0, total = 0; @@ -78,7 +77,6 @@ TEST(SoftCascade, detect) 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; @@ -91,4 +89,5 @@ TEST(SoftCascade, detect) << " " << objects[i].rect.height << std::endl; total++; } + std::cout << "detected: " << (int)objects.size() << std::endl; } \ No newline at end of file