Merge pull request #3104 from dkanafeev:new_ipp_func_master
[profile/ivi/opencv.git] / samples / cpp / image.cpp
1 #include <stdio.h>
2 #include <iostream>
3 #include <opencv2/imgproc/imgproc.hpp>
4 #include <opencv2/highgui/highgui.hpp>
5 #include <opencv2/core/utility.hpp>
6
7 using namespace cv; // all the new API is put into "cv" namespace. Export its content
8 using namespace std;
9
10 static void help()
11 {
12     cout <<
13     "\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n"
14     "It shows reading of images, converting to planes and merging back, color conversion\n"
15     "and also iterating through pixels.\n"
16     "Call:\n"
17     "./image [image-name Default: lena.jpg]\n" << endl;
18 }
19
20 // enable/disable use of mixed API in the code below.
21 #define DEMO_MIXED_API_USE 1
22
23 #ifdef DEMO_MIXED_API_USE
24 #  include <opencv2/highgui/highgui_c.h>
25 #  include <opencv2/imgcodecs/imgcodecs_c.h>
26 #endif
27
28 int main( int argc, char** argv )
29 {
30     help();
31     const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
32 #if DEMO_MIXED_API_USE
33     Ptr<IplImage> iplimg(cvLoadImage(imagename)); // Ptr<T> is safe ref-counting pointer class
34     if(!iplimg)
35     {
36         fprintf(stderr, "Can not load image %s\n", imagename);
37         return -1;
38     }
39     Mat img = cv::cvarrToMat(iplimg); // cv::Mat replaces the CvMat and IplImage, but it's easy to convert
40     // between the old and the new data structures (by default, only the header
41     // is converted, while the data is shared)
42 #else
43     Mat img = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function
44     if(img.empty())
45     {
46         fprintf(stderr, "Can not load image %s\n", imagename);
47         return -1;
48     }
49 #endif
50
51     if( img.empty() ) // check if the image has been loaded properly
52         return -1;
53
54     Mat img_yuv;
55     cvtColor(img, img_yuv, COLOR_BGR2YCrCb); // convert image to YUV color space. The output image will be created automatically
56
57     vector<Mat> planes; // Vector is template vector class, similar to STL's vector. It can store matrices too.
58     split(img_yuv, planes); // split the image into separate color planes
59
60 #if 1
61     // method 1. process Y plane using an iterator
62     MatIterator_<uchar> it = planes[0].begin<uchar>(), it_end = planes[0].end<uchar>();
63     for(; it != it_end; ++it)
64     {
65         double v = *it*1.7 + rand()%21-10;
66         *it = saturate_cast<uchar>(v*v/255.);
67     }
68
69     // method 2. process the first chroma plane using pre-stored row pointer.
70     // method 3. process the second chroma plane using individual element access
71     for( int y = 0; y < img_yuv.rows; y++ )
72     {
73         uchar* Uptr = planes[1].ptr<uchar>(y);
74         for( int x = 0; x < img_yuv.cols; x++ )
75         {
76             Uptr[x] = saturate_cast<uchar>((Uptr[x]-128)/2 + 128);
77             uchar& Vxy = planes[2].at<uchar>(y, x);
78             Vxy = saturate_cast<uchar>((Vxy-128)/2 + 128);
79         }
80     }
81
82 #else
83     Mat noise(img.size(), CV_8U); // another Mat constructor; allocates a matrix of the specified size and type
84     randn(noise, Scalar::all(128), Scalar::all(20)); // fills the matrix with normally distributed random values;
85                                                      // there is also randu() for uniformly distributed random number generation
86     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
87
88     const double brightness_gain = 0;
89     const double contrast_gain = 1.7;
90 #if DEMO_MIXED_API_USE
91     // it's easy to pass the new matrices to the functions that only work with IplImage or CvMat:
92     // step 1) - convert the headers, data will not be copied
93     IplImage cv_planes_0 = planes[0], cv_noise = noise;
94     // step 2) call the function; do not forget unary "&" to form pointers
95     cvAddWeighted(&cv_planes_0, contrast_gain, &cv_noise, 1, -128 + brightness_gain, &cv_planes_0);
96 #else
97     addWeighted(planes[0], contrast_gain, noise, 1, -128 + brightness_gain, planes[0]);
98 #endif
99     const double color_scale = 0.5;
100     // Mat::convertTo() replaces cvConvertScale. One must explicitly specify the output matrix type (we keep it intact - planes[1].type())
101     planes[1].convertTo(planes[1], planes[1].type(), color_scale, 128*(1-color_scale));
102     // alternative form of cv::convertScale if we know the datatype at compile time ("uchar" here).
103     // This expression will not create any temporary arrays and should be almost as fast as the above variant
104     planes[2] = Mat_<uchar>(planes[2]*color_scale + 128*(1-color_scale));
105
106     // Mat::mul replaces cvMul(). Again, no temporary arrays are created in case of simple expressions.
107     planes[0] = planes[0].mul(planes[0], 1./255);
108 #endif
109
110     // now merge the results back
111     merge(planes, img_yuv);
112     // and produce the output RGB image
113     cvtColor(img_yuv, img, COLOR_YCrCb2BGR);
114
115     // this is counterpart for cvNamedWindow
116     namedWindow("image with grain", WINDOW_AUTOSIZE);
117 #if DEMO_MIXED_API_USE
118     // this is to demonstrate that img and iplimg really share the data - the result of the above
119     // processing is stored in img and thus in iplimg too.
120     cvShowImage("image with grain", iplimg);
121 #else
122     imshow("image with grain", img);
123 #endif
124     waitKey();
125
126     return 0;
127     // all the memory will automatically be released by Vector<>, Mat and Ptr<> destructors.
128 }