opencv调整图像对比度的函数
时间: 2023-09-24 16:14:00 浏览: 135
OpenCV中可以使用cv::Mat::convertTo()函数来调整图像的对比度。具体的实现方式是将图像中的每个像素值乘以一个比例因子并加上一个偏移值。
以下是一个示例代码:
```
cv::Mat img = cv::imread("example.jpg");
double alpha = 1.5; // 对比度比例因子
int beta = 50; // 亮度偏移值
cv::Mat new_img;
img.convertTo(new_img, -1, alpha, beta);
cv::imshow("Original Image", img);
cv::imshow("New Image", new_img);
```
在上述代码中,我们首先读取了一张名为 "example.jpg" 的图像。然后,我们指定了一个对比度比例因子 alpha 和一个亮度偏移值 beta。接着,我们使用convertTo()函数将原始图像转换为新的图像,并将其存储在new_img中。最后,我们使用imshow()函数显示原始图像和新的图像。
希望这可以帮助到你。
相关问题
opencv对比度函数
OpenCV中可以使用convertTo函数来改变图像的对比度,其函数原型如下:
```c++
void cv::Mat::convertTo(
OutputArray dst, // 输出图像
int rtype, // 输出图像的数据类型,一般与原始图像相同
double alpha = 1, // 线性变换的乘数
double beta = 0 // 线性变换的偏移量
) const;
```
其中,alpha表示线性变换的乘数(默认为1),beta表示线性变换的偏移量(默认为0)。可以通过调整alpha和beta来改变图像的对比度。当alpha大于1时,图像的对比度增强;当alpha小于1时,图像的对比度减弱。
示例代码:
```c++
cv::Mat src = cv::imread("test.jpg");
cv::Mat dst;
src.convertTo(dst, -1, 1.5, 0); // 增强对比度
cv::imshow("src", src);
cv::imshow("dst", dst);
cv::waitKey();
```
注意:convertTo函数只是对图像进行线性变换,因此如果图像的像素值已经达到了最大值或最小值,再进行线性变换也无法改变其对比度。
opencv调整对比度
OpenCV是一个开源计算机视觉库,它提供了一系列强大的图像处理函数,包括调整图像的对比度。调整对比度主要是为了增强图片中细节的可见度或者改变其整体亮度。在OpenCV中,你可以使用`cv2.convertScaleAbs()`或`cv2.addWeighted()`函数来调整图像的对比度。
1. 使用`convertScaleAbs()`:
```python
import cv2
import numpy as np
# 加载图像
img = cv2.imread('image.jpg')
# 创建转换矩阵,其中alpha参数控制对比度变化(0-255范围),beta参数用于亮度校正
alpha = 1.5 # 调整对比度的程度,1为原图,大于1提高对比度,小于1降低对比度
beta = 50 # 亮度校正值
# 应用转换
new_img = cv2.convertScaleAbs(img, alpha=alpha, beta=beta)
# 显示原图和调整后的图像
cv2.imshow('Original', img)
cv2.imshow('Contrast Adjusted', new_img)
cv2.waitKey(0) # 等待按键,按任意键关闭窗口
```
2. 使用`addWeighted()`:
```python
# 这里alpha相当于对比度因子,beta相当于亮度调整
new_img = cv2.addWeighted(img, alpha, img, 1 - alpha, beta)
# 其他步骤同上
cv2.imshow(...)
cv2.waitKey(...)
```
阅读全文
相关推荐













