Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / samples / c / image.cpp
1 // The short example shows how to use new-style image classes declared in cxcore.hpp.
2 // There is also a very similar matrix class (CvMatrix) - a wrapper for CvMat
3 #include "cv.h"
4 #include "highgui.h"
5
6 int main( int argc, char** argv )
7 {
8     // load image in constructor: the image can be loaded either from bitmap (see cvLoadImage),
9     // or from XML/YAML (see cvLoad)
10     CvImage img(argc > 1 ? argv[1] : "lena.jpg", 0, CV_LOAD_IMAGE_COLOR),
11         img_yuv, y, noise;
12     CvRNG rng = cvRNG(-1);
13
14     if( !img.data() ) // check if the image has been loaded properly
15         return -1;
16
17     img_yuv = img.clone(); // clone the image
18                            // (although, later the content will be replaced with cvCvtColor,
19                            // clone() is used for simplicity and for the illustration)
20     cvCvtColor( img, img_yuv, CV_BGR2YCrCb ); // simply call OpenCV functions and pass the class instances there
21
22     y.create( img.size(), IPL_DEPTH_8U, 1 ); // another method to create an image - from scratch
23     noise.create( img.size(), IPL_DEPTH_32F, 1 );
24
25     cvSplit( img_yuv, y, 0, 0, 0 );
26     cvRandArr( &rng, noise, CV_RAND_NORMAL, cvScalarAll(0), cvScalarAll(20) );
27     cvSmooth( noise, noise, CV_GAUSSIAN, 5, 5, 1, 1 );
28     cvAcc( y, noise );
29     cvConvert( noise, y );
30     cvMerge( y, 0, 0, 0, img_yuv );
31     cvCvtColor( img_yuv, img, CV_YCrCb2BGR );
32
33     cvNamedWindow( "image with grain", CV_WINDOW_AUTOSIZE );
34     img.show( "image with grain" ); // .show method is the conveninient form of cvShowImage
35     cvWaitKey();
36
37     return 0;
38     // all the images will be released automatically
39 }
40
41
42
43