SciPy module applications

1. The image blur

        Gaussian blur image is very classic image convolution example . In essence, it is to blur the image (grayscale) images nuclei I and a Gaussian convolution: where is the standard deviation σ of a two-dimensional Gaussian kernel. Gaussian blur is generally part of other image processing operations, such as image interpolation operation, the point of interest is calculated, and many other applications. Useful filtering operations do SciPy scipy.ndimage.filters module. The module uses a one-dimensional fast way to calculate the convolution isolated. eg:         

from the PIL Import Image
 from numpy * Import
 from scipy.ndimage Import Filters 
IM = Array (Image.open ( ' empire.jpg ' ) .convert ( ' L ' )) 
IM2 = filters.gaussian_filter (IM, . 5 )% second parameter represents the standard deviation

         With the increase of σ, an image of the degree of blurring. The larger σ, the processed image details lost more. If you intend to blur a color image, simply for each color channel Gaussian blur:

im = array(Image.open('empire.jpg'))
im2 = zeros(im.shape)
for i in range(3):
   im2[:,:,i] = filters.gaussian_filter(im[:,:,i],5)
im2 = uint8(im2)

                           

                                        An original image using a Gaussian filter σ = 5

 

 2. derivative image

      In many applications the change of image intensity is very important information. Change in intensity of gray scale image can be I (for color images, typically calculate the derivative for each color channel) is the derivative of the x and y directions the I x and the I y be described. Is the gradient vector of the image . There are two important properties of a gradient, one size gradient: it describes changes in image intensity strength, one of the gradient angle: describing the image on the direction of maximum intensity change at each point (pixel). Arctan2 NumPy in () function returns the signed angle in radians, the angle change interval -π ... π. It can be approximated by a discrete way to calculate the derivative images. Most of the derivative image can be easily achieved by convolution: .

      For Dx and Dy, generally selected Prewitt filter:

                         

                                   Or Sobel filter:

                         

   These derivative filter may be used scipy.ndimage.filters standard convolution module simply achieved:   

from the PIL Import Image
 from numpy * Import
 from scipy.ndimage Import Filters 
IM = Array (Image.open ( ' empire.jpg ' ) .convert ( ' L ' )) is converted to grayscale images # 
# Sobel derivative filter 
IMX = zeros (im.shape) 
filters.sobel (IM, . 1 , IMX) 
IMY = zeros (im.shape) 
filters.sobel (IM, 0 , IMY) 
Magnitude = sqrt (IMX ** 2 + IMY ** 2 )

     The above script using the Sobel filter to calculate the derivative of the x and y directions, and the gradient magnitude. Sobel () function of the second parameter represents the variable x or the derivative selected y-direction, the third parameter stored output. In the two derivative images, positive derivative as a bright pixel, the negative derivative as a dark pixel. The gray area represents the value of the derivative close to zero.

     The method of calculating the above-described image derivative has some disadvantages: In this method, the filter need to scale the image resolution varies varies. For a more robust in terms of image noise, and calculating the derivative at any scale, Gaussian derivative filter may be used: .

      Prior to use fuzzy filters.gaussian_filter () function can accept additional parameter is used to calculate the Gaussian derivative. May simply be processed in the following manner:   

sigma = 5 # 标准差
imx = zeros(im.shape)
filters.gaussian_filter(im, (sigma,sigma), (0,1), imx)
imy = zeros(im.shape)
filters.gaussian_filter(im, (sigma,sigma), (1,0), imy)

    The third parameter specifies the derivative of the function which is calculated for each type of direction, the second parameter is the standard used in the difference.

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160677.htm