分享

​使用 Opencv 创建类似 Instagram 的滤镜!

 新用户0118F7lQ 2021-10-06
什么是图像滤镜?
图像滤镜是一种方法或过程,通过它可以修改图像的颜色、阴影、色调、饱和度、纹理和其他特征。滤镜用于根据商业、艺术或审美需要在视觉上修改图像。
如今,图像滤镜在社交媒体中非常普遍。Instagram 有各种各样的滤镜,Facebook 也是如此。Picsart 等编辑应用程序也提供了许多滤镜。滤镜可以为图像提供新的视觉效果并使其看起来不同。人们使用滤镜为他们的照片提供他们想要的效果。

这里OpenCV有什么用?

OpenCV 是一个免费使用的 Python 库,可用于计算机视觉任务。它具有许多功能和方法,可用于执行各种任务。我将应用一些图像转换方法来获取滤镜并创建所需的效果。
让我们继续进行所需的导入。
import cv2
import numpy as np
import scipy
我们将主要需要 NumPy 和 OpenCV,稍后将需要 SciPy。
现在让我们阅读图像文件。
图片
这是我们将要使用的图像文件。
#Read the image
image = cv2.imread('shop.jpg')
现在,我们可以继续实现滤镜
我们从实现最基本和最广泛使用的滤镜开始。
灰度滤镜用于为图像提供黑白效果。基本上去除了图像中的彩色成分。我们将使用 **cv2.cvtColor()**将图像转换为灰度。
#greyscale filter
def greyscale(img):
    greyscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    return greyscale
现在,将该函数应用于我们的图像。
#making the greyscale image
a1 = greyscale(image)
现在,我们将图像保存为文件。
filename = 'greyscale.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a1)

输出:

图片
因此,我们可以看到图像已成功转换为灰度。接下来,让我们尝试另一个。

亮度调节

通常,我们看到滤镜使图像更亮,而其他滤镜会降低亮度。这些是亮度调整滤镜的结果。为此,我们将使用 cv2.convertScaleAbs()。可以更改 Beta 值以获得适当的结果。
# brightness adjustment
def bright(img, beta_value ):
    img_bright = cv2.convertScaleAbs(img, beta=beta_value)
    return img_bright
函数已定义,现在 beta 值将给出适当的结果。正值表示图像较亮,负值表示图像较暗。
#making the  more bright image
#positive beta value
a2 = bright(image, 60)
现在,我们保存图像。
filename = 'more_bright.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a2)

输出:

图片
正如我们所看到的,图像现在更亮了。现在,让我们制作一个更暗的图像。
#making the  less bright image
#negative beta value
a3 = bright(image, -60)
使用负Beta 值。现在,让我们保存图像。
filename = 'less_bright.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a3)

输出:

图片
我们可以看到图像现在不那么亮了。

锐利效果

锐化效果也被大量使用。我们将使用OpenCV 中的 filter2D方法进行适当的编辑。
锐化效果的内核将是:[[-1, -1, -1], [-1, 9.5, -1], [-1, -1, -1]]
让我们继续编码:
#sharp effect
def sharpen(img):
    kernel = np.array([[-1-1-1], [-19.5-1], [-1-1-1]])
    img_sharpen = cv2.filter2D(img, -1, kernel)
    return img_sharpen
现在,让我们保存图像。
#making the sharp image
a4 = sharpen(image)
filename = 'sharpen.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a4)

输出:

图片
正如我们所看到的,图像现在更清晰了。

棕褐色滤镜

棕褐色是图像编辑中最常用的滤镜之一。棕褐色为照片增添了温暖的棕色效果。复古、平静和怀旧的效果被添加到图像中。
让我们在 Python 中实现。
为此,我们将使用 cv2.transform() 函数。继续代码。
#sepia effect
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.2720.5340.131],
                                    [0.3490.6860.168],
                                    [0.3930.7690.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
让我们实现该功能并保存图像。
#making the sepia image
a5 = sepia(image)
filename = 'sepia.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a5)

输出:

图片
效果看起来很棒。滤镜实现完美。

铅笔素描效果:灰度

让我们实现一个灰度铅笔素描效果。事实上,它很容易实现,因为有一个内置函数来实现它。
#grey pencil sketch effect
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
现在,我们应用该函数并保存图像。
#making the grey pencil sketch
a6 = pencil_sketch_grey(image)
filename = 'pencil_grey.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a6)

输出:

图片
确实,图像看起来像一个粗略的铅笔素描。现在,是时候实现彩色版本了。

铅笔素描效果:彩色版本

现在,我们实现铅笔素描效果的彩色版本。
#colour pencil sketch effect
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
我们应用该函数并保存图像。
#making the colour pencil sketch
a7 = pencil_sketch_col(image)
filename = 'pencil_col.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a7)

输出:

图片
效果相当有趣,总体而言,实现了铅笔素描效果。

HDR效果:

HDR 效果被大量使用,因为它增加了图像的细节层次。我将使用 **cv2.detailEnhance()**来实现这一点。
#HDR effect
def HDR(img):
    hdr = cv2.detailEnhance(img, sigma_s=12, sigma_r=0.15)
    return  hdr
现在,我们应用该函数。
#making the hdr img
a8 = HDR(image)
现在,我们保存图像。
filename = 'HDR.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a8)

输出:

图片

反转滤镜

反转滤镜实际上很容易实现。每个人都曾在某些时候使用过这种滤镜,让他们的头发变白。( 哈哈 )
所有,我们要做的基本上就是反转像素值。这可以通过将像素值减去 255 来完成。在 Python 中,我们可以为此使用 **cv2.bitwise_not()**函数。
# invert filter
def invert(img):
    inv = cv2.bitwise_not(img)
    return inv
现在,让我们应用该功能并保存图像。
#making the invert img
a9 = invert(image)
filename = 'invert.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a9)
输出:
图片
好像是异世界的东西吧?好吧,反转滤镜确实很有趣。
现在我们将尝试夏季和冬季效果滤镜。
为此,我们需要一个查找表。但是从头开始创建查找表是一个很大的过程。我们可以使用 SciPy 函数来实现这一点。
#defining a function
from scipy.interpolate import UnivariateSpline
def LookupTable(x, y):
  spline = UnivariateSpline(x, y)
  return spline(range(256))
现在,函数已定义,让我们继续。

夏季效果滤镜

让我们实现一个夏季效果滤镜,它基本上增加了图像的温暖度。为了实现这一点,我们将增加红色通道中的值并减少蓝色通道中的值。
#summer effect
def Summer(img):
    increaseLookupTable = LookupTable([064128256], [080160256])
    decreaseLookupTable = LookupTable([064128256], [050100256])
    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
现在,保存图像。
#making the summer img
a11 = Summer(image)
filename = 'Summer.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a11)
输出:
图片
实现了夏季效果滤镜。
现在,我们实现冬季效果滤镜。

冬季效果滤镜:

在冬季效果滤镜中,将进行相反的操作。图像的温暖度会降低。红色通道中的值将减少,蓝色通道中的值将增加。
#winter effect
def Winter(img):
    increaseLookupTable = LookupTable([064128256], [080160256])
    decreaseLookupTable = LookupTable([064128256], [050100256])
    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
代码已实现。所以,现在我们保存图像。
#making the winter img
a10 = Winter(image)
filename = 'Winter.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a10)
输出:
图片
效果实现了。图像暖度降低并产生寒冷效果。
要检查代码和图像文件,请检查此链接: https://github.com/prateekmaj21/Image-Processing-Tasks

☆ END ☆

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多