用python中的OpenCV(cv2)来增加彩色图像对比度的最快方法是什么?

2024-04-16 17:16:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用OpenCV处理一些图像,我需要执行的第一步是增加彩色图像的对比度。到目前为止,我找到的最快的方法是使用这段代码(其中np是numpy导入)按照original C-based cv1 docs中的建议进行乘法和加法运算:

    if (self.array_alpha is None):
        self.array_alpha = np.array([1.25])
        self.array_beta = np.array([-100.0])

    # add a beta value to every pixel 
    cv2.add(new_img, self.array_beta, new_img)                    

    # multiply every pixel value by alpha
    cv2.multiply(new_img, self.array_alpha, new_img)  

在Python中有没有更快的方法来实现这一点?我试过使用numpy的标量乘法,但性能实际上更差。我也试过使用cv2.convertScaleAbs(OpenCV文档建议使用convertTo,但是cv2似乎缺少这个函数的接口),但是在测试中性能再次变差。


Tags: 方法selfalphanumpyaddimgnewvalue
3条回答

使用cv::addWeighted函数。它的设计是为了处理两个图像

dst = cv.addWeighted( src1, alpha, src2, beta, gamma[, dst[, dtype]] )

但是如果你使用同一张图片两次,把beta设置为0,你可以得到你想要的效果

dst = cv.addWeighted( src1, alpha, src1, 0, gamma)

使用此函数的最大优点是,当值低于0或高于255时,您不必担心会发生什么。在纽比,你得自己想办法剪掉所有的照片。使用OpenCV函数,它可以为您完成所有的剪辑,而且速度很快。

请尝试以下代码:

import cv2

img = cv2.imread('sunset.jpg', 1)
cv2.imshow("Original image",img)

# CLAHE (Contrast Limited Adaptive Histogram Equalization)
clahe = cv2.createCLAHE(clipLimit=3., tileGridSize=(8,8))

lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)  # convert from BGR to LAB color space
l, a, b = cv2.split(lab)  # split on 3 different channels

l2 = clahe.apply(l)  # apply CLAHE to the L-channel

lab = cv2.merge((l2,a,b))  # merge channels
img2 = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)  # convert from LAB to BGR
cv2.imshow('Increased contrast', img2)
#cv2.imwrite('sunset_modified.jpg', img2)

cv2.waitKey(0)
cv2.destroyAllWindows()

日落前: enter image description here 增强对比度后的日落: enter image description here

正如阿比德·拉哈曼K所评论的,numpy数组中的简单算法是最快的。

使用此图像例如:http://i.imgur.com/Yjo276D.png

下面是一些类似于亮度/对比度操作的图像处理:

'''
Simple and fast image transforms to mimic:
 - brightness
 - contrast
 - erosion 
 - dilation
'''

import cv2
from pylab import array, plot, show, axis, arange, figure, uint8 

# Image data
image = cv2.imread('imgur.png',0) # load as 1-channel 8bit grayscale
cv2.imshow('image',image)
maxIntensity = 255.0 # depends on dtype of image data
x = arange(maxIntensity) 

# Parameters for manipulating image data
phi = 1
theta = 1

# Increase intensity such that
# dark pixels become much brighter, 
# bright pixels become slightly bright
newImage0 = (maxIntensity/phi)*(image/(maxIntensity/theta))**0.5
newImage0 = array(newImage0,dtype=uint8)

cv2.imshow('newImage0',newImage0)
cv2.imwrite('newImage0.jpg',newImage0)

y = (maxIntensity/phi)*(x/(maxIntensity/theta))**0.5

# Decrease intensity such that
# dark pixels become much darker, 
# bright pixels become slightly dark 
newImage1 = (maxIntensity/phi)*(image/(maxIntensity/theta))**2
newImage1 = array(newImage1,dtype=uint8)

cv2.imshow('newImage1',newImage1)

z = (maxIntensity/phi)*(x/(maxIntensity/theta))**2

# Plot the figures
figure()
plot(x,y,'r-') # Increased brightness
plot(x,x,'k:') # Original image
plot(x,z, 'b-') # Decreased brightness
#axis('off')
axis('tight')
show()

# Close figure window and click on other window 
# Then press any keyboard key to close all windows
closeWindow = -1
while closeWindow<0:
    closeWindow = cv2.waitKey(1) 
cv2.destroyAllWindows()

原始图像灰度:

enter image description here

似乎被放大的明亮图像:

enter image description here

看起来被侵蚀、锐化、对比度更好的深色图像:

enter image description here

如何变换像素强度:

enter image description here

如果你玩弄phitheta的值,你会得到非常有趣的结果。您还可以对多通道图像数据实现此技巧。

---编辑---

this youtube video上查看“levels”和“curves”的概念,在photoshop中显示图像编辑。线性变换的方程式在每个像素上创建相同的变化量,即“级别”。如果你写了一个方程,可以区分不同类型的像素(例如,那些已经有一定值的像素),那么你可以根据方程描述的“曲线”来改变像素。

相关问题 更多 >