<p>正如阿比德·拉哈曼K所评论的,numpy数组中的简单算法是最快的。</p>
<p>使用此图像例如:<a href="https://i.imgur.com/Yjo276D.png">http://i.imgur.com/Yjo276D.png</a></p>
<p>下面是一些类似于亮度/对比度操作的图像处理:</p>
<pre><code>'''
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()
</code></pre>
<p>原始图像灰度:</p>
<p><img src="https://i.stack.imgur.com/Zh5bH.jpg" alt="enter image description here"/></p>
<p>似乎被放大的明亮图像:</p>
<p><img src="https://i.stack.imgur.com/d4PWo.jpg" alt="enter image description here"/></p>
<p>看起来被侵蚀、锐化、对比度更好的深色图像:</p>
<p><img src="https://i.stack.imgur.com/jICZl.jpg" alt="enter image description here"/></p>
<p>如何变换像素强度:</p>
<p><img src="https://i.stack.imgur.com/63CBQ.png" alt="enter image description here"/></p>
<p>如果你玩弄<code>phi</code>和<code>theta</code>的值,你会得到非常有趣的结果。您还可以对多通道图像数据实现此技巧。</p>
<p><strong>---编辑---</strong></p>
<p>在<a href="http://youtu.be/hZeMqXxMG_8">this youtube video</a>上查看“levels”和“curves”的概念,在photoshop中显示图像编辑。线性变换的方程式在每个像素上创建相同的变化量,即“级别”。如果你写了一个方程,可以区分不同类型的像素(例如,那些已经有一定值的像素),那么你可以根据方程描述的“曲线”来改变像素。</p>