CONTRAST AND BRIGHTNESS VARIATION OF IMAGES

AUTHORS:ABHISHEK KUMAR ANNAMRAJU,AKASH DEEP SINGH,ADHESH SHRIVASTAVA

Hi Friends,

Lets see how do you change contrast and brightness in opencv

Open a c++ file named contrast.cpp and paste the following code in it

Click once somewhere on the code and press ctrl+A to select whole code.You may not see the whole code so its better to copy the code and paste it in your favourite text editor and then go through it.

**********************CODE STARTS HERE*******************************

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace cv;

double alpha; /**< Simple contrast control */
int beta;  /**< Simple brightness control */

int main( int argc, char** argv )
{

Mat image = imread( argv[1] );
Mat new_image = Mat::zeros( image.size(), image.type() );

std::cout<<" Basic Linear Transforms "<<std::endl;
std::cout<<"-------------------------"<<std::endl;
std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;
std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;

for( int y = 0; y < image.rows; y++ )
{ for( int x = 0; x < image.cols; x++ )
{ for( int c = 0; c < 3; c++ )
{
new_image.at<Vec3b>(y,x)[c] =
saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
}
}
}

namedWindow("Original Image", 1);
namedWindow("New Image", 1);

imshow("Original Image", image);
imshow("New Image", new_image);

return 0;
}

**************************CODE ENDS HERE*******************************

Compilation:

1. chmod +x contrast.cpp
2. g++ -ggdb `pkg-config --cflags opencv` -o `basename contrast.cpp .cpp` contrast.cpp `pkg-config --libs opencv`

Usage:

./contrast IMAGE.EXTENSION CONTRAST_INCREASE_VALUE BRIGHTNESS_ADDING_VALUE

(eg: – ./contrast cat.jpg 2 100)

Advertisements

One thought on “CONTRAST AND BRIGHTNESS VARIATION OF IMAGES”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s