三 (3.1 core 模块) 图像对比度提高

2018-10-25  本文已影响0人  交大小丑

改变图像的对比度和亮度 — OpenCV 2.3.2 documentation http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html#basic-linear-transform

图像处理

一般来说,图像处理算子是带有一幅或多幅输入图像、产生一幅输出图像的函数。 图像变换可分为以下两种:

像素变换

邻域算子

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main( int argc, char** argv ){
    double alpha; /**< 控制对比度 */
    int beta;  /**< 控制亮度 */
    /// 读入用户提供的图像
    Mat image = imread("test.jpg");
    resize(image, image, Size(375, 500));//resize为500*375的图像
    Mat new_image = Mat::zeros(image.size(), image.type());

    /// 初始化
    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;

    int height = image.rows;
    int width = image.cols;


    /// 执行运算 new_image(i,j) = alpha*image(i,j) + beta
    for (int rows = 0; rows < height; rows++)
    {
        for (int cols = 0; cols < width; cols++)
        {
            if (image.channels() == 3) {
                for (int c = 0; c < 3; c++)
                {
                    new_image.atows, cols)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(rows, cols)[c]) + beta          }
            else if(image.channels() == 1){
                new_image.at<uchar>(rows, cols) = saturate_cast<uchar>(alpha*(image.at<uchar>(rows, cols)) + beta);
            }
            
        }
    }

    /// 创建窗口
    namedWindow("Original Image", 1);
    namedWindow("New Image", 1);

    /// 显示图像
    imshow("Original Image", image);
    imshow("New Image", new_image);

    /// 等待用户按键
    waitKey(0);
    return 0;
}

对比度指的是一幅图像中明暗区域最亮的白和最暗的黑之间不同亮度层级的测量,差异范围越大代表对比越大,差异范围越小代表对比越小,好的对比率120:1就可容易地显示生动、丰富的色彩,当对比率高达300:1时,便可支持各阶的颜色。但对比率遭受和亮度相同的困境,现今尚无一套有效又公正的标准来衡量对比率,所以最好的辨识方式还是依靠使用者眼睛。

像素邻域操作

Mat kern = (Mat_<char>(3,3) <<  0, -1,  0,
                               -1,  5, -1,
                                0, -1,  0);
#include <iostream>
#include <opencv2/opencv.hpp>
#include <math.h>

using namespace cv;

int main(int argc, const char * argv[]) {
    Mat src, dst;
    //加载图像
    src = imread("/Users/Longxia/Downloads/552566-XXL.jpg");
    
    if (!src.data) {
        printf("could not load image\n");
        return -1;
    }
    //显示
    namedWindow("input Image", CV_WINDOW_AUTOSIZE);
    imshow("input Image", src);
    
    /*
     //掩膜操作
     int cols = (src.cols-1) * src.channels();
     int offsetx = src.channels();
     int rows = src.rows;
     dst = Mat::zeros(src.size(), src.type());

     for (int row = 1; row < rows-1; row++) {
       const uchar *previous = src.ptr<uchar>(row-1);
       const uchar *current = src.ptr<uchar>(row);
       const uchar *next = src.ptr<uchar>(row+1);
       uchar *output = dst.ptr<uchar>(row);
       for (int col = offsetx; col < cols; col++) {
         output[col] = saturate_cast<uchar>(5*current[col] - (current[col-offsetx] + current[col+offsetx] + previous[col] + next[col]));
       }
     }
     */

    // openCV API 掩膜操作
    //定义一个掩膜
    double t = getTickCount();  //获得当前时间
    Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1 ,0);
    //src.depth() 表示与原图深度一样,-1也表示一样
    filter2D(src, dst, src.depth(), kernel);
    double time = (getTickCount() - t) / getTickFrequency();
    printf("time consume %.5f", time);
    //显示
    namedWindow("contrast Image", CV_WINDOW_AUTOSIZE);
    imshow("contrast Image", dst);
    
    waitKey(0);
    return 0;
}

OpenCV学习之路(四)——矩阵的掩膜操作 - 简书 https://www.jianshu.com/p/c6d1c01c900b

数字图像处理,经典对比度增强算法 - EbowTang的练习场 - CSDN博客 https://blog.csdn.net/ebowtang/article/details/38236441

图像对比度增强算法 - Full_Speed_Turbo - CSDN博客 https://blog.csdn.net/full_speed_turbo/article/details/54581055

自适应图像对比度增强算法 - 羽凌寒 - CSDN博客 https://blog.csdn.net/u011630458/article/details/53523316?utm_source=blogxgwz1

上一篇下一篇

猜你喜欢

热点阅读