使用 Opencv 创建类似 Instagram 的滤镜!
这里OpenCV有什么用?
import numpy as np
import scipy
image = cv2.imread('shop.jpg')
def greyscale(img):
greyscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return greyscale
a1 = greyscale(image)
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a1)
输出:
亮度调节
def bright(img, beta_value ):
img_bright = cv2.convertScaleAbs(img, beta=beta_value)
return img_bright
#positive beta value
a2 = bright(image, 60)
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a2)
输出:
#negative beta value
a3 = bright(image, -60)
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a3)
输出:
锐利效果
def sharpen(img):
kernel = np.array([[-1, -1, -1], [-1, 9.5, -1], [-1, -1, -1]])
img_sharpen = cv2.filter2D(img, -1, kernel)
return img_sharpen
a4 = sharpen(image)
filename = 'sharpen.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a4)
输出:
棕褐色滤镜
def sepia(img):
img_sepia = np.array(img, dtype=np.float64) # converting to float to prevent loss
img_sepia = cv2.transform(img_sepia, np.matrix([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])) # multipying image with special sepia matrix
img_sepia[np.where(img_sepia > 255)] = 255 # normalizing values greater than 255 to 255
img_sepia = np.array(img_sepia, dtype=np.uint8)
return img_sepia
a5 = sepia(image)
filename = 'sepia.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a5)
输出:
铅笔素描效果:灰度
def pencil_sketch_grey(img):
#inbuilt function to create sketch effect in colour and greyscale
sk_gray, sk_color = cv2.pencilSketch(img, sigma_s=60, sigma_r=0.07, shade_factor=0.1)
return sk_gray
a6 = pencil_sketch_grey(image)
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a6)
输出:
铅笔素描效果:彩色版本
def pencil_sketch_col(img):
#inbuilt function to create sketch effect in colour and greyscale
sk_gray, sk_color = cv2.pencilSketch(img, sigma_s=60, sigma_r=0.07, shade_factor=0.1)
return sk_color
a7 = pencil_sketch_col(image)
filename = 'pencil_col.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a7)
输出:
HDR效果:
def HDR(img):
hdr = cv2.detailEnhance(img, sigma_s=12, sigma_r=0.15)
return hdr
a8 = HDR(image)
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a8)
输出:
反转滤镜
def invert(img):
inv = cv2.bitwise_not(img)
return inv
a9 = invert(image)
filename = 'invert.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a9)
from scipy.interpolate import UnivariateSpline
def LookupTable(x, y):
spline = UnivariateSpline(x, y)
return spline(range(256))
夏季效果滤镜
def Summer(img):
increaseLookupTable = LookupTable([0, 64, 128, 256], [0, 80, 160, 256])
decreaseLookupTable = LookupTable([0, 64, 128, 256], [0, 50, 100, 256])
blue_channel, green_channel,red_channel = cv2.split(img)
red_channel = cv2.LUT(red_channel, increaseLookupTable).astype(np.uint8)
blue_channel = cv2.LUT(blue_channel, decreaseLookupTable).astype(np.uint8)
sum= cv2.merge((blue_channel, green_channel, red_channel ))
return sum
a11 = Summer(image)
filename = 'Summer.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a11)
冬季效果滤镜:
def Winter(img):
increaseLookupTable = LookupTable([0, 64, 128, 256], [0, 80, 160, 256])
decreaseLookupTable = LookupTable([0, 64, 128, 256], [0, 50, 100, 256])
blue_channel, green_channel,red_channel = cv2.split(img)
red_channel = cv2.LUT(red_channel, decreaseLookupTable).astype(np.uint8)
blue_channel = cv2.LUT(blue_channel, increaseLookupTable).astype(np.uint8)
win= cv2.merge((blue_channel, green_channel, red_channel))
return win
a10 = Winter(image)
filename = 'Winter.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a10)
赞 (0)