PageRenderTime 180ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/myOpenCV/cpp/image.cpp

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