2 #pragma package <opencv>
5 #define CV_NO_BACKWARD_COMPATIBILITY
16 char wndname[] = "Drawing Demo";
18 CvScalar random_color(CvRNG* rng)
20 int icolor = cvRandInt(rng);
21 return CV_RGB(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
24 int main( int argc, char** argv )
26 int line_type = CV_AA; // change it to 8 to see non-antialiased graphics
36 int width = 1000, height = 700;
37 int width3 = width*3, height3 = height*3;
40 // Load the source image
41 IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 );
45 cvNamedWindow(wndname, 1 );
47 cvShowImage(wndname,image);
50 rng = cvRNG((unsigned)-1);
57 for (i = 0; i< NUMBER; i++)
59 pt1.x=cvRandInt(&rng) % width3 - width;
60 pt1.y=cvRandInt(&rng) % height3 - height;
61 pt2.x=cvRandInt(&rng) % width3 - width;
62 pt2.y=cvRandInt(&rng) % height3 - height;
64 cvLine( image, pt1, pt2, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
65 cvShowImage(wndname,image);
66 if(cvWaitKey(DELAY) >= 0) return 0;
69 for (i = 0; i< NUMBER; i++)
71 pt1.x=cvRandInt(&rng) % width3 - width;
72 pt1.y=cvRandInt(&rng) % height3 - height;
73 pt2.x=cvRandInt(&rng) % width3 - width;
74 pt2.y=cvRandInt(&rng) % height3 - height;
76 cvRectangle( image,pt1, pt2, random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
77 cvShowImage(wndname,image);
78 if(cvWaitKey(DELAY) >= 0) return 0;
81 for (i = 0; i< NUMBER; i++)
83 pt1.x=cvRandInt(&rng) % width3 - width;
84 pt1.y=cvRandInt(&rng) % height3 - height;
85 sz.width =cvRandInt(&rng)%200;
86 sz.height=cvRandInt(&rng)%200;
87 angle = (cvRandInt(&rng)%1000)*0.180;
89 cvEllipse( image, pt1, sz, angle, angle - 100, angle + 200,
90 random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
91 cvShowImage(wndname,image);
92 if(cvWaitKey(DELAY) >= 0) return 0;
95 for (i = 0; i< NUMBER; i++)
97 pt[0][0].x=cvRandInt(&rng) % width3 - width;
98 pt[0][0].y=cvRandInt(&rng) % height3 - height;
99 pt[0][1].x=cvRandInt(&rng) % width3 - width;
100 pt[0][1].y=cvRandInt(&rng) % height3 - height;
101 pt[0][2].x=cvRandInt(&rng) % width3 - width;
102 pt[0][2].y=cvRandInt(&rng) % height3 - height;
103 pt[1][0].x=cvRandInt(&rng) % width3 - width;
104 pt[1][0].y=cvRandInt(&rng) % height3 - height;
105 pt[1][1].x=cvRandInt(&rng) % width3 - width;
106 pt[1][1].y=cvRandInt(&rng) % height3 - height;
107 pt[1][2].x=cvRandInt(&rng) % width3 - width;
108 pt[1][2].y=cvRandInt(&rng) % height3 - height;
110 cvPolyLine( image, pt, arr, 2, 1, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
111 cvShowImage(wndname,image);
112 if(cvWaitKey(DELAY) >= 0) return 0;
115 for (i = 0; i< NUMBER; i++)
117 pt[0][0].x=cvRandInt(&rng) % width3 - width;
118 pt[0][0].y=cvRandInt(&rng) % height3 - height;
119 pt[0][1].x=cvRandInt(&rng) % width3 - width;
120 pt[0][1].y=cvRandInt(&rng) % height3 - height;
121 pt[0][2].x=cvRandInt(&rng) % width3 - width;
122 pt[0][2].y=cvRandInt(&rng) % height3 - height;
123 pt[1][0].x=cvRandInt(&rng) % width3 - width;
124 pt[1][0].y=cvRandInt(&rng) % height3 - height;
125 pt[1][1].x=cvRandInt(&rng) % width3 - width;
126 pt[1][1].y=cvRandInt(&rng) % height3 - height;
127 pt[1][2].x=cvRandInt(&rng) % width3 - width;
128 pt[1][2].y=cvRandInt(&rng) % height3 - height;
130 cvFillPoly( image, pt, arr, 2, random_color(&rng), line_type, 0 );
131 cvShowImage(wndname,image);
132 if(cvWaitKey(DELAY) >= 0) return 0;
135 for (i = 0; i< NUMBER; i++)
137 pt1.x=cvRandInt(&rng) % width3 - width;
138 pt1.y=cvRandInt(&rng) % height3 - height;
140 cvCircle( image, pt1, cvRandInt(&rng)%300, random_color(&rng),
141 cvRandInt(&rng)%10-1, line_type, 0 );
142 cvShowImage(wndname,image);
143 if(cvWaitKey(DELAY) >= 0) return 0;
146 for (i = 1; i< NUMBER; i++)
148 pt1.x=cvRandInt(&rng) % width3 - width;
149 pt1.y=cvRandInt(&rng) % height3 - height;
151 cvInitFont( &font, cvRandInt(&rng) % 8,
152 (cvRandInt(&rng)%100)*0.05+0.1, (cvRandInt(&rng)%100)*0.05+0.1,
153 (cvRandInt(&rng)%5)*0.1, cvRound(cvRandInt(&rng)%10), line_type );
155 cvPutText( image, "Testing text rendering!", pt1, &font, random_color(&rng));
156 cvShowImage(wndname,image);
157 if(cvWaitKey(DELAY) >= 0) return 0;
160 cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type );
162 cvGetTextSize( "OpenCV forever!", &font, &text_size, &ymin );
164 pt1.x = (width - text_size.width)/2;
165 pt1.y = (height + text_size.height)/2;
166 image2 = cvCloneImage(image);
168 for( i = 0; i < 255; i++ )
170 cvSubS( image2, cvScalarAll(i), image, 0 );
171 cvPutText( image, "OpenCV forever!", pt1, &font, CV_RGB(255,i,i));
172 cvShowImage(wndname,image);
173 if(cvWaitKey(DELAY) >= 0) return 0;
176 // Wait for a key stroke; the same function arranges events processing
178 cvReleaseImage(&image);
179 cvReleaseImage(&image2);
180 cvDestroyWindow(wndname);