PageRenderTime 72ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp

https://gitlab.com/geekshabeka/opencv
C++ | 141 lines | 92 code | 28 blank | 21 comment | 6 complexity | ac6838341132d7eaa384ee3bcfaf3f56 MD5 | raw file
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/imgproc/imgproc.hpp>
  5. #include "opencv2/imgcodecs.hpp"
  6. #include <opencv2/highgui/highgui.hpp>
  7. #include <opencv2/core/utility.hpp>
  8. using namespace cv; // The new C++ interface API is inside this namespace. Import it.
  9. using namespace std;
  10. static void help( char* progName)
  11. {
  12. cout << endl << progName
  13. << " shows how to use cv::Mat and IplImages together (converting back and forth)." << endl
  14. << "Also contains example for image read, spliting the planes, merging back and " << endl
  15. << " color conversion, plus iterating through pixels. " << endl
  16. << "Usage:" << endl
  17. << progName << " [image-name Default: lena.jpg]" << endl << endl;
  18. }
  19. // comment out the define to use only the latest C++ API
  20. #define DEMO_MIXED_API_USE
  21. #ifdef DEMO_MIXED_API_USE
  22. # include <opencv2/highgui/highgui_c.h>
  23. # include <opencv2/imgcodecs/imgcodecs_c.h>
  24. #endif
  25. int main( int argc, char** argv )
  26. {
  27. help(argv[0]);
  28. const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
  29. #ifdef DEMO_MIXED_API_USE
  30. Ptr<IplImage> IplI(cvLoadImage(imagename)); // Ptr<T> is a safe ref-counting pointer class
  31. if(!IplI)
  32. {
  33. cerr << "Can not load image " << imagename << endl;
  34. return -1;
  35. }
  36. Mat I = cv::cvarrToMat(IplI); // Convert to the new style container. Only header created. Image not copied.
  37. #else
  38. Mat I = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function
  39. if( I.empty() ) // same as if( !I.data )
  40. {
  41. cerr << "Can not load image " << imagename << endl;
  42. return -1;
  43. }
  44. #endif
  45. // convert image to YUV color space. The output image will be created automatically.
  46. Mat I_YUV;
  47. cvtColor(I, I_YUV, COLOR_BGR2YCrCb);
  48. vector<Mat> planes; // Use the STL's vector structure to store multiple Mat objects
  49. split(I_YUV, planes); // split the image into separate color planes (Y U V)
  50. #if 1 // change it to 0 if you want to see a blurred and noisy version of this processing
  51. // Mat scanning
  52. // Method 1. process Y plane using an iterator
  53. MatIterator_<uchar> it = planes[0].begin<uchar>(), it_end = planes[0].end<uchar>();
  54. for(; it != it_end; ++it)
  55. {
  56. double v = *it * 1.7 + rand()%21 - 10;
  57. *it = saturate_cast<uchar>(v*v/255);
  58. }
  59. for( int y = 0; y < I_YUV.rows; y++ )
  60. {
  61. // Method 2. process the first chroma plane using pre-stored row pointer.
  62. uchar* Uptr = planes[1].ptr<uchar>(y);
  63. for( int x = 0; x < I_YUV.cols; x++ )
  64. {
  65. Uptr[x] = saturate_cast<uchar>((Uptr[x]-128)/2 + 128);
  66. // Method 3. process the second chroma plane using individual element access
  67. uchar& Vxy = planes[2].at<uchar>(y, x);
  68. Vxy = saturate_cast<uchar>((Vxy-128)/2 + 128);
  69. }
  70. }
  71. #else
  72. Mat noisyI(I.size(), CV_8U); // Create a matrix of the specified size and type
  73. // Fills the matrix with normally distributed random values (around number with deviation off).
  74. // There is also randu() for uniformly distributed random number generation
  75. randn(noisyI, Scalar::all(128), Scalar::all(20));
  76. // blur the noisyI a bit, kernel size is 3x3 and both sigma's are set to 0.5
  77. GaussianBlur(noisyI, noisyI, Size(3, 3), 0.5, 0.5);
  78. const double brightness_gain = 0;
  79. const double contrast_gain = 1.7;
  80. #ifdef DEMO_MIXED_API_USE
  81. // To pass the new matrices to the functions that only work with IplImage or CvMat do:
  82. // step 1) Convert the headers (tip: data will not be copied).
  83. // step 2) call the function (tip: to pass a pointer do not forget unary "&" to form pointers)
  84. IplImage cv_planes_0 = planes[0], cv_noise = noisyI;
  85. cvAddWeighted(&cv_planes_0, contrast_gain, &cv_noise, 1, -128 + brightness_gain, &cv_planes_0);
  86. #else
  87. addWeighted(planes[0], contrast_gain, noisyI, 1, -128 + brightness_gain, planes[0]);
  88. #endif
  89. const double color_scale = 0.5;
  90. // Mat::convertTo() replaces cvConvertScale.
  91. // One must explicitly specify the output matrix type (we keep it intact - planes[1].type())
  92. planes[1].convertTo(planes[1], planes[1].type(), color_scale, 128*(1-color_scale));
  93. // alternative form of cv::convertScale if we know the datatype at compile time ("uchar" here).
  94. // This expression will not create any temporary arrays ( so should be almost as fast as above)
  95. planes[2] = Mat_<uchar>(planes[2]*color_scale + 128*(1-color_scale));
  96. // Mat::mul replaces cvMul(). Again, no temporary arrays are created in case of simple expressions.
  97. planes[0] = planes[0].mul(planes[0], 1./255);
  98. #endif
  99. merge(planes, I_YUV); // now merge the results back
  100. cvtColor(I_YUV, I, COLOR_YCrCb2BGR); // and produce the output RGB image
  101. namedWindow("image with grain", WINDOW_AUTOSIZE); // use this to create images
  102. #ifdef DEMO_MIXED_API_USE
  103. // this is to demonstrate that I and IplI really share the data - the result of the above
  104. // processing is stored in I and thus in IplI too.
  105. cvShowImage("image with grain", IplI);
  106. #else
  107. imshow("image with grain", I); // the new MATLAB style function show
  108. #endif
  109. waitKey();
  110. // Tip: No memory freeing is required!
  111. // All the memory will be automatically released by the Vector<>, Mat and Ptr<> destructor.
  112. return 0;
  113. }