Python Image processing library (2)

Source: Internet
Author: User
Tags image processing library

1.4 SciPy

SciPy(http://scipy.org/) is an NumPy open source toolkit built on the basis of numerical operations. SciPyprovides many efficient operations to achieve numerical integration, optimization, statistics, signal processing, and the most important image processing function for us. Next, this section will cover SciPy a number of useful modules. SciPyis an open source toolkit that can be downloaded from http://scipy.org/Download.

1.4.1 Image Blur

The Gaussian Blur of an image is a very classic example of a convolution of images. In essence, image Blur is the convolution operation of (grayscale) image I and a Gaussian core:

I σ = I*Gσ

where * denotes convolution operation; is the Ivigos core with the standard deviation of σ , defined as:

Gaussian blur is often part of other image processing operations, such as interpolation operations, point-of-interest calculations, and many other applications.

SciPyThere are modules for filtering operations scipy.ndimage.filters . The module calculates the convolution using a fast one-dimensional separation method. You can use it as follows:

from PIL import Imagefrom numpy import *from scipy.ndimage import filtersim = array(Image.open(‘empire.jpg‘).convert(‘L‘))im2 = filters.gaussian_filter(im,5)

guassian_filter()the last parameter of the above function represents a standard deviation.

Figure 1-9 shows the extent to which an image is blurred as σ increases. The larger the σ , the more image details are lost after processing. If you plan to blur a color image, simply gaussian blur for each color channel:

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)

In the above script, it is not always necessary to convert the image to the uint8 format, which simply represents the pixel value in eight bits. We can also use:

im2 = array(im2,‘uint8‘)

To complete the conversion.

For more content on this module and a selection of different parameters, see the SciPy section in the documentation on Http://docs.scipy.org/doc/scipy/reference/ndimage.html scipy.ndimage .

Figure 1-9: Gaussian scipy.ndimage.filters Blur using the module: (a) raw grayscale image, (b) Gaussian filter using σ=2, (c) Gaussian filter using σ=5, and (d) Gaussian filter using σ=10

1.4.2 Image derivative

As you can see throughout this book, the changes in the intensity of images in many applications are very important information. The intensity of the change can be used in grayscale image I(for color images, usually for each color channel to calculate the derivative) x and y side Wizard number Ix and I y to describe.

The gradient vector for the image is ∇i = [ix, iy]T. The gradient has two important properties, one is the size of the gradient :

It describes the strength of the change in the intensity of the image, one is the angle of the gradient :

Alpha=arctan2 (iy, ix)

Describes the direction in which the intensity of the image is most varied at each point (pixel). The NumPy function in the arctan2() -π returns the signed angle in radians, and the range of the angles varies from ... Pi.

We can calculate the derivative of the image by means of a discrete approximation. Most of the derivative of the image can be easily achieved by convolution:

I x=I*Dx and iy=i*Dy

For Dx and Dy, the Prewitt filter is usually selected:

And

or Sobel filter:

And

These derivative filters can be scipy.ndimage.filters implemented simply by using the standard convolution operation of the module, for example:

from PIL import Imagefrom numpy import *from scipy.ndimage import filtersim = array(Image.open(‘empire.jpg‘).convert(‘L‘))# Sobel 导数滤波器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 uses Sobel filters to calculate the directional derivative of x and y , as well as the gradient size. sobel()the second parameter of the function indicates the number of x or y -party wizards selected, and the third parameter holds the output variable. Figure 1-10 shows the derivative image computed with the Sobel filter. In two derivative images, the positive derivative is displayed as a bright pixel, and the negative derivative is shown as a dark pixel. The gray area indicates that the value of the derivative is close to zero.

Figure 1-10: The derivative image is computed using the Sobel derivative filter: (a) The original grayscale image, (b) thex derivative image; (c) They derivative image; (d) A gradient-sized image

The method of calculating the derivative of the image has some drawbacks: in this method, the scale of the filter needs to change with the change of the image resolution. To be more robust in terms of image noise and to calculate derivatives at any scale, we can use a Gaussian derivative filter:

I x=I*gσx and iy=i*gσy

Wherein,gσx and gσy represent in the x and y direction of the derivative, is the standard deviation of σ Gaussian function.

The function we used to blur previously filters.gaussian_filter() can accept additional parameters to calculate the Gaussian derivative. This can be handled simply as follows:

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 of the function specifies which type of derivative is computed for each direction, and the second parameter is the standard deviation used. You can view the documentation for more information. Figure 1-11 shows the derivative image and the gradient size at different scales. You can compare the image with the same scale blur in Figure 1-9.

Figure 1-11: Calculation of the derivative of the image using the Gaussian derivative:x derivative image (top),y derivative image (middle), and gradient size image (a) for the original grayscale image, (b) for the image processed by the Gaussian derivative filter using σ=2 , (c) for images treated with a Gaussian derivative filter using σ=5, (d) for images treated with a Gaussian derivative filter using σ=10

1.4.3 Morphology: Object Count

morphology (or mathematical morphology ) is the basic framework and set of image processing methods for measuring and analyzing basic shapes. Morphology is typically used to process two-value images, but it can also be used in grayscale images. A two-value image refers to an image that can fetch only two values per pixel, usually 0 and 1. A binary image is usually the result of a thresholding of an image when calculating the number of objects, or when measuring their size. You can get a general understanding of morphology and how images are processed from http://en.wikipedia.org/wiki/Mathematical_morphology.

scipy.ndimageIn the morphology module can be used to achieve morphological operation. You can use scipy.ndimage the measurements modules in to implement the count and measure function of the two value image. Here's a simple example of how to use them.

Considering the two-value image in Figure 1-12a3, the number of objects in the image can be calculated using the following script:

3 This image is actually the result of a "split" image. If you want to know how the image was created, you can view section 9.3.

from scipy.ndimage import measurements,morphology# 载入图像,然后使用阈值化操作,以保证处理的图像为二值图像im = array(Image.open(‘houses.png‘).convert(‘L‘))im = 1*(im<</span>128)labels, nbr_objects = measurements.label(im)print "Number of objects:", nbr_objects

The above script first loads the image and ensures that the image is a two-value image by means of a thresholding. By multiplying by 1, the script converts a Boolean array into a binary representation. We then use label() functions to find individual objects and assign integer labels to pixels according to which object they belong. Figure 1-12b is labels an image of an array. The grayscale value of the image represents the label of the object. As you can see, there are some small connections between some objects. For binary open operations, we can remove them:

# 形态学开操作更好地分离各个对象im_open = morphology.binary_opening(im,ones((9,5)),iterations=2)labels_open, nbr_objects_open = measurements.label(im_open)print "Number of objects:", nbr_objects_open

binary_opening()The second parameter of the function specifies an array structure element . The array represents which neighboring pixels are used when a pixel is centered. In this case, we use 9 pixels in the y direction (4 pixels above, the pixel itself, the 4 pixels below), and 5 pixels in the x direction. You can specify any array as a structural element, and the non-0 elements in the array determine which neighboring pixels to use. Parameter iterations determines the number of times the operation is performed. You can try using different iterations to iterations see how the number of objects changes. You can see the image that has been opened and the corresponding label image in Figure 1-12c and figure 1-12d. As you might imagine, the binary_closing() function implements the opposite operation. Let's leave this function and the morphology use of other functions in the and measurements module as exercises. You can scipy.ndimage learn more about these functions from the module document Http://docs.scipy.org/doc/scipy/reference/ndimage.html.

Figure 1-12: Morphological examples. Use the two value open operation to separate the objects, and then calculate the number of objects: (a) The original binary image, (b) a label image corresponding to the original image, where the gray value represents the object's label, (c) for the use of a two value image after operation, and (d) a label image of the image after opening the operation

1.4.4 Some useful SciPy modules

SciPyContains some useful modules for input and output. Two of these modules are described below: io and misc .

  1. Read and write. mat files

    If you have some data, or download it online to some interesting datasets stored in the Matlab. Mat file format, you can use the scipy.io module to read.

    data = scipy.io.loadmat(‘test.mat‘)

    In the above code, the data object contains a dictionary, and the keys in the dictionary correspond to the variable names saved in the original. Mat file. Because these variables are in array format, they can be easily saved to a. mat file. You just need to create a dictionary (which contains all the variables you want to save) and then use the savemat() function:

    data = {}data[‘x‘] = xscipy.io.savemat(‘test.mat‘,data)

    Because the script above holds the array x, the name of the variable is still xwhen it is read into Matlab. For scipy.io more information on modules, see the online documentation http://docs.scipy.org/doc/scipy/reference/io.html.

  2. Saving an array as an image

    Because we need to manipulate the image and we need to use an array object to do the operation, it is very useful to save the array directly as an image file 4. Many of the images in this book are created in this way.

    imsave()The function can be scipy.misc loaded from the module. To save the array im to a file, you can use the following command:

    from scipy.misc import imsaveimsave(‘test.jpg‘,im)

    scipy.miscThe module also contains the famous Lena test image:

    lena = scipy.misc.lena()

    The script returns an array of grayscale images for the 512x512.

4 All Pylab graphs can be saved as multiple image formats by tapping the Save button in the Image window.

1.5 Advanced Example: Image denoising

We end this chapter with a very practical example-the denoising of images. Image Denoising is the process of preserving image details and structure as much as possible while removing image noise. We use the ROF (Rudin-osher-fatemi) denoising model here. The model first appeared in the literature [28]. Image Denoising is important for a wide range of applications, ranging in size to make your vacation photos look prettier and larger to improve the quality of satellite imagery. The ROF model has good properties: it makes the processed image smoother, while preserving the image edge and structure information.

The mathematical basis and processing skills of the ROF model are very advanced, not within the scope of this book. Before describing how to implement the ROF solver based on the algorithm proposed by Chambolle [5], this book begins with a brief introduction to the ROF model.

The total Variance of a (grayscale) image I is defined as the sum of the gradient norm (VARIATION,TV). In the case of continuous representations, the total variance is expressed as:

(1.1)

In the case of discrete representations, the total variance is expressed as:

Where the above equation is taken on all image coordinates x=[x, y].

In the ROF model proposed by Chambolle, the objective function is to find the image Uafter noise reduction, so that the following type is minimized:

Where Norm | | I-U| | is the measurement of the difference between the image U and the original image I after denoising. That is, in essence, the model makes the image pixel value "flat", but on the edge of the image area, it allows the image pixel value "jump" to change after denoising.

According to the algorithm in paper [5], we can implement the ROF model denoising according to the following code:

 from NumPy import *def denoise (im,u_init,tolerance=0.1,tau=0.125,tv_weight=100): "" "uses a. Chambolle (2005) The calculation steps in the formula (11) Implement the Rudin-osher-fatemi (ROF) denoising model input: Input image with noise (grayscale image), initial value of U, TV regular term weight, step length, closing condition Output: Image after de-noising and texture removal, texture residue "" "M,n = im.shape # Noise image size # Initialize U = U_INITPX = im # dual domain x component py = im # Dual domain y component error = 1while (Error ; Tolerance): Uold = U # The gradient of the original variable gradux = Roll (U,-1,axis=1)-U # variable U-gradient x component Graduy = Roll (U,-1,axis=0)-U # variable U-Gradient y component # Update dual Variable pxnew = Px + (tau/tv_weight) *gradux pynew = Py + (tau/tv_weight) *graduy normnew = Maximum (1,sqrt (Pxnew**2+pyne w**2) Px = pxnew/normnew # Update x component (Dual) Py = pynew/normnew # update y component (dual) # update original variable rxpx = Roll (Px,1,axis=1) # Right X component X Axis Shift rypy = Roll (py,1,axis=0) # Shift y component right Y-axis DIVP = (PX-RXPX) + (py-rypy) # Divergence of the dual domain U = im + TV_WEIGHT*DIVP # update original variable # more New error = Linalg.norm (u-uold)/sqrt (n*m), return u,im-u # image and texture remnants after de-noising 

In this example, we use a roll() function. As the name implies, on an axis, it loops through the element values in the "scrolling" array. This function makes it very easy to calculate the difference of neighborhood elements, such as the derivative here. We also used the linalg.norm() function, which can measure the difference between two arrays (in this case, the image matrix U and uold). We will denoise() save this function in the rof.py file.

The following example uses a synthesized noise image to illustrate how to use the function:

from numpy import *from numpy import randomfrom scipy.ndimage import filtersimport rof# 使用噪声创建合成图像im = zeros((500,500))im[100:400,100:400] = 128im[200:300,200:300] = 255im = im + 30*random.standard_normal((500,500))U,T = rof.denoise(im,im)G = filters.gaussian_filter(im,10)# 保存生成结果from scipy.misc import imsaveimsave(‘synth_rof.pdf‘,U)imsave(‘synth_gaussian.pdf‘,G)

The original image and image denoising results are shown in 1-13. As you can see, the ROF algorithm's denoising image preserves the image's edge information well.

Figure 1-13: Using the ROF model to de-noising the synthesized Image: (a) for the original noise image, (b) for the Gaussian blurred image (σ=10), and (c) for the image after the ROF model denoising

Here's a look at the effect of using the ROF model to de-noising in the actual image:

from PIL import Imagefrom pylab import *import rofim = array(Image.open(‘empire.jpg‘).convert(‘L‘))U,T = rof.denoise(im,im)figure()gray()imshow(U)axis(‘equal‘)axis(‘off‘)show()

The image 1-14c after ROF denoising is shown. For ease of comparison, the image is also shown in the figure. It can be seen that the ROF image retains the structure information of edges and images, and blurs the "noise".

Figure 1-14: Using the ROF model to de-noising the grayscale Image: (a) for the original noise image, (b) for the Gaussian blurred image (σ=5), and (c) for the image after the ROF model denoising

Python Image processing library (2)

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.