OpenCV study notes to change the contrast and brightness of the image

1. Image contrast and brightness

Image contrast refers to the measurement of the different brightness levels between the brightest white and the darkest black in the bright and dark areas of an image, that is, the size of the grayscale contrast of an image. The larger the difference range, the greater the contrast, and the smaller the difference range, the smaller the contrast. A good contrast ratio of 120:1 can easily display vivid and rich colors. When the contrast ratio is as high as 300:1, all levels can be supported. s color.

Weber's law (the law of sensory threshold ): [2]   Under the same stimulus, the dynamic range of the stimulus that a person can feel is proportional to the intensity of the standard stimulus, K=ΔI/I  K is a constant under a given stimulus, I is Stimulus , ΔI is the dynamic range of the stimulus that can be felt.

Applied to human visual stimuli, define Weber contrast as:

I is the brightness of the object, and I b is the overall brightness of the background.

Michelson contrast

Michelson [3]   Contrast is also called "visibility" and is defined as:

Where the  sum

Represents the brightest brightness and the darkest brightness respectively. The Michelson contrast is theoretically consistent with the perception of the cone cells to the spatial frequency of the field of light flux in the human visual experience .

Rms contrast

Peli proposed in 1990 that the contrast of an image that has nothing to do with the spatial frequency and spatial distribution of the content is defined as the [4] root mean square (that is, standard deviation) of the pixel value in a raster image :  

among them

Describes a raster image with width and height respectively w and h,

Is the value of a pixel at a specified position in the image,

 Is the brightness of the image (average pixel),

 Is the root mean square contrast (pixel standard deviation).

For digital image transformation, suppose the original pixel grayscale is  f(i,j) and the transformed pixel grayscale is  g(i,j) , then the commonly used linear transformation is  g(i,j)= af(i,j) ) + b , where the coefficient a affects the contrast of the image, and the coefficient b affects the brightness of the image, as follows:
(1) When a=1, it is the original image;
(2) When a>1, the contrast is enhanced and the image looks clearer;
( 3) When a<1, the contrast decreases and the image looks darker;
(4) b affects the brightness of the image. As b (b>0) increases and b (b>0) decreases, the overall grayscale value of the image increases Move or move down, that is, the overall image becomes brighter or darker, and does not change the contrast of the image

2. Practice

#include <QCoreApplication>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <QDebug>
#include <QDir>
#include <QFile>
#include "iostream"

using namespace std;
using namespace cv;

int main()
{
    double alpha = 2.2 , beta = 50;
    Mat oriImg;
    //! 读取图片
    oriImg = imread("C:/1.png",IMREAD_COLOR);
    if( !oriImg.data ) { qDebug("Error loading src1 \n"); return -1; }
    imshow("ori",oriImg);

    //! 图片大小要一致
    Mat resImg = Mat::zeros( oriImg.size(), oriImg.type() ); 
    //! 执行运算 resImg(i,j) = alpha*oriImg(i,j) + beta
    for( int y = 0; y < oriImg.rows; y++ )
    {
        for( int x = 0; x < oriImg.cols; x++ )
        {
            for( int c = 0; c < 3; c++ )
            {
                resImg.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( oriImg.at<Vec3b>(y,x)[c] ) + beta );
            }
        }
    }

    imshow( "adjust", resImg );
    waitKey(0);
    return 0;
}

Reference materials:

Appendix 1: The role of saturate_cast

For safe conversion, the result of the operation may exceed the pixel value range (overflow), or it may be a non-integer (if it is a floating point number). Use saturate_cast to convert the result to ensure that it is a valid value.

Guess you like

Origin blog.csdn.net/a8039974/article/details/104862821