OpenCV meanShiftFiltering example source code ( cpu: pyrMeanShiftFiltering, gpu:meanShiftFiltering, gpu:meanShiftSegmentation )

'meanshift' is clustering algorithm. It can be used color segmentation, color tracking..
This article is about color segmentation using meanShiftFiltering function in the opencv.

There are 2 example of cpu, gpu version in the source code.
Note, the input image in the gpu version must be 8uc4 type.

please refer to this page for input parameter.
and this webpage ->

thank you.

#include < time.h>   
#include < opencv2\opencv.hpp>   
#include < opencv2\gpu\gpu.hpp>   
#include < string>   
#include < stdio.h>   
#ifdef _DEBUG           
#pragma comment(lib, "opencv_core249d.lib")   
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing   
#pragma comment(lib, "opencv_gpu249d.lib")   
#pragma comment(lib, "opencv_highgui249d.lib")   
#pragma comment(lib, "opencv_core249.lib")   
#pragma comment(lib, "opencv_imgproc249.lib")   
#pragma comment(lib, "opencv_gpu249.lib")   
#pragma comment(lib, "opencv_highgui249.lib")   

using namespace cv;
using namespace std;

void ProccTimePrint( unsigned long Atime , string msg)   
 unsigned long Btime=0;   
 float sec, fps;   
 Btime = getTickCount();   
 sec = (Btime - Atime)/getTickFrequency();   
 fps = 1/sec;   
 printf("%s %.4lf(sec) / %.4lf(fps) \n", msg.c_str(),  sec, fps );   

void main()
 unsigned long AAtime=0;
 //image load
 Mat img = imread("image2.jpg");
 Mat outImg, outimg2;

 //cpu version meanshift
 AAtime = getTickCount();
 pyrMeanShiftFiltering(img, outImg, 30, 30, 3);
 ProccTimePrint(AAtime , "cpu");

 //gpu version meanshift
 gpu::GpuMat pimgGpu, imgGpu, outImgGpu;
 AAtime = getTickCount();
 //gpu meanshift only support 8uc4 type.
 gpu::cvtColor(pimgGpu, imgGpu, CV_BGR2BGRA);
 gpu::meanShiftFiltering(imgGpu, outImgGpu, 30, 30);;
 ProccTimePrint(AAtime , "gpu");

 //show image
 imshow("origin", img);
 imshow("MeanShift Filter cpu", outImg);
 imshow("MeanShift Filter gpu", outimg2);



Below source code is about gpu::meanShiftSegmentation.
In this function, we can set minimum segment size of pixel count.
The smaller segments are merged.

Mat outImg3;
 AAtime = getTickCount();
 gpu::meanShiftSegmentation(imgGpu, outImg3, 30, 30, 300);
 ProccTimePrint(AAtime , "gpu segment");
 imshow("MeanShift segmentation gpu", outImg3);

Related contents k-means


Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

AMP dose run on my pc?, AMP(Accelerated Massive Parallelism)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Optical Flow sample source code using OpenCV

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Image warping (using opencv findHomography, warpPerspective)

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)