Python圖像特效之模糊玻璃效果
發(fā)布日期:2022-01-22 18:56 | 文章來源:源碼中國
今天介紹一種基于高斯濾波和鄰域隨機采樣,生成一種毛玻璃的圖像特效,簡單來說,就是先對圖像做高斯濾波模糊,然后對模糊后的圖像,通過對鄰域的隨機采樣來賦予當前的像素點,這樣,生成的圖像有有一定的隨機擾動和模糊,看起來就像隔著一層毛玻璃在觀察圖像一樣。
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 20 11:03:53 2017
@author: shiyi
"""
import matplotlib.pyplot as plt
from skimage.filters import gaussian
from scipy.misc import imsave, imread
import random
file_name='D:/Visual Effects/PS Algorithm/4.jpg';
img=imread(file_name)
g_img = gaussian(img, sigma=2, multichannel=True)
img_out = g_img.copy()
rows, cols, dpt = img.shape
p_size = 3
for i in range(p_size, rows-p_size, 1):
for j in range(p_size, cols-p_size, 1):
k1= random.random() - 0.5
k2= random.random() - 0.5
m=int (k1*(p_size*2-1))
n=int (k2*(p_size*2-1))
h=(i+m) % rows
w=(j+n) % cols
img_out[i, j, :] = g_img[h, w, :]
imsave('out.jpg', img_out)
plt.figure
plt.imshow(img_out)
plt.show()
效果圖:

效果圖:

小編再為大家分享一段之前收藏的實例,感謝原作者的分享。
#coding:utf-8
'''
毛玻璃效果
'''
import cv2
import numpy as np
src = cv2.imread('datas/images/f1.jpg')
dst = np.zeros_like(src)
rows,cols,_ = src.shape
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('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持本站。
版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
相關文章
關注官方微信