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

/samples/cpp/image.cpp

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