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 -> http://seiya-kumada.blogspot.kr/2013/05/mean-shift-filtering-practice-by-opencv.html

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")   
#else   
#pragma comment(lib, "opencv_core249.lib")   
#pragma comment(lib, "opencv_imgproc249.lib")   
#pragma comment(lib, "opencv_gpu249.lib")   
#pragma comment(lib, "opencv_highgui249.lib")   
#endif  

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();
 pimgGpu.upload(img);
 //gpu meanshift only support 8uc4 type.
 gpu::cvtColor(pimgGpu, imgGpu, CV_BGR2BGRA);
 gpu::meanShiftFiltering(imgGpu, outImgGpu, 30, 30);
 outImgGpu.download(outimg2);
 ProccTimePrint(AAtime , "gpu");

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


 waitKey();
}


...


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
->http://feelmare.blogspot.kr/search/label/K-means

Comments

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)