switched to Input/Output Array in Mean Shift

This commit is contained in:
Vladislav Vinogradov 2013-04-30 16:29:23 +04:00
parent 70e6dc615a
commit 1fcc8074bd
3 changed files with 39 additions and 35 deletions

View File

@ -409,17 +409,17 @@ CV_EXPORTS Ptr<CornersDetector> createGoodFeaturesToTrackDetector(int srcType, i
///////////////////////////// Mean Shift ////////////////////////////// ///////////////////////////// Mean Shift //////////////////////////////
//! Does mean shift filtering on GPU. //! Does mean shift filtering on GPU.
CV_EXPORTS void meanShiftFiltering(const GpuMat& src, GpuMat& dst, int sp, int sr, CV_EXPORTS void meanShiftFiltering(InputArray src, OutputArray dst, int sp, int sr,
TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1), TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1),
Stream& stream = Stream::Null()); Stream& stream = Stream::Null());
//! Does mean shift procedure on GPU. //! Does mean shift procedure on GPU.
CV_EXPORTS void meanShiftProc(const GpuMat& src, GpuMat& dstr, GpuMat& dstsp, int sp, int sr, CV_EXPORTS void meanShiftProc(InputArray src, OutputArray dstr, OutputArray dstsp, int sp, int sr,
TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1), TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1),
Stream& stream = Stream::Null()); Stream& stream = Stream::Null());
//! Does mean shift segmentation with elimination of small regions. //! Does mean shift segmentation with elimination of small regions.
CV_EXPORTS void meanShiftSegmentation(const GpuMat& src, Mat& dst, int sp, int sr, int minsize, CV_EXPORTS void meanShiftSegmentation(InputArray src, OutputArray dst, int sp, int sr, int minsize,
TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1)); TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
/////////////////////////// Match Template //////////////////////////// /////////////////////////// Match Template ////////////////////////////

View File

@ -47,13 +47,13 @@ using namespace cv::gpu;
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) #if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
void cv::gpu::meanShiftFiltering(const GpuMat&, GpuMat&, int, int, TermCriteria, Stream&) { throw_no_cuda(); } void cv::gpu::meanShiftFiltering(InputArray, OutputArray, int, int, TermCriteria, Stream&) { throw_no_cuda(); }
void cv::gpu::meanShiftProc(const GpuMat&, GpuMat&, GpuMat&, int, int, TermCriteria, Stream&) { throw_no_cuda(); } void cv::gpu::meanShiftProc(InputArray, OutputArray, OutputArray, int, int, TermCriteria, Stream&) { throw_no_cuda(); }
#else /* !defined (HAVE_CUDA) */ #else /* !defined (HAVE_CUDA) */
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// meanShiftFiltering_GPU // meanShiftFiltering
namespace cv { namespace gpu { namespace cudev namespace cv { namespace gpu { namespace cudev
{ {
@ -63,27 +63,26 @@ namespace cv { namespace gpu { namespace cudev
} }
}}} }}}
void cv::gpu::meanShiftFiltering(const GpuMat& src, GpuMat& dst, int sp, int sr, TermCriteria criteria, Stream& stream) void cv::gpu::meanShiftFiltering(InputArray _src, OutputArray _dst, int sp, int sr, TermCriteria criteria, Stream& stream)
{ {
using namespace ::cv::gpu::cudev::imgproc; using namespace ::cv::gpu::cudev::imgproc;
if( src.empty() ) GpuMat src = _src.getGpuMat();
CV_Error( cv::Error::StsBadArg, "The input image is empty" );
if( src.depth() != CV_8U || src.channels() != 4 ) CV_Assert( src.type() == CV_8UC4 );
CV_Error( cv::Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
dst.create( src.size(), CV_8UC4 ); _dst.create(src.size(), CV_8UC4);
GpuMat dst = _dst.getGpuMat();
if( !(criteria.type & TermCriteria::MAX_ITER) ) if (!(criteria.type & TermCriteria::MAX_ITER))
criteria.maxCount = 5; criteria.maxCount = 5;
int maxIter = std::min(std::max(criteria.maxCount, 1), 100); int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
float eps; if (!(criteria.type & TermCriteria::EPS))
if( !(criteria.type & TermCriteria::EPS) ) criteria.epsilon = 1.f;
eps = 1.f;
eps = (float)std::max(criteria.epsilon, 0.0); float eps = (float) std::max(criteria.epsilon, 0.0);
meanShiftFiltering_gpu(src, dst, sp, sr, maxIter, eps, StreamAccessor::getStream(stream)); meanShiftFiltering_gpu(src, dst, sp, sr, maxIter, eps, StreamAccessor::getStream(stream));
} }
@ -99,28 +98,29 @@ namespace cv { namespace gpu { namespace cudev
} }
}}} }}}
void cv::gpu::meanShiftProc(const GpuMat& src, GpuMat& dstr, GpuMat& dstsp, int sp, int sr, TermCriteria criteria, Stream& stream) void cv::gpu::meanShiftProc(InputArray _src, OutputArray _dstr, OutputArray _dstsp, int sp, int sr, TermCriteria criteria, Stream& stream)
{ {
using namespace ::cv::gpu::cudev::imgproc; using namespace ::cv::gpu::cudev::imgproc;
if( src.empty() ) GpuMat src = _src.getGpuMat();
CV_Error( cv::Error::StsBadArg, "The input image is empty" );
if( src.depth() != CV_8U || src.channels() != 4 ) CV_Assert( src.type() == CV_8UC4 );
CV_Error( cv::Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
dstr.create( src.size(), CV_8UC4 ); _dstr.create(src.size(), CV_8UC4);
dstsp.create( src.size(), CV_16SC2 ); _dstsp.create(src.size(), CV_16SC2);
if( !(criteria.type & TermCriteria::MAX_ITER) ) GpuMat dstr = _dstr.getGpuMat();
GpuMat dstsp = _dstsp.getGpuMat();
if (!(criteria.type & TermCriteria::MAX_ITER))
criteria.maxCount = 5; criteria.maxCount = 5;
int maxIter = std::min(std::max(criteria.maxCount, 1), 100); int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
float eps; if (!(criteria.type & TermCriteria::EPS))
if( !(criteria.type & TermCriteria::EPS) ) criteria.epsilon = 1.f;
eps = 1.f;
eps = (float)std::max(criteria.epsilon, 0.0); float eps = (float) std::max(criteria.epsilon, 0.0);
meanShiftProc_gpu(src, dstr, dstsp, sp, sr, maxIter, eps, StreamAccessor::getStream(stream)); meanShiftProc_gpu(src, dstr, dstsp, sp, sr, maxIter, eps, StreamAccessor::getStream(stream));
} }

View File

@ -43,7 +43,7 @@
#if !defined HAVE_CUDA || defined(CUDA_DISABLER) #if !defined HAVE_CUDA || defined(CUDA_DISABLER)
void cv::gpu::meanShiftSegmentation(const GpuMat&, Mat&, int, int, int, TermCriteria) { throw_no_cuda(); } void cv::gpu::meanShiftSegmentation(InputArray, OutputArray, int, int, int, TermCriteria) { throw_no_cuda(); }
#else #else
@ -222,9 +222,12 @@ inline int dist2(const cv::Vec2s& lhs, const cv::Vec2s& rhs)
} // anonymous namespace } // anonymous namespace
void cv::gpu::meanShiftSegmentation(const GpuMat& src, Mat& dst, int sp, int sr, int minsize, TermCriteria criteria) void cv::gpu::meanShiftSegmentation(InputArray _src, OutputArray _dst, int sp, int sr, int minsize, TermCriteria criteria)
{ {
CV_Assert(src.type() == CV_8UC4); GpuMat src = _src.getGpuMat();
CV_Assert( src.type() == CV_8UC4 );
const int nrows = src.rows; const int nrows = src.rows;
const int ncols = src.cols; const int ncols = src.cols;
const int hr = sr; const int hr = sr;
@ -232,7 +235,7 @@ void cv::gpu::meanShiftSegmentation(const GpuMat& src, Mat& dst, int sp, int sr,
// Perform mean shift procedure and obtain region and spatial maps // Perform mean shift procedure and obtain region and spatial maps
GpuMat d_rmap, d_spmap; GpuMat d_rmap, d_spmap;
meanShiftProc(src, d_rmap, d_spmap, sp, sr, criteria); gpu::meanShiftProc(src, d_rmap, d_spmap, sp, sr, criteria);
Mat rmap(d_rmap); Mat rmap(d_rmap);
Mat spmap(d_spmap); Mat spmap(d_spmap);
@ -337,7 +340,7 @@ void cv::gpu::meanShiftSegmentation(const GpuMat& src, Mat& dst, int sp, int sr,
} }
// Sort all graph's edges connecting differnet components (in asceding order) // Sort all graph's edges connecting differnet components (in asceding order)
sort(edges.begin(), edges.end()); std::sort(edges.begin(), edges.end());
// Exclude small components (starting from the nearest couple) // Exclude small components (starting from the nearest couple)
for (size_t i = 0; i < edges.size(); ++i) for (size_t i = 0; i < edges.size(); ++i)
@ -366,7 +369,8 @@ void cv::gpu::meanShiftSegmentation(const GpuMat& src, Mat& dst, int sp, int sr,
} }
// Create final image, color of each segment is the average color of its pixels // Create final image, color of each segment is the average color of its pixels
dst.create(src.size(), src.type()); _dst.create(src.size(), src.type());
Mat dst = _dst.getMat();
for (int y = 0; y < nrows; ++y) for (int y = 0; y < nrows; ++y)
{ {