How does Python measure image brightness?

You can think of \(f(x)\) as the source image pixels and \(g(x)\) as the output image pixels. Then, more conveniently we can write the expression as:

\[g(i,j) = \alpha \cdot f(i,j) + \beta\]

where \(i\) and \(j\) indicates that the pixel is located in the i-th row and j-th column.

Code

  • Downloadable code: Click here
  • The following code performs the operation \(g(i,j) = \alpha \cdot f(i,j) + \beta\) :

    int main( int argc, char** argv )

    cout << "Could not open or find the image!\n" << endl;

    cout << "Usage: " << argv[0] << " " << endl;

    cout << " Basic Linear Transforms " << endl;

    cout << "-------------------------" << endl;

    cout << "* Enter the alpha value [1.0-3.0]: "; cin >> alpha;

    cout << "* Enter the beta value [0-100]: "; cin >> beta;

    for( int y = 0; y < image.; y++ ) {

    for( int x = 0; x < image.; x++ ) {

    for( int c = 0; c < image.(); c++ ) {

    ("Original Image", image);

    ("New Image", new_image);

  • Downloadable code: Click here
  • The following code performs the operation \(g(i,j) = \alpha \cdot f(i,j) + \beta\) :

    import org.opencv.core.Core;

    import org.opencv.core.Mat;

    import org.opencv.highgui.HighGui;

    import org.opencv.imgcodecs.Imgcodecs;

    class BasicLinearTransforms {

    private byte saturate(double val) {

    int iVal = (int) Math.round(val);

    iVal = iVal > 255 ? 255 : (iVal < 0 ? 0 : iVal);

    public void run(String[] args) {

    String imagePath = args.length > 0 ? args[0] : "../data/lena.jpg";

    Mat image = Imgcodecs.imread(imagePath);

    System.out.println("Empty image: " + imagePath);

    Mat newImage = Mat.(image.size(), image.type());

    System.out.println(" Basic Linear Transforms ");

    System.out.println("-------------------------");

    try (Scanner scanner = new Scanner(System.in)) {

    System.out.print("* Enter the alpha value [1.0-3.0]: ");

    alpha = scanner.nextDouble();

    System.out.print("* Enter the beta value [0-100]: ");

    beta = scanner.nextInt();

    byte[] imageData = new byte[(int) (image.total()*image.channels())];

    image.get(0, 0, imageData);

    byte[] newImageData = new byte[(int) (newImage.total()*newImage.channels())];

    for (int y = 0; y < image.rows(); y++) {

    for (int x = 0; x < image.cols(); x++) {

    for (int c = 0; c < image.channels(); c++) {

    double pixelValue = imageData[(y * image.cols() + x) * image.channels() + c];

    pixelValue = pixelValue < 0 ? pixelValue + 256 : pixelValue;

    newImageData[(y * image.cols() + x) * image.channels() + c]

    = saturate(alpha * pixelValue + beta);

    newImage.put(0, 0, newImageData);

    HighGui.imshow("Original Image", image);

    HighGui.imshow("New Image", newImage);

    public class BasicLinearTransformsDemo {

    public static void main(String[] args) {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    new BasicLinearTransforms().run(args);

  • Downloadable code: Click here
  • The following code performs the operation \(g(i,j) = \alpha \cdot f(i,j) + \beta\) :

    from __future__ import print_function

    parser = argparse.ArgumentParser(description='Code for Changing the contrast and brightness of an image! tutorial.')

    parser.add_argument('--input', help='Path to input image.', default='lena.jpg')

    args = parser.parse_args()

    ('Could not open or find the image: ', args.input)

    new_image = np.zeros(image.shape, image.dtype)

    (' Basic Linear Transforms ')

    ('-------------------------')

    alpha = float(input('* Enter the alpha value [1.0-3.0]: '))

    beta = int(input('* Enter the beta value [0-100]: '))

    ('Error, not a number')

    for y in range(image.shape[0]):

    for x in range(image.shape[1]):

    for c in range(image.shape[2]):

    new_image[y,x,c] = np.clip(alpha*image[y,x,c] + beta, 0, 255)

Explanation

  • We load an image using and save it in a Mat object:

CommandLineParser parser( argc, argv, "{@input | lena.jpg | input image}" );

cout << "Could not open or find the image!\n" << endl;

cout << "Usage: " << argv[0] << " " << endl;

String imagePath = args.length > 0 ? args[0] : "../data/lena.jpg";

Mat image = Imgcodecs.imread(imagePath);

System.out.println("Empty image: " + imagePath);

parser = argparse.ArgumentParser(description='Code for Changing the contrast and brightness of an image! tutorial.')

parser.add_argument('--input', help='Path to input image.', default='lena.jpg')

args = parser.parse_args()

('Could not open or find the image: ', args.input)

  • Now, since we will make some transformations to this image, we need a new Mat object to store it. Also, we want this to have the following features:
    • Initial pixel values equal to zero
    • Same size and type as the original image

Mat new_image = Mat::zeros( image.size(), image.type() );

Mat newImage = Mat.zeros(image.size(), image.type());

new_image = np.zeros(image.shape, image.dtype)

We observe that returns a Matlab-style zero initializer based on image.size() and image.type()

  • We ask now the values of \(\alpha\) and \(\beta\) to be entered by the user:

cout << " Basic Linear Transforms " << endl;

cout << "-------------------------" << endl;

cout << "* Enter the alpha value [1.0-3.0]: "; cin >> alpha;

cout << "* Enter the beta value [0-100]: "; cin >> beta;

System.out.println(" Basic Linear Transforms ");

System.out.println("-------------------------");

try (Scanner scanner = new Scanner(System.in)) {

System.out.print("* Enter the alpha value [1.0-3.0]: ");

alpha = scanner.nextDouble();

System.out.print("* Enter the beta value [0-100]: ");

beta = scanner.nextInt();

(' Basic Linear Transforms ')

('-------------------------')

alpha = float(input('* Enter the alpha value [1.0-3.0]: '))

beta = int(input('* Enter the beta value [0-100]: '))

('Error, not a number')

  • Now, to perform the operation \(g(i,j) = \alpha \cdot f(i,j) + \beta\) we will access to each pixel in image. Since we are operating with BGR images, we will have three values per pixel (B, G and R), so we will also access them separately. Here is the piece of code:

for( int y = 0; y < image.rows; y++ ) {

for( int x = 0; x < image.cols; x++ ) {

for( int c = 0; c < image.channels(); c++ ) {

new_image.at<>(y,x)[c] =

byte[] imageData = new byte[(int) (image.total()*image.channels())];

image.get(0, 0, imageData);

byte[] newImageData = new byte[(int) (newImage.total()*newImage.channels())];

for (int y = 0; y < image.rows(); y++) {

for (int x = 0; x < image.cols(); x++) {

for (int c = 0; c < image.channels(); c++) {

double pixelValue = imageData[(y * image.cols() + x) * image.channels() + c];

pixelValue = pixelValue < 0 ? pixelValue + 256 : pixelValue;

newImageData[(y * image.cols() + x) * image.channels() + c]

= saturate(alpha * pixelValue + beta);

newImage.put(0, 0, newImageData);

for y in range(image.shape[0]):

for x in range(image.shape[1]):

for c in range(image.shape[2]):

new_image[y,x,c] = np.clip(alpha*image[y,x,c] + beta, 0, 255)

Notice the following (C++ code only):

  • To access each pixel in the images we are using this syntax: image.at(y,x)[c]where y is the row, x is the column and c is B, G or R (0, 1 or 2).
  • Since the operation \(\alpha \cdot p(i,j) + \beta\) can give values out of range or not integers (if \(\alpha\) is float), we use to make sure the values are valid.
  • Finally, we create windows and show the images, the usual way.

("Original Image", image);

("New Image", new_image);

HighGui.imshow("Original Image", image);

HighGui.imshow("New Image", newImage);

NoteInstead of using the for loops to access each pixel, we could have simply used this command:

image.convertTo(new_image, -1, alpha, beta);

image.convertTo(newImage, -1, alpha, beta);

where would effectively perform *new_image = a*image + beta*. However, we wanted to show you how to access each pixel. In any case, both methods give the same result but convertTo is more optimized and works a lot faster.

Result

  • Running our code and using \(\alpha = 2.2\) and \(\beta = 50\)

    $ ./BasicLinearTransforms lena.jpg

    -------------------------

    * Enter the alpha value [1.0-3.0]: 2.2

    * Enter the beta value [0-100]: 50

  • We get this:

    How does Python measure image brightness?

Practical example

In this paragraph, we will put into practice what we have learned to correct an underexposed image by adjusting the brightness and the contrast of the image. We will also see another technique to correct the brightness of an image called gamma correction.

Brightness and contrast adjustments

Increasing (/ decreasing) the \(\beta\) value will add (/ subtract) a constant value to every pixel. Pixel values outside of the [0 ; 255] range will be saturated (i.e. a pixel value higher (/ lesser) than 255 (/ 0) will be clamped to 255 (/ 0)).

How does Python measure image brightness?

In light gray, histogram of the original image, in dark gray when brightness = 80 in Gimp

The histogram represents for each color level the number of pixels with that color level. A dark image will have many pixels with low color value and thus the histogram will present a peak in its left part. When adding a constant bias, the histogram is shifted to the right as we have added a constant bias to all the pixels.

The \(\alpha\) parameter will modify how the levels spread. If \( \alpha < 1 \), the color levels will be compressed and the result will be an image with less contrast.

How does Python measure image brightness?

In light gray, histogram of the original image, in dark gray when contrast < 0 in Gimp

Note that these histograms have been obtained using the Brightness-Contrast tool in the Gimp software. The brightness tool should be identical to the \(\beta\) bias parameters but the contrast tool seems to differ to the \(\alpha\) gain where the output range seems to be centered with Gimp (as you can notice in the previous histogram).

It can occur that playing with the \(\beta\) bias will improve the brightness but in the same time the image will appear with a slight veil as the contrast will be reduced. The \(\alpha\) gain can be used to diminue this effect but due to the saturation, we will lose some details in the original bright regions.

Gamma correction

Gamma correction can be used to correct the brightness of an image by using a non linear transformation between the input values and the mapped output values:

\[O = \left( \frac{I}{255} \right)^{\gamma} \times 255\]

As this relation is non linear, the effect will not be the same for all the pixels and will depend to their original value.

How does Python measure image brightness?

Plot for different values of gamma

When \( \gamma < 1 \), the original dark regions will be brighter and the histogram will be shifted to the right whereas it will be the opposite with \( \gamma > 1 \).

Correct an underexposed image

The following image has been corrected with: \( \alpha = 1.3 \) and \( \beta = 40 \).

How does Python measure image brightness?

By Visem (Own work) [CC BY-SA 3.0], via Wikimedia Commons

The overall brightness has been improved but you can notice that the clouds are now greatly saturated due to the numerical saturation of the implementation used (highlight clipping in photography).

The following image has been corrected with: \( \gamma = 0.4 \).

How does Python measure image brightness?

By Visem (Own work) [CC BY-SA 3.0], via Wikimedia Commons

The gamma correction should tend to add less saturation effect as the mapping is non linear and there is no numerical saturation possible as in the previous method.

How does Python measure image brightness?

Left: histogram after alpha, beta correction ; Center: histogram of the original image ; Right: histogram after the gamma correction

The previous figure compares the histograms for the three images (the y-ranges are not the same between the three histograms). You can notice that most of the pixel values are in the lower part of the histogram for the original image. After \( \alpha \), \( \beta \) correction, we can observe a big peak at 255 due to the saturation as well as a shift in the right. After gamma correction, the histogram is shifted to the right but the pixels in the dark regions are more shifted (see the gamma curves figure) than those in the bright regions.

In this tutorial, you have seen two simple methods to adjust the contrast and the brightness of an image. They are basic techniques and are not intended to be used as a replacement of a raster graphics editor!

Code

Code for the tutorial is here.

Code for the tutorial is here.

Code for the tutorial is here.

Code for the gamma correction:

Mat lookUpTable(1, 256, );

* p = lookUpTable.ptr();

for( int i = 0; i < 256; ++i)

(img, lookUpTable, res);

Mat lookUpTable = new Mat(1, 256, CvType.CV_8U);

byte[] lookUpTableData = new byte[(int) (lookUpTable.total()*lookUpTable.channels())];

for (int i = 0; i < lookUpTable.cols(); i++) {

lookUpTableData[i] = saturate(Math.pow(i / 255.0, gammaValue) * 255.0);

lookUpTable.put(0, 0, lookUpTableData);

Core.LUT(matImgSrc, lookUpTable, img);

lookUpTable = np.empty((1,256), np.uint8)

lookUpTable[0,i] = np.clip((i / 255.0, gamma) * 255.0, 0, 255)

res = (img_original, lookUpTable)

A look-up table is used to improve the performance of the computation as only 256 values needs to be calculated once.

How is brightness of a photo measured?

The image brightness measure can be calculated as the average of the brightness component after converting it into the HSB color space [14] . HSL stands for hue, saturation, and brightness, and is also often called HSV (V for value)) or HSL (L for lightness). ...

What are some methods to analyze image brightness using Python?

Convert image to greyscale, return RMS pixel brightness. Average pixels, then transform to "perceived brightness". RMS of pixels, then transform to "perceived brightness". Calculate "perceived brightness" of pixels, then return average.

How do you find the brightness of an image in Python OpenCV?

It ranges from 0 to 100%. When the value is '0′ the color space will be totally black. With the increase in the value, the color space brightness up and shows various colors." So use OpenCV method cvCvtColor(const CvArr* src, CvArr* dst, int code), that converts an image from one color space to another.

How do I find the brightest pixel of an image in Python?

The susceptible method to finding the brightest spot in an image is to use the cv2. minMaxLoc function without any pre-processing. This function requires a single argument, which is our grayscale image.