Use python for image processing

Source: Internet
Author: User
Tags image filter image processing library
Use python for image processing

Recently, I am working on verification code recognition to learn some new skills. Because I am a beginner, I don't know much about image processing. To do my best, I must first use the image processing tool. Since it is just an experiment, it would be better to use python for prototype development. In python, the commonly used image processing library is Pil (Python Image Library), and the current version is 1.1.6, which is very convenient to use. You can download and learn in http://www.pythonware.com/products/pil/index.htm. Here, I will mainly introduce some Pil functions that may be used for image recognition, such as enhancement and filtering. Finally, the advantages and disadvantages of using python for image processing and recognition are given. Basic Image Processing Before using Pil, You need to import the image module: Import image Then you can use image.open('xx.bmp ') to open a bitmap file for processing. You don't have to worry about the format or understand the format to open the file. Whatever the format, you just need to throw the file name to image. Open. The so-called BMP, JPG, PNG, GIF ......, No fewer than one. IMG = image.open('origin.png ') # Obtain the IMG image of an image instance object. 1 Source image In image processing, the most basic thing is the color space conversion. In general, our images are all RGB color spaces, but in image recognition, we may need to convert the images to different color spaces, such as grayscale images and binary images. PIL also provides excellent support in this respect. We can: new_img = IMG. convert ('l') converts IMG to a 256-level grayscale image. Convert () is a method of image instance objects. It accepts a mode parameter to specify a color mode, the value of mode can be 1 (1-bit pixels, black and white, stored with one pixel per byte) · L (8-bit pixels, black and white) · P (8-bit pixels, mapped to any other mode using a color palette) · RGB (3x8-bit pixels, true color) · rgba (4x8-bit pixels, true color with transparency mask) · C Myk (4x8-bit pixels, color separation) · YCbCr (3x8-bit pixels, color video format) · I (32-bit signed integer pixels) · F (32-bit floating point pixels, rich enough? In fact, Pil also supports the following rare color modes: La (L with alpha), rgbx (true color with padding) and rgba (true color with premultiplied alpha ). The following figure shows the converted image when the mode is '1', 'l', or 'P ': 2 Mode = '1' Figure 3 Mode = 'l' Figure 4 Mode = 'P' The convert () function also accepts another implicit parameter matrix. The conversion matrix is a length of 4 or 16 tuple. The following is an example of converting an RGB space to a cie xyz space: Rgb2xyz = ( 0.412453, 0.357580, 0.180423, 0, 0.212671, 0.715160, 0.072169, 0, 0.019334, 0.119193, 0.950227, 0) Out = Im. Convert ("RGB", rgb2xyz) In addition to the complete color space conversion capabilities, Pil also provides functions such as resize () and rotate () to obtain the ability to change the size, rotate images, and other geometric transformations. In terms of image recognition, the image example provides a histogram () method to calculate the histogram, which is very convenient and practical. Image Enhancement Image enhancement is usually used for pre-processing before image recognition. Appropriate image enhancement can make the recognition process get twice the result with half the effort. PIL provides a module named imageenhance in this regard and provides several common image enhancement solutions: Import imageenhanceenhancer = imageenhance. Sharpness (image) for I in range (8 ): Factor = I/4.0 Enhancer. Enhance (factor). Show ("sharpness % F" % factor) The above Code This is a typical example of using the imageenhance module. Sharpness is a class of the imageenhance module used to sharpen images. This module mainly includes the following classes: color, brightness, contrast, and sharpness. They all have a common interface. Enhance (factor), which accepts a floating-point parameter factor to indicate the enhancement ratio. Next, let's take a look at the effects of these four types under different factors. 5 Use color for color enhancement. The value of factor is [0, 4], and the step is 0.5. 6 Increase the brightness with birghtness. The value of factor is [0, 4], and the step is 0.5. 7 Use contrast to enhance the contrast. The value of factor is [0, 4], and the image is displayed in step 0.5. 8 Sharpen the image with sharpness. The value of factor is [0, 4], and the step is 0.5. Image Filter PIL The filter support is very complete, in addition to common fuzzy, embossed, contour, edge enhancement and smooth, as well as median filter, modefilter, etc, it's easy to make a Photoshop by yourself. These filters are placed in the imagefilter module. imagefilter consists of two parts: one is the built-in filter, such as blur and detail, and the other is the filter function, you can specify different parameters to get different results. Example: Import imagefilterim1 = IM. filter (imagefilter. blur) IM2 = IM. filter (imagefilter. minfilter (3) IM3 = IM. filter (imagefilter. minfilter () # Same as minfilter (3) you can see that the imagefilter module is very easy to use. Every filter can be called with only one line of code, and the development efficiency is very high. Figure 9 Use a blur Diagram 10 Use a contour graph 11 Use the detail Diagram 12 Use emboss Diagram 13 Use the edge_enhance Graph 14 Use the edge_enhance_more Graph 15 Use find_edges Graph 16 Use a sharpen Graph 17 Use the smooth Graph 18 Use smooth_more The above are several built-in filters. In addition, imagefilter also provides some filter functions. Let's take a look at these filters that can change behavior through parameters: Figure 19 Use kernel (), parameter: size = (3, 3), kernel = (0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5) 20 Use maxfilter, default parameter diagram 21 Use minfilter, default parameter diagram 22 Use medianfilter, default parameter diagram 23 Use modefilter. The parameter size is shown in figure 3. 24 Use rankfilter, parameter size = 3, Rank = 3 Summary So far, the introduction to Pil has come to an end. In general, Pil has built powerful support for image processing and recognition, from various enhancements Algorithm To filter, there is no doubt about the feasibility of using python. The only disadvantage of python is that the execution time is too slow, especially when some algorithms with a large amount of computing are implemented, it requires a lot of patience. I used the Hough transform to find the straight line in the image, the implementation of pure Python takes several seconds to process a 340*100 image (P4 3.0g + 1g memory ). However, Pil does not need to focus on the image format, built-in image enhancement algorithms, and filter algorithms. These advantages make Python suitable for prototyping and experiments. In these two aspects, python is more convenient than Matlab. For commercial image recognition product development, you can consider the open-source C ++ library Gil from Adobe that has been boost accepted, which can take into account both execution performance and development efficiency.

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.