PageRenderTime 110ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/cpp/image.cpp

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