PageRenderTime 109ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/opencv/src/image.cpp

https://gitlab.com/juan-cardelino/misc_projects
C++ | 109 lines | 74 code | 15 blank | 20 comment | 7 complexity | 35a2427d52078ab161c939df2cf35674 MD5 | raw file
  1. #include "cv.h" // include standard OpenCV headers, same as before
  2. #include "highgui.h"
  3. using namespace cv; // all the new API is put into "cv" namespace. Export its content
  4. // enable/disable use of mixed API in the code below.
  5. #define DEMO_MIXED_API_USE 1
  6. int main( int argc, char** argv )
  7. {
  8. const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
  9. #if DEMO_MIXED_API_USE
  10. Ptr<IplImage> iplimg = cvLoadImage(imagename); // Ptr<T> is safe ref-conting pointer class
  11. if(iplimg.empty())
  12. {
  13. fprintf(stderr, "Can not load image %s\n", imagename);
  14. return -1;
  15. }
  16. Mat img(iplimg); // cv::Mat replaces the CvMat and IplImage, but it's easy to convert
  17. // between the old and the new data structures (by default, only the header
  18. // is converted, while the data is shared)
  19. #else
  20. Mat img = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function
  21. if(img.empty())
  22. {
  23. fprintf(stderr, "Can not load image %s\n", imagename);
  24. return -1;
  25. }
  26. #endif
  27. if( !img.data ) // check if the image has been loaded properly
  28. return -1;
  29. Mat img_yuv;
  30. cvtColor(img, img_yuv, CV_BGR2YCrCb); // convert image to YUV color space. The output image will be created automatically
  31. vector<Mat> planes; // Vector is template vector class, similar to STL's vector. It can store matrices too.
  32. split(img_yuv, planes); // split the image into separate color planes
  33. #if 1
  34. // method 1. process Y plane using an iterator
  35. MatIterator_<uchar> it = planes[0].begin<uchar>(), it_end = planes[0].end<uchar>();
  36. for(; it != it_end; ++it)
  37. {
  38. double v = *it*1.7 + rand()%21-10;
  39. *it = saturate_cast<uchar>(v*v/255.);
  40. }
  41. // method 2. process the first chroma plane using pre-stored row pointer.
  42. // method 3. process the second chroma plane using individual element access
  43. for( int y = 0; y < img_yuv.rows; y++ )
  44. {
  45. uchar* Uptr = planes[1].ptr<uchar>(y);
  46. for( int x = 0; x < img_yuv.cols; x++ )
  47. {
  48. Uptr[x] = saturate_cast<uchar>((Uptr[x]-128)/2 + 128);
  49. uchar& Vxy = planes[2].at<uchar>(y, x);
  50. Vxy = saturate_cast<uchar>((Vxy-128)/2 + 128);
  51. }
  52. }
  53. #else
  54. Mat noise(img.size(), CV_8U); // another Mat constructor; allocates a matrix of the specified size and type
  55. randn(noise, Scalar::all(128), Scalar::all(20)); // fills the matrix with normally distributed random values;
  56. // there is also randu() for uniformly distributed random number generation
  57. 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
  58. const double brightness_gain = 0;
  59. const double contrast_gain = 1.7;
  60. #if DEMO_MIXED_API_USE
  61. // it's easy to pass the new matrices to the functions that only work with IplImage or CvMat:
  62. // step 1) - convert the headers, data will not be copied
  63. IplImage cv_planes_0 = planes[0], cv_noise = noise;
  64. // step 2) call the function; do not forget unary "&" to form pointers
  65. cvAddWeighted(&cv_planes_0, contrast_gain, &cv_noise, 1, -128 + brightness_gain, &cv_planes_0);
  66. #else
  67. addWeighted(planes[0], constrast_gain, noise, 1, -128 + brightness_gain, planes[0]);
  68. #endif
  69. const double color_scale = 0.5;
  70. // Mat::convertTo() replaces cvConvertScale. One must explicitly specify the output matrix type (we keep it intact - planes[1].type())
  71. planes[1].convertTo(planes[1], planes[1].type(), color_scale, 128*(1-color_scale));
  72. // alternative form of cv::convertScale if we know the datatype at compile time ("uchar" here).
  73. // This expression will not create any temporary arrays and should be almost as fast as the above variant
  74. planes[2] = Mat_<uchar>(planes[2]*color_scale + 128*(1-color_scale));
  75. // Mat::mul replaces cvMul(). Again, no temporary arrays are created in case of simple expressions.
  76. planes[0] = planes[0].mul(planes[0], 1./255);
  77. #endif
  78. // now merge the results back
  79. merge(planes, img_yuv);
  80. // and produce the output RGB image
  81. cvtColor(img_yuv, img, CV_YCrCb2BGR);
  82. // this is counterpart for cvNamedWindow
  83. namedWindow("image with grain", CV_WINDOW_AUTOSIZE);
  84. #if DEMO_MIXED_API_USE
  85. // this is to demonstrate that img and iplimg really share the data - the result of the above
  86. // processing is stored in img and thus in iplimg too.
  87. cvShowImage("image with grain", iplimg);
  88. #else
  89. imshow("image with grain", img);
  90. #endif
  91. waitKey();
  92. return 0;
  93. // all the memory will automatically be released by Vector<>, Mat and Ptr<> destructors.
  94. }