圖像毛玻璃特效
圖像毛玻璃特效是用圖像鄰域內隨機一個像素點的顏色來代替當前像素點顏色的過程,從而為圖像增加一個毛玻璃模糊的特效。
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