Python implements the rotation blur function example of the PS filter, python Filter

Source: Internet
Author: User

Python implements the rotation blur function example of the PS filter, python Filter

This article describes how to implement the rotation blur function of the PS filter in Python. We will share this with you for your reference. The details are as follows:

Here, we use Python to implement rotation blur in the PS filter. For more information about the algorithm principles and effects, see the appendix. The Python code is as follows:

from skimage import img_as_floatimport matplotlib.pyplot as pltfrom skimage import ioimport numpy as npimport numpy.matlibfile_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.shapexx = np.arange (col)yy = np.arange (row)x_mask = numpy.matlib.repmat (xx, row, 1)y_mask = numpy.matlib.repmat (yy, col, 1)y_mask = np.transpose(y_mask)center_y = (row -1) / 2.0center_x = (col -1) / 2.0R = np.sqrt((x_mask - center_x) **2 + (y_mask - center_y) ** 2)angle = np.arctan2(y_mask - center_y , x_mask - center_x)Num = 20arr = ( np.arange(Num) + 1 ) / 100.0for i in range (row):  for j in range (col):    T_angle = angle[i, j] + arr    new_x = R[i, j] * np.cos(T_angle) + center_x    new_y = R[i, j] * np.sin(T_angle) + center_y    int_x = new_x.astype(int)    int_y = new_y.astype(int)    int_x[int_x > col-1] = col - 1    int_x[int_x < 0] = 0    int_y[int_y < 0] = 0    int_y[int_y > row -1] = row -1    img_out[i,j,0] = img[int_y, int_x, 0].sum()/Num    img_out[i,j,1] = img[int_y, int_x, 1].sum()/Num    img_out[i,j,2] = img[int_y, int_x, 2].sum()/Numplt.figure(1)plt.imshow(img)plt.axis('off')plt.figure(2)plt.imshow(img_out)plt.axis('off')plt.show()

Appendix: PS filter-rotating blur

Here we provide a fuzzy algorithm for Grayscale Images. color images only need to blur three channels.

% Spin blur % rotation blur clc; clear all; close allpolici1_imread('4.jpg '); I = double (I); % I _new = I; % for kk = % I _new (:,:, kk) = Spin_blur_Fun (I (:,:, kk), 30, 30 ); % end % imshow (I _new/255) Image = I; Image = 0.2989 * I (:,:, 1) + 0.5870 * I (:,:, 2) + 0.1140 * I (:,:, 3); [row, col] = size (Image); Image_new = Image; Center_X = (col + 1)/2; center_Y = (row + 1)/2; validPoint = 1; angle = 5; radian = angle * pi/180; radian2 = radian * radian; Num = 30; num2 = Num * Num; for I = 1: row for j = 1: col validPoint = 1; x0 = j-Center_X; y0 = Center_Y-i; x1 = x0; y1 = y0; sum_Pixel = Image (I, j); for k = 1: Num x0 = x1; y0 = y1; % % x1 = x0-radian * y0/Num-radian2 * x0/Num2; % y1 = y0 + radian * x0/Num-radian2 * y0/Num2; % clockwise x1 = x0 + radian * y0/Num-radian2 * x0/Num2; y1 = y0-radian * x0/Num-radian2 * y0/Num2; x = floor (x1 + Center_X); y = floor (Center_Y-y1 ); if (x> 1 & x <col & y> 1 & y <row) validPoint = validPoint + 1; Sum_Pixel = Sum_Pixel + Image (y, x ); end Image_new (I, j) = Sum_Pixel/validPoint; endend imshow (Image_new/255 );

Source image

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.