Change Brightness & Contrast of a video (OpenCV C++)






Example code 1: change brightness

--------------------------------------------------------------------------------------------------------------------------

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
VideoCapture video("C:/Users/hashan/Desktop/test.mov");
//VideoCapture video(0);

if (!video.isOpened())
{
cout << "Cannot open the video cam" << endl;
return -1;
}



namedWindow("TestVideo", CV_WINDOW_NORMAL);
namedWindow("B_H_Video", CV_WINDOW_NORMAL);
namedWindow("B_L_Video", CV_WINDOW_NORMAL);


while (1)
{
Mat frame;

bool bSuccess = video.read(frame);

if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat B_H = frame + Scalar(50,50,50);
Mat B_L = frame - Scalar(50, 50, 50);



imshow("TestVideo", frame);
imshow("B_H_Video", B_H);
imshow("B_L_Video", B_L);

if (waitKey(30) == 27)
{
cout << "esc key pressed " << endl;
break;
}
}
return 0;

}


--------------------------------------------------------------------------------------------------------------------------



Example code 2: change contrast

--------------------------------------------------------------------------------------------------------------------------

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
VideoCapture video("C:/Users/hashan/Desktop/test.mov");
//VideoCapture video(0);

if (!video.isOpened())
{
cout << "Cannot open the video cam" << endl;
return -1;
}



namedWindow("TestVideo", CV_WINDOW_NORMAL);
namedWindow("C_H_Video", CV_WINDOW_NORMAL);
namedWindow("C_L_Video", CV_WINDOW_NORMAL);


while (1)
{
Mat frame;

bool bSuccess = video.read(frame);

if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat C_H = frame * 2;
Mat C_L = frame / 2;



imshow("TestVideo", frame);
imshow("C_H_Video", C_H);
imshow("C_L_Video", C_L);

if (waitKey(30) == 27)
{
cout << "esc key pressed " << endl;
break;
}
}
return 0;

}

--------------------------------------------------------------------------------------------------------------------------

See the video demo from below.

  • Thank you very much for reading this tutorial.
  • If you have problems regarding this tutorial feel free to make a comment below or contact us by sending message through our facebook page or by sending email to this address progtpoint@gmail.com 
  • Follow us on facebook and Twitter also subcirbe us youtube

2 comments:

  1. Working as a freelancer exposes you to various work cultures and communication approaches when you deal with customers from various countries. A 9-to-5 remote java developer is cautious about taking on freelancing work because most companies do not allow their employees to do so. Eiliana is a renowned platform where you can find freelance remote java developer jobs, and this platform also assists you in maintaining secrecy.

    ReplyDelete

Social