brightness.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <iostream>
  2. #include <string>
  3. #include <opencv2/highgui.hpp>
  4. #include <opencv2/imgproc.hpp>
  5. using namespace std;
  6. using namespace cv;
  7. void nothing(int x, void* data) {}
  8. void brightness(Mat img) {
  9. namedWindow("image");
  10. int slider = 100;
  11. createTrackbar("val","image",&slider,150,nothing);
  12. Mat hsv;
  13. while (true) {
  14. cvtColor(img, hsv, COLOR_BGR2HSV);
  15. float val = getTrackbarPos("val","image");
  16. val=val/100.0;
  17. Mat channels[3];
  18. split(hsv,channels);
  19. Mat H = channels[0];
  20. H.convertTo(H,CV_32F);
  21. Mat S = channels[1];
  22. S.convertTo(S,CV_32F);
  23. Mat V = channels[2];
  24. V.convertTo(V,CV_32F);
  25. for (int i=0; i < H.size().height; i++){
  26. for (int j=0; j < H.size().width; j++){
  27. // scale pixel values up or down for channel 1(Saturation)
  28. S.at<float>(i,j) *= val;
  29. if (S.at<float>(i,j) > 255)
  30. S.at<float>(i,j) = 255;
  31. // scale pixel values up or down for channel 2(Value)
  32. V.at<float>(i,j) *= val;
  33. if (V.at<float>(i,j) > 255)
  34. V.at<float>(i,j) = 255;
  35. }
  36. }
  37. H.convertTo(H,CV_8U);
  38. S.convertTo(S,CV_8U);
  39. V.convertTo(V,CV_8U);
  40. vector<Mat> hsvChannels{H,S,V};
  41. Mat hsvNew;
  42. merge(hsvChannels,hsvNew);
  43. Mat res;
  44. cvtColor(hsvNew,res,COLOR_HSV2BGR);
  45. imshow("original",img);
  46. imshow("image",res);
  47. if (waitKey(1) == 'q')
  48. break;
  49. }
  50. destroyAllWindows();
  51. }
  52. int main(){
  53. Mat img = imread("image.jpg");
  54. brightness(img);
  55. return 0;
  56. }