Reputation: 23
I wanted to know if there is any function of OpenCV using C++ to adjust the brightness and contrast of a video / frame. You can convert from BGR color space to HSV color space, and discard the latter component V (luminance) to make the algorithm less sensitive to light conditions in the video, but how can I do it?
I was thinking of using something like cvAddS (frame, cvScalar (-50, -50, -50), frame) to Decrease the brightness, cvAddS and cvScalar work's well for C but how can I do that for C++, I use AddS and Scalar in my program, but don't work with C++
int main() {
VideoCapture video(1);
if(!video.isOpened()) {
cerr<<"No video input"<<endl; return -1;
}
namedWindow("Video",CV_WINDOW_AUTOSIZE);
for(;;) {
Mat frame;
video >> frame; if(!frame.data) break;
Mat frame2;
//I USE AddS AND Scalar TO DECREASE THE BRIGHTNESS
AddS(frame,Scalar(-50,-50,-50),frame2);
//BUT DON'T WORK WITH C++
imshow("Video",frame2);
int c=waitKey(20);
if(c >= 0)break;
}
}
Upvotes: 2
Views: 8784
Reputation: 17295
Brightness and contrast are usually corrected using a linear transformation of the pixel values. Brightness corresponds to the additive shift and contrast corresponds to a multiplicative factor.
In general, given a pixel value v
, the new value after to correction would be v'=a*v + b
.
Upvotes: 3
Reputation: 16462
Use matrix expression:
cv::Mat frame2 = frame + cv::Scalar(-50, -50, -50);
You might also want to adjust the contrast with histogram equalization. Convert your RGB image to HSV and apply cv::equalizeHist()
to the V channel.
Upvotes: 4