How to change contrast of image?

Hello! I have a problem . I decided to write a function to change the contrast of the image. Original image : RGB 8-bit 1920x1080. Processor of computer , which I use for processing is AMD Phenom II 810 2.6 GHz. To compile in Visual Studio (c + +) I use configuration Release x64. If the algorithm is used as here : http://docs.opencv.org/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html, then the image processing takes 340ms , that's a lot . Next, I decided to write the code in the likeness http://habrahabr.ru/post/139428/ ( taking remarks in the comments ) , the processing time became 240ms , it is lot. Then I tried to use to calculate the "buf ( see article from http://habrahabr.ru/post/139428/) using a thumbnail image of the original ( somewhere in the 10 times ) , the processing time was reduced to 100 ms , but still a lot.

Google doesn't give results about image contrast changing fast algorithm.

edit retag close merge delete

2

Sort by » oldest newest most voted

A quick and dirty contrast enhancement via matrix expressions and a linear transformation:

float alpha, beta = ...
Mat image = imread( argv );
image += beta;
image *= alpha;

For instance, set beta to -(minimum value) and alpha to (maximum value - minimum value).

more

I found next solution, this takes 30-33ms in my conditions(see above):

Mat ConBright(Mat img, int contrast, int bright, int dStep=200){
CV_Assert(img.depth() != sizeof(uchar));

unsigned int nSize = img.cols*img.rows*img.channels();
unsigned int nStep = img.cols*img.rows / (dStep*dStep)+1;
unsigned char buf;
unsigned int midBright = 0;
uchar* p=img.data;
int is = 0;
for (int i; i < nSize; i+=nStep,is++)
midBright+=*(p++);
midBright /= (256 * is / 3);

for (size_t i = 0; i < 256; i++)
{
int a = (((i - midBright) * contrast) / 256) + midBright + bright;
if (a < 0) buf[i] = 0;
else if (a > 255) buf[i] = 255;
else buf[i] = a;
}
Mat oimg(img.rows, img.cols, CV_MAKETYPE(img.depth(), img.channels()));
uchar* po=oimg.data;
p = img.data;
for (int i = 0; i < nSize; ++i)
*(po++) = buf[*(p++)];
return oimg;
}
more

1

You may simplify the inner part of the first loop to buf[i] = cv::saturate_cast<unsigned char>((((i - midBright) * contrast) / 256) + midBright + bright);

Code for changing contrast of image

******code starts 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

(eg: - ./contrast cat.jpg 2 100)
*/

#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 )
{
/// Read image given by user
Mat image = imread( argv );
Mat new_image = Mat::zeros( image.size(), image.type() );

/// Initialize values
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;

/// Do the operation new_image(i,j) = alpha*image(i,j) + 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 );
}
}
}

/// Create Windows
namedWindow("Original Image", 1);
namedWindow("New Image", 1);

/// Show stuff
imshow("Original Image", image);
imshow("New Image", new_image);

/// Wait until user press some key
waitKey();
return 0;
}

Hope this helps you :)

more

1

This does not change the contrast. It just makes the image brighter by a certain amount.

sorry,by mistake i pasted the wrong code,i have changed it now there itself.

I found solution,but I will paste code tommorow, because this opencv forum show me message:"New users must wait 2 days before answering their own question. You can post an answer tomorrow".

Why don't you use image processing book?? Gonzalez & Woods for example...

Stats

Asked: 2014-02-09 12:17:01 -0500

Seen: 2,390 times

Last updated: Feb 13 '14