6 function ret = randint(v1, v2)
7 ret = int32(rand() * (v2 - v1) + v1);
13 CV_RGB(100,100,255), \
16 img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
19 cvNamedWindow( "clusters", 1 );
22 cluster_count = randint(2, MAX_CLUSTERS);
23 sample_count = randint(1, 1000);
24 points = cvCreateMat( sample_count, 1, CV_32FC2 );
25 clusters = cvCreateMat( sample_count, 1, CV_32SC1 );
27 ## generate random sample from multigaussian distribution
28 for k=0:cluster_count-1,
30 center.x = mod(cvRandInt(rng), img.width);
31 center.y = mod(cvRandInt(rng), img.height);
32 first = k*sample_count/cluster_count;
34 if (k != cluster_count)
35 last = (k+1)*sample_count/cluster_count;
38 point_chunk = cvGetRows(points, first, last);
40 cvRandArr( rng, point_chunk, CV_RAND_NORMAL, \
41 cvScalar(center.x,center.y,0,0), \
42 cvScalar(img.width*0.1,img.height*0.1,0,0));
47 cvRandShuffle( points, rng );
49 cvKMeans2( points, cluster_count, clusters, \
50 cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 ));
54 for i=0:sample_count-1,
55 cluster_idx = clusters(i);
57 cvCircle( img, pt, 2, color_tab{cluster_idx + 1}, CV_FILLED, CV_AA, 0 );
59 cvCircle( img, pt, 2, color_tab{cluster_idx + 1}, CV_FILLED, CV_AA, 0 );
63 cvShowImage( "clusters", img );
66 if( key == 27 || key == 'q' || key == 'Q' )
72 cvDestroyWindow( "clusters" );