افزایش کنتراست تصویر
ShahBaz | دوشنبه, ۲۵ مرداد ۱۳۹۵، ۰۴:۳۰ ب.ظ
- void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 )
This OpenCV function converts image into another format with scaling. Scaling is done according to the following formula.
m[i,j] = alfa * img[i,j] + beta
Here is the parameters of this function
- OutputArray m - Store the converted image
- int rtype - Depth of the output image. If the value of rtype is negative, output type is same as the input image. I have used a negative value in the above program because I don't want to change the depth of the original image. Possible inputs to this parameter
- CV_8U
- CV_32S
- CV_64F
- -1
Complete list of depths can be found in Basics of OpenCV API
- double alpha - Multiplication factor; Every pixel will be multiplied by this value
- double beta - This value will be added to very pixels after multiplying with the above value.
Here is the formula again. Here m[i, j] means a pixel at ith row and jth column.
m[i,j] = alfa * img[i,j] + beta
m[i,j] = alfa * img[i,j] + beta
Change the Contrast of a Video
It is similar to the above program except that you have to change the contrast for each and every frame of the video. Here is the example OpenCV program.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap("C:/Users/SHERMAL/Desktop/SampleVideo.wmv"); // open the video file for reading
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
//create windows
namedWindow("Original Video",CV_WINDOW_AUTOSIZE);
namedWindow("Contrast Increased",CV_WINDOW_AUTOSIZE);
namedWindow("Contrast Decreased",CV_WINDOW_AUTOSIZE);
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
Mat imgH;
frame.convertTo(imgH, -1, 2, 0); //increase the contrast (double)
Mat imgL;
frame.convertTo(imgL, -1, 0.5, 0); //decrease the contrast (halve)
//show the image
imshow("Original Video", frame);
imshow("Contrast Increased", imgH);
imshow("Contrast Decreased", imgL);
if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
You can download this OpenCV visual c++ project from here. (The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)
All the OpenCV methods in the above example program have been discussed in previous lessons. So, I am not going to repeat them again.