From cba926a428ebcc65661e650b1f0df6496c4e3f81 Mon Sep 17 00:00:00 2001 From: Maria Dimashova Date: Wed, 28 Jul 2010 16:41:12 +0000 Subject: [PATCH] added c++ interface for cvPyrMeanShiftFiltering; added sample on meanshift segmentation --- modules/features2d/src/calonder.cpp | 2 +- .../imgproc/include/opencv2/imgproc/imgproc.hpp | 5 ++ modules/imgproc/src/segmentation.cpp | 11 ++++ samples/cpp/meanshift_segmentation.cpp | 66 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 samples/cpp/meanshift_segmentation.cpp diff --git a/modules/features2d/src/calonder.cpp b/modules/features2d/src/calonder.cpp index abea8ad..fd9925c 100644 --- a/modules/features2d/src/calonder.cpp +++ b/modules/features2d/src/calonder.cpp @@ -333,7 +333,7 @@ void RandomizedTree::train(std::vector const& base_set, Size patchSize(PATCH_SIZE, PATCH_SIZE); for (keypt_it = base_set.begin(); keypt_it != base_set.end(); ++keypt_it, ++class_id) { for (int i = 0; i < views; ++i) { - make_patch( Mat(keypt_it->image), Point(keypt_it->y, keypt_it->x ), patch, patchSize, rng ); + make_patch( Mat(keypt_it->image), Point(keypt_it->x, keypt_it->y ), patch, patchSize, rng ); IplImage iplPatch = patch; addExample(class_id, getData(&iplPatch)); } diff --git a/modules/imgproc/include/opencv2/imgproc/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc/imgproc.hpp index 83bad0e..99dcd98 100644 --- a/modules/imgproc/include/opencv2/imgproc/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc/imgproc.hpp @@ -653,6 +653,11 @@ CV_EXPORTS void equalizeHist( const Mat& src, Mat& dst ); //! segments the image using watershed algorithm CV_EXPORTS void watershed( const Mat& image, Mat& markers ); +//! filters image using meanshift algorithm +CV_EXPORTS void pyrMeanShiftFiltering( const Mat& src, Mat& dst, + double sp, double sr, int maxLevel=1, + TermCriteria termcrit=TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS,5,1) ); + //! class of the pixel in GrabCut algorithm enum { GC_BGD = 0, //!< background GC_FGD = 1, //!< foreground diff --git a/modules/imgproc/src/segmentation.cpp b/modules/imgproc/src/segmentation.cpp index 17def20..978a89b 100644 --- a/modules/imgproc/src/segmentation.cpp +++ b/modules/imgproc/src/segmentation.cpp @@ -526,3 +526,14 @@ cvPyrMeanShiftFiltering( const CvArr* srcarr, CvArr* dstarr, } } +void cv::pyrMeanShiftFiltering( const Mat& src, Mat& dst, + double sp, double sr, int maxLevel, + TermCriteria termcrit ) +{ + if( src.empty() ) + return; + + dst.create( src.size(), src.type() ); + CvMat _src = src, _dst = dst; + cvPyrMeanShiftFiltering( &_src, &_dst, sp, sr, maxLevel, termcrit ); +} diff --git a/samples/cpp/meanshift_segmentation.cpp b/samples/cpp/meanshift_segmentation.cpp new file mode 100644 index 0000000..7998459 --- /dev/null +++ b/samples/cpp/meanshift_segmentation.cpp @@ -0,0 +1,66 @@ +#include +#include "opencv2/core/core.hpp" +#include "opencv2/imgproc/imgproc.hpp" +#include + +using namespace cv; +using namespace std; + +void floodFillPostprocess( Mat& img, const Scalar& colorDiff=Scalar::all(1) ) +{ + CV_Assert( !img.empty() ); + RNG rng = theRNG(); + Mat mask( img.rows+2, img.cols+2, CV_8UC1, Scalar::all(0) ); + for( int y = 0; y < img.rows; y++ ) + { + for( int x = 0; x < img.cols; x++ ) + { + if( mask.at(y+1, x+1) == 0 ) + { + Scalar newVal( rng(256), rng(256), rng(256) ); + floodFill( img, mask, Point(x,y), newVal, 0, colorDiff, colorDiff ); + } + } + } +} + +string winName = "meanshift"; +int spatialRad, colorRad, maxPyrLevel; +Mat img, res; + +void meanShiftSegmentation( int, void* ) +{ + cout << "spatialRad=" << spatialRad << "; " + << "colorRad=" << colorRad << "; " + << "maxPyrLevel=" << maxPyrLevel << endl; + pyrMeanShiftFiltering( img, res, spatialRad, colorRad, maxPyrLevel ); + floodFillPostprocess( res, Scalar::all(2) ); + imshow( winName, res ); +} + +int main(int argc, char** argv) +{ + if( argc !=2 ) + { + cout << "Format:" << endl << argv[0] << " image" << endl; + return -1; + } + + img = imread( argv[1] ); + if( img.empty() ) + return -1; + + spatialRad = 10; + colorRad = 10; + maxPyrLevel = 1; + + namedWindow( winName, CV_WINDOW_AUTOSIZE ); + + createTrackbar( "spatialRad", winName, &spatialRad, 80, meanShiftSegmentation ); + createTrackbar( "colorRad", winName, &colorRad, 60, meanShiftSegmentation ); + createTrackbar( "maxPyrLevel", winName, &maxPyrLevel, 5, meanShiftSegmentation ); + + meanShiftSegmentation(0, 0); + waitKey(); + return 0; +} -- 2.7.4