Showing posts with label Clustering. Show all posts
Showing posts with label Clustering. Show all posts

12/29/2014

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

10/19/2011

Incremental K-means matlab source code

Created Date : 2011.10.
Language : Matlab 2010
Tool : -
Library & Utilized :-
Reference :An Incremental K-means algorithm(D.T. Pham, S.S. Dimov and C.D. Nguyen)
Etc. :-




I made Incremental K-means algorithm as matlab source code.
I made the code base on above table which is introduced in the paper.
The incremental K-means is similar to K-means but the different point is number of cluster class is increasing. but we have to set the maximum number.

Below figure is result of clustering. the sample data is normal vector of the 3D point that is acquied by bumblebee.
I think the K-means algorithm is sensitive to error or outlier data. because the algorithm use euclidean distance. and The result of clustering is different every time because the initial position of the class is selected randomly.

I hope my code help to your problem and study.
thank you.
Give me your valuable comments. ^^

<source code>
Clustering result of the sample datas.





enlarge image of the above figure