opencv 彩色图像增强对比度 Python
时间: 2023-11-05 13:21:57 浏览: 127
图像增强与合成(python+opencv)
5星 · 资源好评率100%
要在Python中使用OpenCV对彩色图像进行对比度增强,可以使用以下代码:
import cv2 as cv
import numpy as np
# 读取图像
img = cv.imread("./image/fengjing.jpg")
# 将图像转换为灰度图像
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 创建新图像,以便进行后续操作
img_enhanced = np.zeros(img_gray.shape, np.uint8)
# 对比度增强
for i in range(img_gray.shape):
for j in range(img_gray.shape):
result = int(img_gray[i, j] * 1.6)
if result > 255:
result = 255
img_enhanced[i, j] = np.uint8(result)
# 显示原始图像和增强后的图像
cv.imshow("Original Image", img)
cv.imshow("Enhanced Image", img_enhanced)
cv.waitKey(0)
cv.destroyAllWindows()
这段代码使用了OpenCV库来读取彩色图像,并将其转换为灰度图像。然后,通过遍历每个像素,并将其乘以一个系数来增强对比度。最后,将增强后的图像显示出来。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Python 图像处理OpenCV:灰度图对比度操作(笔记)](https://blog.csdn.net/m0_46820710/article/details/127654760)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [【图像处理】彩色图像自适应对比度增强(OpenCV实现)](https://blog.csdn.net/u013921430/article/details/83865427)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文