3 #include <opencv2/imgproc/imgproc.hpp>
4 #include <opencv2/highgui/highgui.hpp>
5 #include <opencv2/flann/miniflann.hpp>
6 #include <opencv2/core/utility.hpp>
8 using namespace cv; // all the new API is put into "cv" namespace. Export its content
10 using namespace cv::flann;
15 "\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n"
16 "It shows reading of images, converting to planes and merging back, color conversion\n"
17 "and also iterating through pixels.\n"
19 "./image [image-name Default: lena.jpg]\n" << endl;
22 // enable/disable use of mixed API in the code below.
23 #define DEMO_MIXED_API_USE 1
25 #ifdef DEMO_MIXED_API_USE
26 # include <opencv2/highgui/highgui_c.h>
29 int main( int argc, char** argv )
32 const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
33 #if DEMO_MIXED_API_USE
34 Ptr<IplImage> iplimg(cvLoadImage(imagename)); // Ptr<T> is safe ref-counting pointer class
37 fprintf(stderr, "Can not load image %s\n", imagename);
40 Mat img = cv::cvarrToMat(iplimg); // cv::Mat replaces the CvMat and IplImage, but it's easy to convert
41 // between the old and the new data structures (by default, only the header
42 // is converted, while the data is shared)
44 Mat img = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function
47 fprintf(stderr, "Can not load image %s\n", imagename);
52 if( !img.data ) // check if the image has been loaded properly
56 cvtColor(img, img_yuv, COLOR_BGR2YCrCb); // convert image to YUV color space. The output image will be created automatically
58 vector<Mat> planes; // Vector is template vector class, similar to STL's vector. It can store matrices too.
59 split(img_yuv, planes); // split the image into separate color planes
62 // method 1. process Y plane using an iterator
63 MatIterator_<uchar> it = planes[0].begin<uchar>(), it_end = planes[0].end<uchar>();
64 for(; it != it_end; ++it)
66 double v = *it*1.7 + rand()%21-10;
67 *it = saturate_cast<uchar>(v*v/255.);
70 // method 2. process the first chroma plane using pre-stored row pointer.
71 // method 3. process the second chroma plane using individual element access
72 for( int y = 0; y < img_yuv.rows; y++ )
74 uchar* Uptr = planes[1].ptr<uchar>(y);
75 for( int x = 0; x < img_yuv.cols; x++ )
77 Uptr[x] = saturate_cast<uchar>((Uptr[x]-128)/2 + 128);
78 uchar& Vxy = planes[2].at<uchar>(y, x);
79 Vxy = saturate_cast<uchar>((Vxy-128)/2 + 128);
84 Mat noise(img.size(), CV_8U); // another Mat constructor; allocates a matrix of the specified size and type
85 randn(noise, Scalar::all(128), Scalar::all(20)); // fills the matrix with normally distributed random values;
86 // there is also randu() for uniformly distributed random number generation
87 GaussianBlur(noise, noise, Size(3, 3), 0.5, 0.5); // blur the noise a bit, kernel size is 3x3 and both sigma's are set to 0.5
89 const double brightness_gain = 0;
90 const double contrast_gain = 1.7;
91 #if DEMO_MIXED_API_USE
92 // it's easy to pass the new matrices to the functions that only work with IplImage or CvMat:
93 // step 1) - convert the headers, data will not be copied
94 IplImage cv_planes_0 = planes[0], cv_noise = noise;
95 // step 2) call the function; do not forget unary "&" to form pointers
96 cvAddWeighted(&cv_planes_0, contrast_gain, &cv_noise, 1, -128 + brightness_gain, &cv_planes_0);
98 addWeighted(planes[0], contrast_gain, noise, 1, -128 + brightness_gain, planes[0]);
100 const double color_scale = 0.5;
101 // Mat::convertTo() replaces cvConvertScale. One must explicitly specify the output matrix type (we keep it intact - planes[1].type())
102 planes[1].convertTo(planes[1], planes[1].type(), color_scale, 128*(1-color_scale));
103 // alternative form of cv::convertScale if we know the datatype at compile time ("uchar" here).
104 // This expression will not create any temporary arrays and should be almost as fast as the above variant
105 planes[2] = Mat_<uchar>(planes[2]*color_scale + 128*(1-color_scale));
107 // Mat::mul replaces cvMul(). Again, no temporary arrays are created in case of simple expressions.
108 planes[0] = planes[0].mul(planes[0], 1./255);
111 // now merge the results back
112 merge(planes, img_yuv);
113 // and produce the output RGB image
114 cvtColor(img_yuv, img, COLOR_YCrCb2BGR);
116 // this is counterpart for cvNamedWindow
117 namedWindow("image with grain", WINDOW_AUTOSIZE);
118 #if DEMO_MIXED_API_USE
119 // this is to demonstrate that img and iplimg really share the data - the result of the above
120 // processing is stored in img and thus in iplimg too.
121 cvShowImage("image with grain", iplimg);
123 imshow("image with grain", img);
128 // all the memory will automatically be released by Vector<>, Mat and Ptr<> destructors.