本文實例講述了Python實現PS濾鏡中馬賽克效果。分享給大家供大家參考,具體如下:
這里利用 Python 實現PS 濾鏡中的馬賽克效果,具體的算法原理和效果可以參考附錄說明,Python示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
from skimage import img_as_float import matplotlib.pyplot as plt from skimage import io import random import numpy as np file_name = 'D:/Visual Effects/PS Algorithm/4.jpg' ; img = io.imread(file_name) img = img_as_float(img) img_out = img.copy() row, col, channel = img.shape half_patch = 10 for i in range (half_patch, row - 1 - half_patch, half_patch): for j in range (half_patch, col - 1 - half_patch, half_patch): k1 = random.random() - 0.5 k2 = random.random() - 0.5 m = np.floor(k1 * (half_patch * 2 + 1 )) n = np.floor(k2 * (half_patch * 2 + 1 )) h = int ((i + m) % row) w = int ((j + n) % col) img_out[i - half_patch:i + half_patch, j - half_patch:j + half_patch, :] = \ img[h, w, :] plt.figure( 1 ) plt.imshow(img) plt.axis( 'off' ) plt.figure( 2 ) plt.imshow(img_out) plt.axis( 'off' ) plt.show() |
附:PS 濾鏡算法原理 ——馬賽克
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
% method : 利用鄰域的任意一點代替當前鄰域所有像素點 % % % % mosaic clc; clear all ; addpath( 'E:\PhotoShop Algortihm\Image Processing\PS Algorithm' ); Image = imread( '4.jpg' ); Image = double(Image); size_info = size(Image); height = size_info( 1 ); width = size_info( 2 ); N = 11 ; % 控制鄰域大小 Image_out = Image; for i = 1 + N:N:height - N for j = 1 + N:N:width - N k1 = rand() - 0.5 ; k2 = rand() - 0.5 ; m = (k1 * (N * 2 - 1 )); n = (k2 * (N * 2 - 1 )); h = floor(mod(i + m,height)); w = floor(mod(j + n,width)); if w = = 0 ; w = width; end if h = = 0 h = height; end Image_out(i - N:i + N,j - N:j + N, 1 ) = Image(h,w, 1 ); Image_out(i - N:i + N,j - N:j + N, 2 ) = Image(h,w, 2 ); Image_out(i - N:i + N,j - N:j + N, 3 ) = Image(h,w, 3 ); end end imshow(Image_out / 255 ); |
原圖
效果圖
希望本文所述對大家Python程序設計有所幫助。
原文鏈接:http://blog.csdn.net/matrix_space/article/details/72305574