Brightness and contrast modeling

The brightness of an object is the perceived luminance or intensity and depends on the luminance of the environment. Two objects in different environments could have the same luminance but different brightness. The reason is that the human visual perception is sensitive to luminance contrast rather than absolute luminance. Contrast is the difference in luminance and/or color that makes an object distinguishable compared to other objects within the same field of view. The maximum contrast of an image is known as the contrast ratio or dynamic range.

It is possible to modify the brightness and contrast of an image by means of point-wise operations. Point operations map a given gray pixel value into a different gray level according to a transform previously defined. In OpenCV, point operations may be performed with the function void Mat::convertTo(OutputArray m, int rtype, double alpha=1, double beta=0). The convertTo function converts an image array to another data type with optional scaling. The first parameter is the output image and the second parameter is the output matrix type, that is, the depth, since the number of channels is the same as the input image. Thus, the source pixel values I(x,y) are converted to the target data type with the new value (I(x,y) * alpha + beta).

The following BrightnessContrast example shows how to perform an image pixel (point) operation to modify brightness and contrast:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

int init_brightness  = 100;
int init_contrast = 100;

Mat image;

/* brightness and contrast function to highlight the image*/
void updateBrightnessContrast(int, void* )
{
    int histSize = 255;
    int var_brightness = init_brightness  - 100;
    int var_contrast = init_contrast - 100;

    double a, b;
    if( var_contrast > 0 )
    {
        double delta = 127.*var_contrast/100;
        a = 255./(255. - delta*2);
        b = a*(var_brightness - delta);
    }
    else
    {
        double delta = -128.*var_contrast/100;
        a = (256.-delta*2)/255.;
        b = a*var_brightness + delta;
    }

    Mat dst, hist;

    image.convertTo(dst, CV_8U, a, b);

    imshow("image", dst);

    calcHist(&dst, 1, 0, Mat(), hist, 1, &histSize, 0);
    Mat histImage = Mat::ones(200, 320, CV_8U)*255;

    normalize(hist, hist, 0, histImage.rows, CV_MINMAX, CV_32F);

    histImage = Scalar::all(255);
    int binW = cvRound((double)histImage.cols/histSize);

    for( int i = 0; i < histSize; i++ )
        rectangle( histImage, Point(i*binW, histImage.rows), Point((i+1)*binW, histImage.rows – cvRound(hist.at<float>(i))), Scalar::all(0), -1, 8, 0 );
       imshow("histogram", histImage);
   }

const char* keys = {
    "{1| |fruits.jpg|input image file}"
};

int main( int argc, const char** argv )
    {
        CommandLineParser parser(argc, argv, keys);
        string inputImage = parser.get<string>("1");

        //Read the input image.
        image = imread( inputImage, 0 );
        namedWindow("image", 0);
        namedWindow("histogram", 0);

        createTrackbar("brightness", "image", &init_brightness , 200, updateBrightnessContrast);
        createTrackbar("contrast", "image", &init_contrast, 200, updateBrightnessContrast);

        updateBrightnessContrast(0, 0);

    waitKey();
    return 0;
}

The code explanation is given here: the example creates two windows with the grayscale image and its histogram. The new values for the brightness and contrast are selected by the user using the function createTrackbar. This function attaches two sliders or range controls to the image for brightness and contrast. The following screenshot shows the output of the BrightnessContrast example for a value of 148 for brightness and 81 for contrast:

Brightness and contrast modeling

Output of the brightness and contrast image modification

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset