OpenCV Development Notes (13): OpenCV adjust image contrast, brightness

If the text is the original article, shall not be reproduced without permission
of the original bloggers blog address: https://blog.csdn.net/qq21497936
original bloggers blog Navigation: https://blog.csdn.net/qq21497936/article/details / 102 478 062
This article blog address: https://blog.csdn.net/qq21497936/article/details/104427405

table of Contents

Foreword

Related blog

Demo demonstration effect

Contrast

brightness

Operator calculates the contrast and brightness of formula

The concept of child count

Contrast and brightness formula

Source Demo

Project templates: corresponds to the version number v1.8.0

Into the pit

Into a pit: drag the scroll bar does not move care


OpenCV development Box

" OpenCV Development Notes (square): Use mingw530_32 compiled openCV3.4.1 source code, build Qt5.9.3 of openCV development environment ."

" OpenCV development notes (a): OpenCV presentation, compiled "

" OpenCV Development Notes (b): cvui interface "

" OpenCV Development Notes (c): OpenCV image concept and basic operation ."

" OpenCV Development Notes (d): OpenCV fetch and store pictures and video data ."

" The OpenCV Development Notes (V): OpenCV reading operation Camera "

" The OpenCV Development Notes (VI): OpenCV underlying data structure, color, and color space conversion function "

" OpenCV development notes (seven): OpenCV basic graphics drawing "

" OpenCV Development Notes (VIII): OpenCV timing of the common operations, scaling, rotation, mirroring "

" The OpenCV Development Notes (IX) : the OpenCV area image (the ROI ) and the whole, partial images mixed "

" The OpenCV Development Notes ( ten): OpenCV image color channel separation image color and multi-channel mixing "

" OpenCV Development Notes (XI): OpenCV compile support Gpu (CUDA) win-qt-mingw32 accelerate the development of the compiler ."

" OpenCV Development Notes (xii): OpenCV compile support Gpu (cuda) to accelerate the development of win-qt-msvc2015 compiler (opencv3.4.0, cuda9.0, VS2015) "

" The OpenCV Development Notes (XIII): the OpenCV image contrast, brightness adjustment "

Continued to supplement ...

 

    OpenCV Development Notes (13): OpenCV adjust image contrast, brightness

 

Foreword

      Pixels of the image itself to adjust contrast and brightness.

 

Related blog

      " The OpenCV Development Notes (V): OpenCV reading operation Camera " may be an image captured by the camera contrast, brightness, adjust the exposure or the like, but require camera hardware itself supports, and Benpian directly to image processing.

 

Demo demonstration effect

 

Contrast

Contrast refers to the measurement of different brightness levels of light and dark areas in an image between the brightest and the darkest black and white, the greater the difference in range represents a higher contrast, the smaller the difference the smaller the range representative of comparison, a good contrast ratio 120 : 1 can be easily displayed vivid, rich color, when the ratio up to 300: 1, can support the color of each floor. But the plight of suffering and the ratio of the same brightness, today there is no effective and fair set of criteria to measure the ratio, so the best way to identify or rely on the user's eyes.

 

brightness

      Brightness refers to the brightness of the screen, the unit is candela per square meter (cd / m2) also known as nits, that is, per square meter per candlelight. Current methods to improve display brightness, there are two, one is to improve the rate of light through the LCD panel; the other is to increase the brightness of the background light.

      Converted to RGB luminance operation value is a fixed value for each added.

 

Operator calculates the contrast and brightness of formula

The concept of child count

      General image processing operator (we understand a calculation formula) is a function that takes one or more input images, and to generate an output image.

      The following is the general form of the operator:

      g (x) = h (f (x)) or g (x) = h (f0 (x) ...... fn (x))

Contrast and brightness formula

      g (x, y) = f (x, y) * alpha + beta;

      Wherein: f (x, y) the representative color value of the original pixel image at a certain point; Alpha denotes the contrast value; Beta representative luminance value;

      Here as in the actual development process, according to the calculation method requires the actual contrast and brightness adjustments:

      g (x, y) = f (x, y) * (alpha / 100.0f) + beta

      The contrast range of 0 to 400 (I selected), the denominator of 100.0f;

      Luminance range -255 to 255 (I selected), no denominator (the denominator can be considered as 1.0f);

 

Source Demo

void OpenCVManager::testContrastAndBrightness()
{
    cv::Mat mat1;
    QString fileName1 = "E:/qtProject/openCVDemo/openCVDemo/modules/openCVManager/images/1.jpg";
    mat1 = cv::imread(fileName1.toStdString());

    cv::String windowName = "openCVDemo v1.8.0";
    cvui::init(windowName);

    if(!mat1.data)
    {
        qDebug() << __FILE__ << __LINE__
                 << "Failed to load image:" << fileName1;
        return;
    }
    // 增强对比度
    float r;
    float g;
    float b;
    cv::Mat dstMat;
    dstMat = cv::Mat::zeros(mat1.size(), mat1.type());
    cv::Mat windowMat = cv::Mat(cv::Size(dstMat.cols * 2, dstMat.rows), CV_8UC3);
    int alpha = 100;    // 小于1,则降低对比度
    int beta = 0;     // 负数,则降低亮度
    cvui::window(windowMat, dstMat.cols, 0, dstMat.cols, dstMat.rows, "settings");
    while(true)
    {
        windowMat = cv::Scalar(0, 0, 0);
        cvui::printf(windowMat, 375, 40, "contrast");
        cvui::trackbar(windowMat, 375, 50, 165, &alpha, 0, 400);
        cvui::printf(windowMat, 375, 100, "brightness");
        cvui::trackbar(windowMat, 375, 110, 165, &beta, -255, 255);
#if 1
        for(int row = 0; row < mat1.rows; row++)
        {
            for(int col = 0; col < mat1.cols; col++)
            {
                b = mat1.at<cv::Vec3b>(row, col)[0];
                g = mat1.at<cv::Vec3b>(row, col)[1];
                r = mat1.at<cv::Vec3b>(row, col)[2];
#if 0
                // 改变背景色
                if(r > 200 && g > 200 && b > 200)
                {
                    r = 25/4.0;
                    g = 246.0f;
                    b = 197.0f;
                    dstMat.at<cv::Vec3b>(row, col)[0] = b;
                    dstMat.at<cv::Vec3b>(row, col)[1] = g;
                    dstMat.at<cv::Vec3b>(row, col)[2] = r;
                }
#endif
#if 1
                // 对比度、亮度计算公式 cv::saturate_cast<uchar>(value):防止溢出
                dstMat.at<cv::Vec3b>(row, col)[0] = cv::saturate_cast<uchar>(b * alpha / 100.0f + beta);
                dstMat.at<cv::Vec3b>(row, col)[1] = cv::saturate_cast<uchar>(g * alpha / 100.0f + beta);
                dstMat.at<cv::Vec3b>(row, col)[2] = cv::saturate_cast<uchar>(r * alpha / 100.0f + beta);
#else
                // 对比度、亮度计算公式 cv::saturate_cast<uchar>(value):防止溢出
                dstMat.at<cv::Vec3b>(row, col)[0] = cv::saturate_cast<uchar>(b * alpha / 100.0f + beta);
                dstMat.at<cv::Vec3b>(row, col)[1] = cv::saturate_cast<uchar>(g * alpha / 100.0f + beta);
                dstMat.at<cv::Vec3b>(row, col)[2] = cv::saturate_cast<uchar>(r * alpha / 100.0f + beta);
#endif
            }
        }
//        cvui::trackbar(windowMat, dstMat.cols, dstMat.rows / 3, dstMat.cols, &?alpha, 0, 100, );
        cv::Mat imageMat = windowMat(cv::Range(0, dstMat.rows), cv::Range(0, dstMat.cols));
        cv::addWeighted(imageMat, 0.0, dstMat, 1.0, 0.0f, imageMat);
#endif
        cvui::update();
        cv::imshow(windowName, windowMat);
        if(cv::waitKey(25) == 27)
        {
            break;
        }
    }
}

 

Project templates: corresponds to the version number v1.8.0

      The corresponding version number v1.8.0

 

Into the pit

Into a pit: do not move the slider pull prop

the reason:

      Do not leave the interval cycle to obtain.

Solution:

 

The original bloggers blog address: https://blog.csdn.net/qq21497936
original bloggers blog Navigation: https://blog.csdn.net/qq21497936/article/details/102478062
This article blog address: HTTPS: // Blog .csdn.net / qq21497936 / article / details / 104427405

Published 239 original articles · won praise 259 · views 430 000 +

Guess you like

Origin blog.csdn.net/qq21497936/article/details/104427405