Ask Your Question
1

How to change contrast of image?

asked 2014-02-09 12:17:01 -0600

CrazySiberianScientist gravatar image

updated 2014-02-11 13:26:29 -0600

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 flag offensive close merge delete

Comments

2
Bartis Áron gravatar imageBartis Áron ( 2014-02-09 12:54:15 -0600 )edit

3 answers

Sort by » oldest newest most voted
1

answered 2014-02-10 11:42:01 -0600

SR gravatar image

updated 2014-02-10 11:48:01 -0600

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

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

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

edit flag offensive delete link more

Comments

This takes 40-43ms.

CrazySiberianScientist gravatar imageCrazySiberianScientist ( 2014-02-13 00:58:21 -0600 )edit

see cv::Mat::convertTo

image.convertTo( new_image , -1, alpha, beta );
sturkmen gravatar imagesturkmen ( 2016-01-07 16:03:53 -0600 )edit
1

answered 2014-02-11 13:25:55 -0600

CrazySiberianScientist gravatar image

updated 2014-02-13 00:55:37 -0600

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[256];
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;
}
edit flag offensive delete link more

Comments

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);

SR gravatar imageSR ( 2014-02-25 06:10:54 -0600 )edit
0

answered 2014-02-09 15:18:27 -0600

updated 2014-02-10 06:03:28 -0600

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`

Usage: ./contrast IMAGE.EXTENSION CONTRAST_ADDING_VALUE BRIGHTNESS_ADDING_VALUE
(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[1] );
 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 :)

edit flag offensive delete link more

Comments

1

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

SR gravatar imageSR ( 2014-02-10 03:12:19 -0600 )edit

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

Abhishek Kumar Annamraju gravatar imageAbhishek Kumar Annamraju ( 2014-02-10 06:04:58 -0600 )edit

This is non-effective code from opencv documentation http://docs.opencv.org/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html , that I gave above.

CrazySiberianScientist gravatar imageCrazySiberianScientist ( 2014-02-10 11:35:35 -0600 )edit

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".

CrazySiberianScientist gravatar imageCrazySiberianScientist ( 2014-02-10 11:39:46 -0600 )edit

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

JohannesZ gravatar imageJohannesZ ( 2014-02-11 02:03:58 -0600 )edit

Question Tools

Stats

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

Seen: 3,268 times

Last updated: Feb 13 '14