正如阿比德·拉哈曼K所评论的,numpy数组中的简单算法是最快的。
下面是一些类似于亮度/对比度操作的图像处理:'''
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的值,你会得到非常有趣的结果。您还可以对多通道图像数据实现此技巧。
---编辑---
在this youtube video上查看“levels”和“curves”的概念,在photoshop中显示图像编辑。线性变换的方程式在每个像素上创建相同的变化量,即“级别”。如果你写了一个方程,可以区分不同类型的像素(例如,那些已经有一定值的像素),那么你可以根据方程描述的“曲线”来改变像素。