一文學會使用Opencv創(chuàng)建類似Instagram的濾鏡
什么是圖像濾鏡?
圖像濾鏡是一種方法或過程,通過它可以修改圖像的顏色、陰影、色調、飽和度、紋理和其他特征。濾鏡用于根據商業(yè)、藝術或審美需要在視覺上修改圖像。
如今,圖像濾鏡在社交媒體中非常普遍。Instagram 有各種各樣的濾鏡,Facebook 也是如此。Picsart 等編輯應用程序也提供了許多濾鏡。濾鏡可以為圖像提供新的視覺效果并使其看起來不同。人們使用濾鏡為他們的照片提供他們想要的效果。
這里OpenCV有什么用?
OpenCV 是一個免費使用的 Python 庫,可用于計算機視覺任務。它具有許多功能和方法,可用于執(zhí)行各種任務。我將應用一些圖像轉換方法來獲取濾鏡并創(chuàng)建所需的效果。
讓我們繼續(xù)進行所需的導入。
import cv2
import numpy as np
import scipy
我們將主要需要 NumPy 和 OpenCV,稍后將需要 SciPy。
現在讓我們閱讀圖像文件。
這是我們將要使用的圖像文件。
#Read the image
image = cv2.imread('shop.jpg')
現在,我們可以繼續(xù)實現濾鏡。
灰度濾鏡
我們從實現最基本和最廣泛使用的濾鏡開始。
灰度濾鏡用于為圖像提供黑白效果;旧先コ藞D像中的彩色成分。我們將使用 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)
輸出:
因此,我們可以看到圖像已成功轉換為灰度。接下來,讓我們嘗試另一個。
亮度調節(jié)
通常,我們看到濾鏡使圖像更亮,而其他濾鏡會降低亮度。這些是亮度調整濾鏡的結果。為此,我們將使用 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)
輸出:
正如我們所看到的,圖像現在更亮了,F在,讓我們制作一個更暗的圖像。
#making the less bright image
#negative beta value
a3 = bright(image, -60)
使用負Beta 值,F在,讓我們保存圖像。
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]]
讓我們繼續(xù)編碼:
#sharp effect
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
現在,讓我們保存圖像。
#making the sharp image
a4 = sharpen(image)
filename = 'sharpen.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a4)
輸出:
正如我們所看到的,圖像現在更清晰了。
棕褐色濾鏡
棕褐色是圖像編輯中最常用的濾鏡之一。棕褐色為照片增添了溫暖的棕色效果。復古、平靜和懷舊的效果被添加到圖像中。
讓我們在 Python 中實現。
為此,我們將使用 cv2.transform() 函數。繼續(xù)代碼。
#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.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
讓我們實現該功能并保存圖像。
#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 效果被大量使用,因為它增加了圖像的細節(jié)層次。我將使用 **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)
輸出:
反轉濾鏡
反轉濾鏡實際上很容易實現。每個人都曾在某些時候使用過這種濾鏡,讓他們的頭發(fā)變白。
所有,我們要做的基本上就是反轉像素值。這可以通過將像素值減去 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)
輸出:
好像是異世界的東西吧?好吧,反轉濾鏡確實很有趣。
現在我們將嘗試夏季和冬季效果濾鏡。
為此,我們需要一個查找表。但是從頭開始創(chuàng)建查找表是一個很大的過程。我們可以使用 SciPy 函數來實現這一點。
#defining a function
from scipy.interpolate import UnivariateSpline
def LookupTable(x, y):
spline = UnivariateSpline(x, y)
return spline(range(256))
現在,函數已定義,讓我們繼續(xù)。
夏季效果濾鏡
讓我們實現一個夏季效果濾鏡,它基本上增加了圖像的溫暖度。為了實現這一點,我們將增加紅色通道中的值并減少藍色通道中的值。
#summer effect
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
現在,保存圖像。
#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([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
代碼已實現。所以,現在我們保存圖像。
#making the winter img
a10 = Winter(image)
filename = 'Winter.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, a10)
輸出:
效果實現了。圖像暖度降低并產生寒冷效果。

請輸入評論內容...
請輸入評論/評論長度6~500個字
最新活動更多
推薦專題
- 1 AI 眼鏡讓百萬 APP「集體失業(yè)」?
- 2 大廠紛紛入局,百度、阿里、字節(jié)搶奪Agent話語權
- 3 深度報告|中國AI產業(yè)正在崛起成全球力量,市場潛力和關鍵挑戰(zhàn)有哪些?
- 4 上海跑出80億超級獨角獸:獲上市公司戰(zhàn)投,干人形機器人
- 5 國家數據局局長劉烈宏調研格創(chuàng)東智
- 6 下一代入口之戰(zhàn):大廠為何紛紛押注智能體?
- 7 百億AI芯片訂單,瘋狂傾銷中東?
- 8 Robotaxi新消息密集釋放,量產元年誰在領跑?
- 9 格斗大賽出圈!人形機器人致命短板曝光:頭腦過于簡單
- 10 “搶灘”家用機器人領域,聯通、海爾、美的等紛紛入局