正如Abid Rahaman 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上“水平”和“曲线”的概念,在photoshop中显示图像编辑.线性变换的等式产生相同的量,即每个像素的变化“水平”.如果您编写一个可以区分像素类型的方程式(例如那些已经具有特定值的方程式),那么您可以根据该方程式描述的“曲线”更改像素.