[原创]opencv图像亮度/对比度调整实验

[原创]opencv图像亮度/对比度调整实验

Author: chad
Mail: linczone@163.com

亮度和对比度调整实验
两种常用的点过程(即点算子),是用常数对点进行 乘法 和 加法 运算:
g(x) = A*f(x) + B

两个参数 A和 B 一般称作 增益 和 偏置 参数。我们往往用这两个参数来分别控制 对比度 和 亮度 。

你可以把 f(x) 看成源图像像素,把 g(x) 看成输出图像像素。这样一来,上面的式子就能写得更清楚些:
g(i,j) = A*f(i,j) + B
其中, i 和 j 表示像素位于 第i行 和 第j列 。
降低亮度时保证g(x) < f(x), 增加亮度时g(x) > f(x) .

源码位置:
opencv-3.0.0-rc1/samples/cpp/tutorial_code/ImgProc/BasicLinearTransforms.cpp
源码如下 :

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
double alpha; /**< 控制对比度 */
int beta;  /**< 控制亮度 */
int main( int argc, char** argv )
{    
    /// 读入用户提供的图像    
    Mat new_image = Mat::zeros( image.size(), image.type() );    
    /// 初始化    
    cout << "-------------------------" << endl;    
    cout << "* Enter the alpha value [1.0-3.0]: ";    
    cin >> alpha;    cout << "* Enter the beta value [0-100]: ";    
    cin >> beta;    
    new_image(i,j) = alpha*image(i,j) + beta    
    for( int y = 0; y < image.rows; y++ )    {        
        for( int x = 0; x < image.cols; x++ )        {            
                for( int c = 0; c < 3; c++ )            {  
                    new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );           
                    }
                }
        }    
        /// 创建窗口    
        namedWindow("Original Image", 1);    
        namedWindow("New Image", 1);    /// 显示图像  
        imshow("Original Image", image);    
        imshow("New Image", new_image);    
        /// 等待用户按键    
        waitKey();    
        return 0;
}

实验结果如下:

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,可以使用OpenCV库来实现图像亮度对比度和伽马值调整。下面是一个简单的示例代码: ```cpp #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; // 亮度对比度调整函数 void adjustBrightnessContrast(Mat& image, double alpha, int beta) { // 遍历图像的每个像素 for (int y = 0; y < image.rows; y++) { for (int x = 0; x < image.cols; x++) { for (int c = 0; c < image.channels(); c++) { // 对每个通道的像素进行亮度对比度调整 image.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha * image.at<Vec3b>(y, x)[c] + beta); } } } } // 伽马值调整函数 void adjustGamma(Mat& image, double gamma) { // 建立查找表 unsigned char lut[256]; for (int i = 0; i < 256; i++) { lut[i] = saturate_cast<uchar>(pow((double)i / 255.0, gamma) * 255.0); } // 应用查找表 image = image.clone(); LUT(image, Mat(1, 256, CV_8UC1, lut), image); } int main() { // 读取图像 Mat image = imread("input.jpg"); if (image.empty()) { std::cerr << "Failed to read image." << std::endl; return -1; } // 调整亮度对比度 double alpha = 1.5; // 亮度调整参数 int beta = 30; // 对比度调整参数 adjustBrightnessContrast(image, alpha, beta); // 调整伽马值 double gamma = 1.5; // 伽马值调整参数 adjustGamma(image, gamma); // 显示结果图像 imshow("Adjusted Image", image); waitKey(0); return 0; } ``` 在这个示例代码中,我们首先定义了两个函数:`adjustBrightnessContrast`和`adjustGamma`,分别用于亮度对比度、伽马值的调整。然后在`main`函数中,我们读取了一张图像,然后分别调用这两个函数进行图像处理。最后,将处理后的图像显示出来。 请确保在编译和运行代码之前,已经安装了OpenCV库,并将示例代码中的`input.jpg`替换为你自己的图像路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值