1. 程式人生 > 實用技巧 >[Python影象處理]十三.影象特效處理

[Python影象處理]十三.影象特效處理

影象毛玻璃特效

影象毛玻璃特效是用影象鄰域內隨機一個畫素點的顏色來代替當前畫素點顏色的過程,從而為影象增加一個毛玻璃模糊的特效。

src = cv2.imread("rose.jpg")
dst = np.zeros_like(src)
rows, cols = src.shape[:2]
#定義偏移量和隨機數
offsets = 5
random_num = 0
# 毛玻璃效果: 畫素點鄰域內隨機畫素點的顏色替代當前畫素點的顏色
for y in range(rows - offsets):
    for x in range(cols - offsets):
        random_num 
= np.random.randint(0, offsets) dst[y, x] = src[y + random_num, x + random_num] cv2.imshow("src", src) cv2.imshow("result", dst) if cv2.waitKey() == 27: cv2.destroyAllWindows()

效果如下:

影象浮雕特效

將要呈現的影象突起於石頭表面,根據凹凸程度不同形成三維的立體效果。Python繪製浮雕影象是通過勾畫影象的輪廓,並降低周圍的畫素值,從而產生一張具有立體感的浮雕效果圖。設定卷積核,再呼叫filter2D()實現浮雕特效。

src = cv2.imread("rose.jpg")
dst = np.zeros_like(src)
rows, cols = src.shape[:2]
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# 目標影象
dst = np.zeros((rows, cols, 1), np.uint8)
for i in range(0, rows):
    for j in range(0, cols - 1):
        grayCurrentPixel = int(gray[i, j])
        grayNextPixel = int(gray[i, j + 1])
        nextPixel 
= grayCurrentPixel - grayNextPixel + 120 if nextPixel > 255: nextPixel = 255 if nextPixel < 0: nextPixel = 0 dst[i, j] = nextPixel cv2.imshow("src", gray) cv2.imshow("result", dst) if cv2.waitKey() == 27: cv2.destroyAllWindows()

效果如下:

影象油漆特效

它主要採用自定義卷積核和cv2.filter2D(),卷積核公式

src = cv2.imread("rose.jpg")
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# 自定義卷積核
kerbnel = np.array([[-1, -1, -1],[-1, 10, -1],[-1, -1, -1]])
# 影象浮雕效果
dst = cv2.filter2D(gray, -1, kerbnel)
cv2.imshow("src", gray)
cv2.imshow("result", dst)
if cv2.waitKey() == 27:
    cv2.destroyAllWindows()

效果如下:

影象素描特效

主要通過以下步驟:

1:呼叫cv2.cvtColor()函式將彩色影象灰度化處理

2:通過cv2.GaussianBlur()函式實現高斯濾波降噪

3:邊緣檢測採用Canny運算元實現

4:最後通過cv2.threshold()反二進位制閾值化處理實現素描特效

src = cv2.imread("rose.jpg")
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
gaussian = cv2.GaussianBlur(gray, (5, 5), 0)
canny = cv2.Canny(gaussian, 50, 150)
ret, result = cv2.threshold(canny, 100, 255, cv2.THRESH_BINARY_INV)
cv2.imshow("src", gray)
cv2.imshow("result", result)
if cv2.waitKey() == 27:
    cv2.destroyAllWindows()

效果如下:

影象懷舊特效

懷舊特效是將影象的RGB三個分量分別按照一定比例進行處理的結果,其懷舊公式如下:

主要通過雙層迴圈遍歷影象的各畫素點,再結合公式計算各顏色通道的畫素值。

img = cv2.imread("rose.jpg", 1)
rows, cols = img.shape[:2]
dst = np.zeros((rows, cols, 3), dtype=np.uint8)
for i in range(rows):
    for j in range(cols):
        B = 0.272 * img[i, j][2] + 0.534 * img[i, j][1] + 0.131 * img[i, j][0]
        G = 0.349 * img[i, j][2] + 0.686 * img[i, j][1] + 0.168 * img[i, j][0]
        R = 0.393 * img[i, j][2] + 0.769 * img[i, j][1] + 0.189 * img[i, j][0]
        if B > 255:
            B = 255
        if G > 255:
            G = 255
        if R < 255:
            R = 255
        dst[i, j] = np.uint8((B, G, R))
cv2.imshow("src", img)
cv2.imshow("result", dst)
if cv2.waitKey() == 27:
    cv2.destroyAllWindows()

效果如下:

影象光照特效

主要是通過雙層迴圈遍歷影象的各畫素點,尋找影象的中心點,再通過計算當前點到光照中心的距離(平面座標系中兩點之間的距離),判斷該距離與影象中點圓半徑的大小關係,中心圓範圍內的影象灰度值增強,範圍外的影象灰度值保留,並結合邊界範圍判斷生成最終的光照效果。

import math
img = cv2.imread("rose.jpg", 1)
rows, cols = img.shape[:2]
centerX = int(rows / 2)
centerY = int(cols / 2)
radius = min(centerX, centerY)
strength = 200
dst = np.zeros((rows, cols, 3), dtype=np.uint8)
for i in range(rows):
    for j in range(cols):
        # 計算當前點到光照中心距離
        distance = math.pow((centerY - j ), 2) + math.pow((centerX - i), 2)
        B = img[i, j][0]
        G = img[i, j][1]
        R = img[i, j][2]
        if distance < (radius * radius):
            result = int(strength * (1.0 - math.sqrt(distance) / radius))
            B = img[i, j][0] + result
            G = img[i, j][1] + result
            R = img[i, j][2] + result
            # 判斷邊界,防止越界
            B = min(255, max(0, B))
            G = min(255, max(0, G))
            R = min(255, max(0, R))
            dst[i, j] = np.uint8((B, G, R))
        else:
            dst[i, j] = np.uint8((B, G, R))
cv2.imshow("src", img)
cv2.imshow("result", dst)
if cv2.waitKey() == 27:
    cv2.destroyAllWindows()

效果如下:

影象流年特效

將原始影象的任一種元素(B/G/R)通道的畫素開根號,再乘以一個權重引數,產生流年效果。

import math
img = cv2.imread("rose.jpg", 1)
rows, cols = img.shape[:2]
dst = np.zeros((rows, cols, 3), dtype=np.uint8)
for i in range(rows):
    for j in range(cols):
        # R通道的數值開平方再乘以12
        B = img[i, j][0]
        G = img[i, j][1]
        R = math.sqrt(img[i, j][2]) * 12
        R = min(255, max(0, R))
        dst[i, j] = np.uint8((B, G, R))
cv2.imshow("src", img)
cv2.imshow("result", dst)
if cv2.waitKey() == 27:
    cv2.destroyAllWindows()

效果如下:

影象濾鏡特效

# 獲取濾鏡顏色
def getBGR(img, table, i, j):
    # 獲取影象顏色
    b, g, r = img[i][j]
    # 計算標準顏色表中顏色的位置座標
    x = min(255, int(g / 4 + int(b / 32) * 64))
    y = min(255, int(r / 4 + int((b % 32) / 4) * 64))
    # 返回濾鏡顏色表中對應的顏色
    return table[x][y]

# 讀取原始影象
img = cv2.imread('rose.jpg')
lj_map = cv2.imread('table.png')
# 獲取影象行和列
rows, cols = img.shape[:2]
# 新建目標影象
dst = np.zeros((rows, cols, 3), dtype=np.uint8)
# 迴圈設定濾鏡顏色
for i in range(rows):
    for j in range(cols):
        dst[i][j] = getBGR(img, lj_map, i, j)

# 顯示影象
cv2.imshow('src', img)
cv2.imshow('dst', dst)

if cv2.waitKey() == 27:
    cv2.destroyAllWindows()

效果如下:

轉自: https://blog.csdn.net/Eastmount/article/details/99566969