pointset.cpp
上传用户:banwdc
上传日期:2016-06-25
资源大小:2871k
文件大小:2k
- #include "cxcore.h"
- #include "highgui.h"
- #include <stdio.h>
- CvSeq* point_seq = 0;
- IplImage* canvas = 0;
- CvScalar* colors = 0;
- int pos = 10;
- int is_equal( const void* _a, const void* _b, void* userdata )
- {
- CvPoint a = *(const CvPoint*)_a;
- CvPoint b = *(const CvPoint*)_b;
- double threshold = *(double*)userdata;
- return (double)(a.x - b.x)*(a.x - b.x) + (double)(a.y - b.y)*(a.y - b.y) <= threshold;
- }
- void on_track( int pos )
- {
- CvSeq* labels = 0;
- double threshold = pos*pos;
- int i, class_count = cvSeqPartition( point_seq, 0, &labels, is_equal, &threshold );
- printf("%4d classesn", class_count );
- cvZero( canvas );
- for( i = 0; i < labels->total; i++ )
- {
- CvPoint pt = *(CvPoint*)cvGetSeqElem( point_seq, i );
- CvScalar color = colors[*(int*)cvGetSeqElem( labels, i )];
- cvCircle( canvas, pt, 1, color, -1 );
- }
- cvShowImage( "points", canvas );
- }
- int main( int argc, char** argv )
- {
- CvMemStorage* storage = cvCreateMemStorage(0);
- point_seq = cvCreateSeq( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage );
- CvRNG rng = cvRNG(0xffffffff);
- int width = 500, height = 500;
- int i, count = 1000;
- canvas = cvCreateImage( cvSize(width,height), 8, 3 );
- colors = (CvScalar*)cvAlloc( count*sizeof(colors[0]) );
- for( i = 0; i < count; i++ )
- {
- CvPoint pt;
- int icolor;
- pt.x = cvRandInt( &rng ) % width;
- pt.y = cvRandInt( &rng ) % height;
- cvSeqPush( point_seq, &pt );
- icolor = cvRandInt( &rng ) | 0x00404040;
- colors[i] = CV_RGB(icolor & 255, (icolor >> 8)&255, (icolor >> 16)&255);
- }
- cvNamedWindow( "points", 1 );
- cvCreateTrackbar( "threshold", "points", &pos, 50, on_track );
- on_track(pos);
- cvWaitKey(0);
- return 0;
- }