Python 計算機視覺(十五)—— 影像特效處理

專注的阿熊發表於2021-11-22

"""

Author:XiaoMa

date:2021/11/16

"""

import cv2

import numpy as np

import math

import matplotlib.pyplot as plt

img0 = cv2.imread('E:\From Zhihu\For the desk\cvfifteen1.jpg')

img1 = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)

h, w = img0.shape[:2]

print(h, w)

cv2.imshow("W0", img0)

cv2.imshow("W1", img1)

cv2.waitKey(delay = 0)

# 毛玻璃特效

img2 = np.zeros((h - 6, w - 6, 3), np.uint8)        # 生成的全零矩陣考慮到了隨機數範圍,變小了

for i in range(0, h - 6):                   # 防止下面的隨機數超出邊緣

     for j in range(0, w - 6):

         index = int(np.random.random()*6)   #0~6 的隨機數

         (b, g, r) = img0[i + index, j + index]

         img2[i, j] = (b, g, r)

cv2.imshow("W2", img2)

cv2.waitKey(delay = 0)

# 浮雕特效 ( 需要對灰度影像進行操作 )

img3 = np.zeros((h, w, 3), np.uint8)

for i in range(0, h):

     for j in range(0, w - 2):                # 2 的效果和上面一樣

         grayP0 = int(img1[i, j])

         grayP1 = int(img1[i, j + 2])         # 取與前一個畫素點相鄰的點

         newP = grayP0 - grayP1 + 150         # 得到差值,加一個常數可以增加浮雕立體感

         if newP > 255:

             newP = 255

         if newP < 0:

             newP = 0

         img3[i, j] = newP

cv2.imshow("W3", img3)

cv2.waitKey(delay = 0)

# 素描特效

img4 = 255 - img1                                               # 對原灰度影像的畫素點進行反轉

blurred = cv2.GaussianBlur(img4, (21, 21), 0)                   # 進行高斯模糊

inverted_blurred = 255 - blurred                                # 反轉

img4 = cv2.divide(img1, inverted_blurred, scale = 127.0)        # 灰度影像除以倒置的模糊影像得到鉛筆素描畫

cv2.imshow("W4", img4)

cv2.waitKey(delay = 0)

# 懷舊特效

img5 = np.zeros((h, w, 3), np.uint8)

for i in range(0, h):

     for j in range(0, w):

         B = 0.272 * img0[i, j][2] + 0.534 * img0[i, j][1] + 0.131 * img0[i, j][0]

         G = 0.349 * img0[i, j][2] + 0.686 * img0[i, j][1] + 0.168 * img0[i, j][0]

         R = 0.393 * img0[i, j][2] + 0.769 * img0[i, j][1] + 0.189 * img0[i, j][0]

         if B > 255:

             B = 255

         if G > 255:

             G = 255

         if R > 255:

             R = 255

         img5[i, j] = np.uint8((B, G, R))

cv2.imshow("W5", img5)

cv2.waitKey(delay = 0)

# 流年特效

img6 = np.zeros((h, w, 3), np.uint8)

for i in range(0, h):

     for j in range(0, w):

         B = math.sqrt(img0[i, j][0]) *14       # B 通道的數值開平方乘以引數 14

         G = img0[i, j][1]

         R = img0[i, j][2]

         if B > 255:

             B = 255

         img6[i, j] = np.uint8((B, G, R))

cv2.imshow("W6", img6)

cv2.waitKey(delay = 0)

# 水波特效

img7 = np.zeros((h, w, 3), np.uint8)

wavelength = 20                                 # 定義水波特效波長

amplitude = 30                                  # 幅度

phase = math.pi / 4                             # 相位

centreX = 0.5                                   # 水波中心點 X

centreY = 0.5                                   # 水波中心點 Y

radius = min(h, w) / 2

icentreX = w*centreX                            # 水波覆蓋寬度

icentreY = h*centreY                            # 水波覆蓋高度

for i in range(0, h):

     for j in range(0, w):

         dx = j - icentreX

         dy = i - icentreY

         distance = dx * dx + dy * dy

         if distance > radius * radius:

             x = j

             y = i

         else:

             # 計算水波區域

             distance = math.sqrt(distance)

             amount = amplitude * math.sin(distance / wavelength * 2 * math.pi - phase)

             amount = amount * (radius - distance) / radius

             amount = amount * wavelength / (distance + 0.0001)

             x = j + dx * amount

             y = i + dy * amount

         # 邊界判斷

         if x < 0:

             x = 0

         if x >= w - 1:

             x = w - 2

         if y < 0:

             y = 0

         if y >= h - 1:

             y = h - 2

         p = x - int(x)

         q = y - int(y)

         # 影像水波賦值

         img7[i, j, :] = (1 - p) * (1 - q) * img0[int(y), int(x), :] + p * (1 - q) * img0[int(y), int(x), :]

         + (1 - p) * q * img0[int(y), int(x), :] + p * q * img0[int(y), int(x), :]

cv2.imshow("W7", img7)

cv2.waitKey(delay = 0)

# 卡通特效

num_bilateral = 7                                      # 定義雙邊濾波的數目

for i in range(num_bilateral):                         # 雙邊濾波處理

     img_color = cv2.bilateralFilter(img0, d = 9, sigmaColor = 5, sigmaSpace = 3)

img_blur = cv2.medianBlur(img1, 7)                     # 中值濾波處理

img_edge = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize = 5, C = 2) # 邊緣檢測及自適應閾值化處理

img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)  # 轉換回彩色影像

img8 = cv2.bitwise_and(img0, img_edge)          # 影像的與運算

cv2.imshow('W8', img8)

cv2.waitKey(delay = 0)

# 將所有影像儲存到一張圖中

plt.rcParams['font.family'] = 'SimHei'

imgs = [img0, img1, img2, img3, img4, img5, img6, img7, img8]

titles =外匯跟單gendan5.com [' 原圖 ', ' 灰度圖 ', ' 毛玻璃特效 ', ' 浮雕特效 ', ' 素描特效 ', ' 懷舊特效 ', ' 流年特效 ', ' 水波特效 ', ' 卡通特效 ']

for i in range(9):

     imgs[i] = cv2.cvtColor(imgs[i], cv2.COLOR_BGR2RGB)

     plt.subplot(3, 3, i + 1)

     plt.imshow(imgs[i])

     plt.title(titles[i])

     plt.xticks([])

     plt.yticks([])

plt.suptitle(' 影像特效處理 ')

plt.savefig('E:\From Zhihu\For the desk\cvfifteenresult.jpg', dpi = 1080)

plt.show()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2843513/,如需轉載,請註明出處,否則將追究法律責任。

相關文章