PageRenderTime 163ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/myOpenCV/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp

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