從代碼實現濾鏡開始

從代碼實現濾鏡開始

來自專欄圖靈的彩筆14 人贊了文章

開設這個專欄的背景:現在從事計算機視覺相關的一些工作,國內的資料參差不齊。希望能吸引到一些有相關經驗的學者和工作者進來一起研究設計與演算法或者是設計與人工智慧。

計算機視覺在在技術上已經非常成熟了,簡單介紹下OpencvOpenCV的全稱是Open Source Computer Vision Library,是一個跨平台的計算機庫。OpenCV是由Inter公司發起並參與開發,以BSD許可證授權發行,可以在商業和研究領域中免費使用。OpenCV可用於開發實時的圖像處理、計算機視覺等程序。該程序庫也可以使用英特爾公司的IPP進行加速處理。那麼開始整體了,第一次想從大眾最能接受最容易上手的濾鏡開始,選用的是python語言。

一、顏色變換(基礎)

選用一張顏色分明的圖片

選自 unsplash 翻牆可以找到很多好看的圖片

1. 顏色互換 -- RGB2BGR

import cv2image = cv2.imread(color.jpg)BGR_image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)cv2.imshow(BGR,BGR_image)cv2.waitKey()

結果:

2.其他變換 -- 官方提供了很多種顏色轉換不一一列舉。

import cv2image = cv2.imread(color.jpg)BGR_image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)GRAY_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)HSV_image = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)LAB_image = cv2.cvtColor(image,cv2.COLOR_BGR2Lab)XYZ_image = cv2.cvtColor(image,cv2.COLOR_BGR2XYZ)cv2.imshow(RGB,image)cv2.imshow(BGR,BGR_image)cv2.imshow(GRAY,GRAY_image)cv2.imshow(HSV,HSV_image)cv2.imshow(LAB,LAB_image)cv2.imshow(XYZ,XYZ_image)cv2.waitKey()

結果:


二、風格變換

1. 鉛筆風格 (opencv自帶)

工作原理:製作鉛筆風格的演算法,修改圖像通道的顏色、邊緣。

需要幾步操作:

  • cv2.pencilSketch 製作鉛筆風格 吐出一個dist為gray 另一個是color

pencilSketch(src[, dst1[, dst2[, sigma_s[, sigma_r[, shade_factor]]]]]) -> dst1, dst2

. @brief Pencil-like non-photorealistic line drawing

. @param src Input 8-bit 3-channel image.

. @param dst1 Output 8-bit 1-channel image.

. @param dst2 Output image with the same size and type as src.

. @param sigma_s Range between 0 to 200.

. @param sigma_r Range between 0 to 1.

. @param shade_factor Range between 0 to 0.1.

  • cv2.stylization 讓圖片風格化 吐出一個風格化後的圖片

stylization(src[, dst[, sigma_s[, sigma_r]]]) -> dst

. @brief Stylization aims to produce digital imagery with a wide variety of effects not focused on

. photorealism. Edge-aware filters are ideal for stylization, as they can abstract regions of low

. contrast while preserving, or enhancing, high-contrast features.

. @param src Input 8-bit 3-channel image.

. @param dst Output image with the same size and type as src.

. @param sigma_s Range between 0 to 200.

. @param sigma_r Range between 0 to 1.

  • cv2.detailEnhance 將特徵細節增強 吐出細節化增強後的圖片

detailEnhance(src[, dst[, sigma_s[, sigma_r]]]) -> dst

. @brief This filter enhances the details of a particular image.

. @param src Input 8-bit 3-channel image.

. @param dst Output image with the same size and type as src.

. @param sigma_s Range between 0 to 200.

. @param sigma_r Range between 0 to 1.

  • cv2.edgePreservingFilter 降噪過濾器

edgePreservingFilter(src[, dst[, flags[, sigma_s[, sigma_r]]]]) -> dst

. @brief Filtering is the fundamental operation in image and video processing. Edge-preserving smoothing

. filters are used in many different applications @cite EM11 .

. @param src Input 8-bit 3-channel image.

. @param dst Output 8-bit 3-channel image.

. @param flags Edge preserving filters:

. - **RECURS_FILTER** = 1

. - **NORMCONV_FILTER** = 2

. @param sigma_s Range between 0 to 200.

. @param sigma_r Range between 0 to 1.

我錄屏看下分步驟效果:

https://www.zhihu.com/video/1000025794420346880

代碼如下:

import cv2img = cv2.imread("color.jpg")gray, color = cv2.pencilSketch(img, sigma_s=80, sigma_r=0.3, shade_factor=0.05)stylization = cv2.stylization(img, sigma_s=80, sigma_r=0.3)detailEnhance = cv2.detailEnhance(img, sigma_s=80, sigma_r=0.3)filter = cv2.edgePreservingFilter(img, flags=1, sigma_s=10, sigma_r=0.9)cv2.imshow("image", img)cv2.waitKey()cv2.imshow("gray", gray)cv2.waitKey()cv2.imshow("color", color)cv2.waitKey()cv2.imshow("stylization", stylization)cv2.waitKey()cv2.imshow("detailEnhance", detailEnhance)cv2.waitKey()cv2.imshow("filter", filter)cv2.waitKey()

2. 雕刻風格

工作原理:讀取圖片的形狀科學矩陣遍歷每個像素並且修改。

代碼如下:

import cv2import numpy as npimg = cv2.imread("color.jpg")shape = img.shapeheight, width = shape[0], shape[1]gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)dst = np.zeros((height, width, 1), np.uint8)for i in range(0, height): for j in range(0, width - 1): P0 = int(gray[i, j]) P1 = int(gray[i, j + 1]) P = P0 - P1 + 200 if P > 255: P = 255 elif P < 0: newP = 0 dst[i, j] = Pcv2.imshow(img, img)cv2.imshow(cameo, dst)cv2.waitKey()

3. 漫畫風格

工作原理:通過濾波改變圖像的風格

代碼如下:

import cv2img = cv2.imread("color.jpg")color = imgfor _ in xrange(2): color = cv2.pyrDown(color)for _ in xrange(7): color = cv2.bilateralFilter(color, d=7, sigmaColor=1, sigmaSpace=3)for _ in xrange(2): color = cv2.pyrUp(color)gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)blur = cv2.medianBlur(gray, 7)edge = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, blockSize=7, C=1)edge = cv2.cvtColor(edge, cv2.COLOR_GRAY2RGB)color = cv2.resize(img, (edge.shape[1], edge.shape[0]))cartoon = cv2.bitwise_and(color, edge)cv2.imshow(img, img)cv2.imshow(cartoon, cartoon)cv2.waitKey()

4. 彩色均衡

工作原理:將圖像分離split 對RGB分別進行均衡化

處理後

處理前

代碼如下:

import cv2img = cv2.imread(color.jpg)b, g, r = cv2.split(img)BH = cv2.equalizeHist(b)GH = cv2.equalizeHist(g)RH = cv2.equalizeHist(r)ELH = cv2.merge((BH, GH, RH))cv2.imshow(img,img)cv2.imshow(color, ELH)cv2.waitKey(0)

5. 彩色增強

工作原理:將圖像分離split 對RGB分別進行均衡化

處理後

處理前

代碼如下:

import cv2img = cv2.imread(color.jpg)YUV = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)channelYUV = cv2.split(YUV)channelYUV[0] = cv2.equalizeHist(channelYUV[0])channels = cv2.merge(channelYUV)YUV = cv2.cvtColor(channels, cv2.COLOR_YCrCb2BGR)cv2.imshow(img, img)cv2.imshow(yuv, YUV)cv2.waitKey()

6. 磨皮美白

工作原理:opencv自帶的雙邊濾波就可以完成

  • cv2.bilateralFilter 一個方法即可

bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]]) -> dst

. @brief Applies the bilateral filter to an image.

. The function applies bilateral filtering to the input image, as described in

. dai.ed.ac.uk/CVonline/L

. bilateralFilter can reduce unwanted noise very well while keeping edges fairly sharp. However, it is

. very slow compared to most filters.

.

. _Sigma values_: For simplicity, you can set the 2 sigma values to be the same. If they are small (<

. 10), the filter will not have much effect, whereas if they are large (> 150), they will have a very

. strong effect, making the image look "cartoonish".

.

. _Filter size_: Large filters (d > 5) are very slow, so it is recommended to use d=5 for real-time

. applications, and perhaps d=9 for offline applications that need heavy noise filtering.

.

. This filter does not work inplace.

. @param src Source 8-bit or floating-point, 1-channel or 3-channel image.

. @param dst Destination image of the same size and type as src .

. @param d Diameter of each pixel neighborhood that is used during filtering. If it is non-positive,

. it is computed from sigmaSpace.

. @param sigmaColor Filter sigma in the color space. A larger value of the parameter means that

. farther colors within the pixel neighborhood (see sigmaSpace) will be mixed together, resulting

. in larger areas of semi-equal color.

. @param sigmaSpace Filter sigma in the coordinate space. A larger value of the parameter means that

. farther pixels will influence each other as long as their colors are close enough (see sigmaColor

. ). When d>0, it specifies the neighborhood size regardless of sigmaSpace. Otherwise, d is

. proportional to sigmaSpace.

. @param borderType border mode used to extrapolate pixels outside of the image, see cv::BorderTypes

上面氣球的圖片不太好展示,換一張分步驟和參數查看。

處理前

處理後

代碼如下:

import cv2img = cv2.imread(girl.jpg)dst0 = cv2.bilateralFilter(img, 5, 10, 10)dst1 = cv2.bilateralFilter(img, 10, 20, 20)dst2 = cv2.bilateralFilter(img, 15, 50, 50)dst3 = cv2.bilateralFilter(img, 20, 70, 70)dst4 = cv2.bilateralFilter(img, 25, 100, 100)cv2.imshow(girl, img)cv2.imshow(beauty0, dst0)cv2.imshow(beauty1, dst1)cv2.imshow(beauty2, dst2)cv2.imshow(beauty3, dst3)cv2.imshow(beauty4, dst4)cv2.waitKey()

本文從最基礎的一些Opencv封裝好的演算法開始使用,希望循序漸進從最簡單的開始,之後會寫些業務層從而實現的濾鏡(不是搞學術科研的所以沒能力搞定自研演算法)

還有太多濾鏡可以去實現。以上為幾個典型的實現,如有興趣留言可以更新更多,也可以去研究封裝一些新的濾鏡。 歡迎有志之士加入專欄,可以一起研究討論。


以上。


推薦閱讀:

數碼單反相機濾鏡使用20問:
PS置換濾鏡製作逼真水面倒影的風景圖片
濾鏡的色溫,完美不熱不冷。
濾鏡系列知識

TAG:濾鏡 | 計算機視覺 | 科技 |