2 #pragma package <opencv>
14 char wndname[] = "Drawing Demo";
16 CvScalar random_color(CvRNG* rng)
18 int icolor = cvRandInt(rng);
19 return CV_RGB(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
22 int main( int argc, char** argv )
24 int line_type = CV_AA; // change it to 8 to see non-antialiased graphics
34 int width = 1000, height = 700;
35 int width3 = width*3, height3 = height*3;
38 // Load the source image
39 IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 );
43 cvNamedWindow(wndname, 1 );
45 cvShowImage(wndname,image);
47 rng = cvRNG((unsigned)-1);
54 for (i = 0; i< NUMBER; i++)
56 pt1.x=cvRandInt(&rng) % width3 - width;
57 pt1.y=cvRandInt(&rng) % height3 - height;
58 pt2.x=cvRandInt(&rng) % width3 - width;
59 pt2.y=cvRandInt(&rng) % height3 - height;
61 cvLine( image, pt1, pt2, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
62 cvShowImage(wndname,image);
66 for (i = 0; i< NUMBER; i++)
68 pt1.x=cvRandInt(&rng) % width3 - width;
69 pt1.y=cvRandInt(&rng) % height3 - height;
70 pt2.x=cvRandInt(&rng) % width3 - width;
71 pt2.y=cvRandInt(&rng) % height3 - height;
73 cvRectangle( image,pt1, pt2, random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
74 cvShowImage(wndname,image);
78 for (i = 0; i< NUMBER; i++)
80 pt1.x=cvRandInt(&rng) % width3 - width;
81 pt1.y=cvRandInt(&rng) % height3 - height;
82 sz.width =cvRandInt(&rng)%200;
83 sz.height=cvRandInt(&rng)%200;
84 angle = (cvRandInt(&rng)%1000)*0.180;
86 cvEllipse( image, pt1, sz, angle, angle - 100, angle + 200,
87 random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
88 cvShowImage(wndname,image);
92 for (i = 0; i< NUMBER; i++)
94 pt[0][0].x=cvRandInt(&rng) % width3 - width;
95 pt[0][0].y=cvRandInt(&rng) % height3 - height;
96 pt[0][1].x=cvRandInt(&rng) % width3 - width;
97 pt[0][1].y=cvRandInt(&rng) % height3 - height;
98 pt[0][2].x=cvRandInt(&rng) % width3 - width;
99 pt[0][2].y=cvRandInt(&rng) % height3 - height;
100 pt[1][0].x=cvRandInt(&rng) % width3 - width;
101 pt[1][0].y=cvRandInt(&rng) % height3 - height;
102 pt[1][1].x=cvRandInt(&rng) % width3 - width;
103 pt[1][1].y=cvRandInt(&rng) % height3 - height;
104 pt[1][2].x=cvRandInt(&rng) % width3 - width;
105 pt[1][2].y=cvRandInt(&rng) % height3 - height;
107 cvPolyLine( image, pt, arr, 2, 1, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
108 cvShowImage(wndname,image);
112 for (i = 0; i< NUMBER; i++)
114 pt[0][0].x=cvRandInt(&rng) % width3 - width;
115 pt[0][0].y=cvRandInt(&rng) % height3 - height;
116 pt[0][1].x=cvRandInt(&rng) % width3 - width;
117 pt[0][1].y=cvRandInt(&rng) % height3 - height;
118 pt[0][2].x=cvRandInt(&rng) % width3 - width;
119 pt[0][2].y=cvRandInt(&rng) % height3 - height;
120 pt[1][0].x=cvRandInt(&rng) % width3 - width;
121 pt[1][0].y=cvRandInt(&rng) % height3 - height;
122 pt[1][1].x=cvRandInt(&rng) % width3 - width;
123 pt[1][1].y=cvRandInt(&rng) % height3 - height;
124 pt[1][2].x=cvRandInt(&rng) % width3 - width;
125 pt[1][2].y=cvRandInt(&rng) % height3 - height;
127 cvFillPoly( image, pt, arr, 2, random_color(&rng), line_type, 0 );
128 cvShowImage(wndname,image);
132 for (i = 0; i< NUMBER; i++)
134 pt1.x=cvRandInt(&rng) % width3 - width;
135 pt1.y=cvRandInt(&rng) % height3 - height;
137 cvCircle( image, pt1, cvRandInt(&rng)%300, random_color(&rng),
138 cvRandInt(&rng)%10-1, line_type, 0 );
139 cvShowImage(wndname,image);
143 for (i = 1; i< NUMBER; i++)
145 pt1.x=cvRandInt(&rng) % width3 - width;
146 pt1.y=cvRandInt(&rng) % height3 - height;
148 cvInitFont( &font, cvRandInt(&rng) % 8,
149 (cvRandInt(&rng)%100)*0.05+0.1, (cvRandInt(&rng)%100)*0.05+0.1,
150 (cvRandInt(&rng)%5)*0.1, cvRound(cvRandInt(&rng)%10), line_type );
152 cvPutText( image, "Testing text rendering!", pt1, &font, random_color(&rng));
153 cvShowImage(wndname,image);
157 cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type );
159 cvGetTextSize( "OpenCV forever!", &font, &text_size, &ymin );
161 pt1.x = (width - text_size.width)/2;
162 pt1.y = (height + text_size.height)/2;
163 image2 = cvCloneImage(image);
165 for( i = 0; i < 255; i++ )
167 cvSubS( image2, cvScalarAll(i), image, 0 );
168 cvPutText( image, "OpenCV forever!", pt1, &font, CV_RGB(255,i,i));
169 cvShowImage(wndname,image);
173 // Wait for a key stroke; the same function arranges events processing
175 cvReleaseImage(&image);
176 cvReleaseImage(&image2);
177 cvDestroyWindow(wndname);