OpenCV (5) Contrast and brightness

Source: Internet
Author: User

?

Formula:

Two parameters \alpha > 0 and \beta are generally referred to as gain and offset parameters. We often use these two parameters to control the contrast and brightness separately.

?

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <thread>
  4. #include <vector>
  5. #include <opencv2/core/core.hpp>
  6. #include <opencv2/contrib/contrib.hpp>
  7. #include <opencv2/highgui/highgui.hpp>
  8. #include <opencv2/imgproc/imgproc.hpp>
  9. #include <opencv2/objdetect/objdetect.hpp>
  10. ?
  11. using namespace CV;
  12. using namespace std;
  13. ?
  14. int g_slider_position = 0, g_slider_position2 = 0;
  15. Mat image;
  16. Mat New_image;
  17. double alpha, beta;
  18. ?
  19. void ontrackingbarslide (int POS)
  20. {
  21. ??? New_image = Mat::zeros (Image.size (), Image.type ());
  22. ??? beta = pos;
  23. ??? for (int y = 0; y < image.rows; y++)
  24. ??? {
  25. ?????? for (int x = 0; x < image.cols; × x + +)
  26. ?????? {
  27. ????????? for (int c = 0; c < 3; C + +)
  28. ????????? {
  29. ???????????? //saturate_cast prevent data overflow
  30. ???????????? New_image.at<vec3b> (y, x) [c] = saturate_cast<uchar> (alpha* (image.at<vec3b> (y, x) [C]) + beta);
  31. ?????????}
  32. ??????}
  33. ???}
  34. ?
  35. ??? Imshow ("New Image", new_image);
  36. }
  37. ?
  38. void onTrackingbarSlide2 (int POS)
  39. {
  40. ??? New_image = Mat::zeros (Image.size (), Image.type ());
  41. ?
  42. ??? Alpha = (double) pos/10.0;
  43. ?
  44. ??? for (int y = 0; y < image.rows; y++)
  45. ??? {
  46. ?????? for (int x = 0; x < image.cols; × x + +)
  47. ?????? {
  48. ????????? for (int c = 0; c < 3; C + +)
  49. ????????? {
  50. ???????????? //saturate_cast prevent data overflow
  51. ???????????? New_image.at<vec3b> (y, x) [c] = saturate_cast<uchar> (alpha* (image.at<vec3b> (y, x) [C]) + beta);
  52. ?????????}
  53. ??????}
  54. ???}
  55. ?
  56. ??? Imshow ("New Image", new_image);
  57. }
  58. ?
  59. int _tmain (int argc, _tchar* argv[])
  60. {
  61. ??? /// read into user-supplied images
  62. ??? Image = Imread ("e:\\myimage\\sql.png");
  63. ?
  64. ??? //initialized to 0 array
  65. ??? Mat new_image = Mat::zeros (Image.size (), Image.type ());
  66. ?
  67. ??? /// Initialize
  68. ??? cout << "* Enter the alpha value [1.0-3.0]: ";
  69. ??? CIN >> Alpha;
  70. ??? cout << "* Enter the beta value [0-100]: ";
  71. ??? CIN >> Beta;
  72. ?
  73. ??? // Create window
  74. ??? Namedwindow ("Original Image", 1); //1:window_autosize
  75. ??? Namedwindow ("New Image", 1);
  76. ?
  77. ??? Cvcreatetrackbar (" brightness (Gain)", "New Image", &g_slider_position, Ontrackingbarslide);
  78. ??? Cvcreatetrackbar (" contrast (offset)", "New Image", &g_slider_position2, OnTrackingbarSlide2);
  79. ?
  80. ??? ///Perform operation new_image (i,j) = Alpha*image (i,j) + Beta
  81. ??? for (int y = 0; y < image.rows; y++)
  82. ??? {
  83. ?????? for (int x = 0; x < image.cols; × x + +)
  84. ?????? {
  85. ????????? for (int c = 0; c < 3; C + +)
  86. ????????? {
  87. ???????????? //saturate_cast prevent data overflow
  88. ???????????? New_image.at<vec3b> (y, x) [c] = saturate_cast<uchar> (alpha* (image.at<vec3b> (y, x) [C]) + beta);
  89. ?????????}
  90. ??????}
  91. ???}
  92. ?
  93. ??? /// display image
  94. ??? Imshow ("Original image", image);
  95. ??? Imshow ("New Image", new_image);
  96. ?
  97. ??? /// wait for user keys
  98. ??? Waitkey ();
  99. ??? return 0;
  100. }

?

?

Reference:

Http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/basic_linear_transform/basic_linear_ Transform.html#basic-linear-transform

http://blog.csdn.net/mjlsuccess/article/details/12401839

OpenCV (5) Contrast and brightness

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.