From fa62e2b72f5d1d84b1d056d0e42b80486e210f71 Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Thu, 11 Oct 2012 16:47:49 +0400 Subject: [PATCH] move preprocessing into separate function --- modules/gpu/src/softcascade.cpp | 134 +++++++++++++++++--------------- 1 file changed, 72 insertions(+), 62 deletions(-) diff --git a/modules/gpu/src/softcascade.cpp b/modules/gpu/src/softcascade.cpp index b834e0319..8fa82867f 100644 --- a/modules/gpu/src/softcascade.cpp +++ b/modules/gpu/src/softcascade.cpp @@ -93,6 +93,10 @@ namespace icf { PtrStepSzi counter, const int downscales); } +namespace imgproc +{ + void meanShiftFiltering_gpu(const PtrStepSzb& src, PtrStepSzb dst, int sp, int sr, int maxIter, float eps, cudaStream_t stream); +} }}} struct cv::gpu::SoftCascade::Filds @@ -104,8 +108,8 @@ struct cv::gpu::SoftCascade::Filds fplane.create(FRAME_HEIGHT * 6, FRAME_WIDTH, CV_32FC1); luv.create(FRAME_HEIGHT, FRAME_WIDTH, CV_8UC3); shrunk.create(FRAME_HEIGHT / 4 * HOG_LUV_BINS, FRAME_WIDTH / 4, CV_8UC1); - integralBuffer.create(shrunk.rows + 1 * HOG_LUV_BINS, shrunk.cols + 1, CV_32SC1); - hogluv.create((FRAME_HEIGHT / 4 + 1) * HOG_LUV_BINS, FRAME_WIDTH / 4 + 1, CV_32SC1); + integralBuffer.create(1 , (shrunk.rows + 1) * HOG_LUV_BINS * (shrunk.cols + 1), CV_32SC1); + hogluv.create((FRAME_HEIGHT / 4 + 1) * HOG_LUV_BINS, FRAME_WIDTH / 4 + 64, CV_32SC1); detCounter.create(1,1, CV_32SC1); } @@ -146,6 +150,8 @@ struct cv::gpu::SoftCascade::Filds std::vector scales; + static const int shrinkage = 4; + enum { BOOST = 0 }; enum { @@ -160,19 +166,80 @@ struct cv::gpu::SoftCascade::Filds }; bool fill(const FileNode &root, const float mins, const float maxs); - void detect(cv::gpu::GpuMat roi, cv::gpu::GpuMat objects, cudaStream_t stream) const + void detect(const cv::gpu::GpuMat& roi, cv::gpu::GpuMat& objects, cudaStream_t stream) const { cudaMemset(detCounter.data, 0, detCounter.step * detCounter.rows * sizeof(int)); device::icf::detect(roi, levels, octaves, stages, nodes, leaves, hogluv, objects , detCounter, downscales); } - void detectAtScale(int scale, cv::gpu::GpuMat roi, cv::gpu::GpuMat objects, cudaStream_t stream) const + void detectAtScale(int scale, const cv::gpu::GpuMat& roi, cv::gpu::GpuMat& objects, cudaStream_t stream) const { cudaMemset(detCounter.data, 0, detCounter.step * detCounter.rows * sizeof(int)); device::icf::detectAtScale(scale, roi, levels, octaves, stages, nodes, leaves, hogluv, objects, detCounter, downscales); } + void preprocess(const cv::gpu::GpuMat& colored) + { + cudaMemset(plane.data, 0, plane.step * plane.rows); + + int fw = Filds::FRAME_WIDTH; + int fh = Filds::FRAME_HEIGHT; + + GpuMat gray(plane, cv::Rect(0, fh * Filds::HOG_LUV_BINS, fw, fh)); + cv::gpu::cvtColor(colored, gray, CV_BGR2GRAY); + + //create hog + GpuMat dfdx(fplane, cv::Rect(0, 0, fw, fh)); + GpuMat dfdy(fplane, cv::Rect(0, fh, fw, fh)); + + cv::gpu::Sobel(gray, dfdx, CV_32F, 1, 0, 3, 0.125f); + cv::gpu::Sobel(gray, dfdy, CV_32F, 0, 1, 3, 0.125f); + + GpuMat mag(fplane, cv::Rect(0, 2 * fh, fw, fh)); + GpuMat ang(fplane, cv::Rect(0, 3 * fh, fw, fh)); + + cv::gpu::cartToPolar(dfdx, dfdy, mag, ang, true); + + // normolize magnitude to uchar interval and angles to 6 bins + + GpuMat nmag(fplane, cv::Rect(0, 4 * fh, fw, fh)); + GpuMat nang(fplane, cv::Rect(0, 5 * fh, fw, fh)); + + cv::gpu::multiply(mag, cv::Scalar::all(1.f / ::log(2)), nmag); + cv::gpu::multiply(ang, cv::Scalar::all(1.f / 60.f), nang); + + //create uchar magnitude + GpuMat cmag(plane, cv::Rect(0, fh * Filds::HOG_BINS, fw, fh)); + nmag.convertTo(cmag, CV_8UC1); + + // create luv + cv::gpu::cvtColor(colored, luv, CV_BGR2Luv); + + std::vector splited; + for(int i = 0; i < Filds::LUV_BINS; ++i) + { + splited.push_back(GpuMat(plane, cv::Rect(0, fh * (7 + i), fw, fh))); + } + + cv::gpu::split(luv, splited); + + device::icf::fillBins(plane, nang, fw, fh, Filds::HOG_BINS); + + GpuMat channels(plane, cv::Rect(0, 0, fw, fh * Filds::HOG_LUV_BINS)); + cv::gpu::resize(channels, shrunk, cv::Size(), 0.25, 0.25, CV_INTER_AREA); + + fw /= shrinkage; + fh /= shrinkage; + + for(int i = 0; i < Filds::HOG_LUV_BINS; ++i) + { + GpuMat channel(shrunk, cv::Rect(0, fh * i, fw, fh )); + GpuMat sum(hogluv, cv::Rect(0, (fh + 1) * i, fw + 1, fh + 1)); + cv::gpu::integralBuffered(channel, sum, integralBuffer); + } + } + private: void calcLevels(const std::vector& octs, int frameW, int frameH, int nscales); @@ -479,64 +546,7 @@ void cv::gpu::SoftCascade::detectMultiScale(const GpuMat& colored, const GpuMat& Filds& flds = *filds; - GpuMat& plane = flds.plane; - GpuMat& shrunk = flds.shrunk; - cudaMemset(plane.data, 0, plane.step * plane.rows); - - int fw = Filds::FRAME_WIDTH; - int fh = Filds::FRAME_HEIGHT; - - GpuMat gray(plane, cv::Rect(0, fh * Filds::HOG_LUV_BINS, fw, fh)); - cv::gpu::cvtColor(colored, gray, CV_BGR2GRAY); - - //create hog - GpuMat dfdx(flds.fplane, cv::Rect(0, 0, fw, fh)); - GpuMat dfdy(flds.fplane, cv::Rect(0, fh, fw, fh)); - - cv::gpu::Sobel(gray, dfdx, CV_32F, 1, 0, 3, 0.125f); - cv::gpu::Sobel(gray, dfdy, CV_32F, 0, 1, 3, 0.125f); - - GpuMat mag(flds.fplane, cv::Rect(0, 2 * fh, fw, fh)); - GpuMat ang(flds.fplane, cv::Rect(0, 3 * fh, fw, fh)); - - cv::gpu::cartToPolar(dfdx, dfdy, mag, ang, true); - - // normolize magnitude to uchar interval and angles to 6 bins - - GpuMat nmag(flds.fplane, cv::Rect(0, 4 * fh, fw, fh)); - GpuMat nang(flds.fplane, cv::Rect(0, 5 * fh, fw, fh)); - - cv::gpu::multiply(mag, cv::Scalar::all(1.f / ::log(2)), nmag); - cv::gpu::multiply(ang, cv::Scalar::all(1.f / 60.f), nang); - - //create uchar magnitude - GpuMat cmag(plane, cv::Rect(0, fh * Filds::HOG_BINS, fw, fh)); - nmag.convertTo(cmag, CV_8UC1); - - // create luv - cv::gpu::cvtColor(colored, flds.luv, CV_BGR2Luv); - - std::vector splited; - for(int i = 0; i < Filds::LUV_BINS; ++i) - { - splited.push_back(GpuMat(plane, cv::Rect(0, fh * (7 + i), fw, fh))); - } - - cv::gpu::split(flds.luv, splited); - - device::icf::fillBins(plane, nang, fw, fh, Filds::HOG_BINS); - - GpuMat hogluv(plane, cv::Rect(0, 0, fw, fh * Filds::HOG_LUV_BINS)); - cv::gpu::resize(hogluv, flds.shrunk, cv::Size(), 0.25, 0.25, CV_INTER_AREA); - - fw /= 4; - fh /= 4; - for(int i = 0; i < Filds::HOG_LUV_BINS; ++i) - { - GpuMat channel(shrunk, cv::Rect(0, fh * i, fw, fh )); - GpuMat sum(flds.hogluv, cv::Rect(0, (fh + 1) * i, fw + 1, fh + 1)); - cv::gpu::integralBuffered(channel, sum, flds.integralBuffer); - } + flds.preprocess(colored); if (specificScale == -1) flds.detect(rois,objects, 0);