opencv调整亮度和对比度
时间: 2023-09-13 16:08:26 浏览: 78
OpenCV提供了很多函数来调整图像的亮度和对比度,以下是一个简单的例子:
```python
import cv2
import numpy as np
# 读取图片
img = cv2.imread('test.jpg')
# 调整亮度和对比度
alpha = 1.5 # 调整对比度的参数
beta = 50 # 调整亮度的参数
new_img = np.clip(alpha * img + beta, 0, 255).astype(np.uint8)
# 显示原图和调整后的图
cv2.imshow('Original Image', img)
cv2.imshow('New Image', new_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个例子中,我们使用了以下公式来调整亮度和对比度:
$$
g(x) = \alpha f(x) + \beta
$$
其中,$f(x)$ 表示原始像素值,$g(x)$ 表示调整后的像素值,$\alpha$ 是对比度调整参数,$\beta$ 是亮度调整参数。我们使用 `np.clip` 函数将调整后的像素值限制在 0 到 255 之间,然后使用 `astype` 函数将数据类型转换为 `np.uint8`,最后显示原始图像和调整后的图像。
你可以根据自己的需要调整参数 $\alpha$ 和 $\beta$ 来得到不同的效果。
相关问题
c++ opencv实现图像亮度对比度伽马值调整
在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`替换为你自己的图像路径。
用c++结合OpenCV库实现调整图片亮度和对比度的算法
可以使用以下代码来调整图片的亮度和对比度:
```
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// 读入图像
Mat image = imread("image.jpg");
// 创建窗口
namedWindow("Original Image");
namedWindow("Adjusted Image");
// 显示原始图像
imshow("Original Image", image);
// 调整亮度和对比度
Mat adjusted;
image.convertTo(adjusted, -1, 1.2, 50); // 参数分别为:输出图像、输出图像的深度、亮度系数、对比度系数
// 显示调整后的图像
imshow("Adjusted Image", adjusted);
waitKey(0);
return 0;
}
```
其中,`image.convertTo` 函数用于调整图像的亮度和对比度。该函数的第一个参数是输出图像,第二个参数是输出图像的深度,第三个参数是亮度系数,第四个参数是对比度系数。
注意:在使用 OpenCV 库之前,需要先在程序中包含头文件 `#include <opencv2/opencv.hpp>`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)