正如 Abid Rahaman 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()
灰度原始图像:
看起来膨胀的变亮图像:
看起来被侵蚀、锐化、对比度更好的变暗图像:
如何转换像素强度:
如果您使用 和 的值,phi
您theta
可以获得非常有趣的结果。您还可以为多通道图像数据实现此技巧。
- - 编辑 - -
看看这个 youtube 视频中的“级别”和“曲线”的概念,展示了 Photoshop 中的图像编辑。线性变换方程在每个像素上产生相同数量的变化,即“水平”变化。如果您编写一个可以区分像素类型的方程式(例如,那些已经具有特定值的像素),那么您可以根据该方程式描述的“曲线”来更改像素。