Python basic image operations
Python basic image operationsUse python for basic image operations and ProcessingPreface:
Most programs in the early computer vision field were written in C/C ++. As computer hardware becomes faster and faster, researchers will consider the efficiency and ease-of-use of coding when considering the implementation of algorithm languages, rather than putting the algorithm execution efficiency first in the early years. This has led more and more researchers to use Python to implement algorithms in recent years.
Today, in the field of computer vision, more and more researchers are using Python for research, so it is necessary to learn about the use of very easy-to-use python in the field of image processing, this blog will introduce how to use several famous image processing libraries in Python to perform basic image operations and processing.
Use PIL for basic image operations
PIL introduction:
PIL (Python Imaging Library Python, image processing class Library) provides common image processing functions and a large number of useful basic image operations, such as scaling, cropping, rotating, and color conversion.
PIL reads and stores images:
Using the PIL function, we can read data from most image format files and then write data to the most common image format files. The most important module in PIL is Image.
The following program uses PIL to read a jpg image and save it as a png file:
#-*-Coding: UTF-8-*-from PIL import Imageimport OS # Open the Image to get a PIL Image object img = Image. open (". /source/test.jpg ") # convert it into a grayscale image img = img. convert ('l') # store the image try: img. save ("test.png") failed t IOError: print "cannot convert"
Test.jpg
Test.png
PIL to generate a thumbnail:
#-*-Coding: UTF-8-*-from PIL import Imageimport OS # Open the Image to get a PIL Image object img = Image. open (". /source/test.jpg ") # create a thumbnail img with the longest side of 128. thumbnail (128,128) # store the image try: img. save ("test.png") failed t IOError: print "cannot convert"
Test.png
PIL Resize and rotate:
#-*-Coding: UTF-8-*-from PIL import Imageimport OS # Open the Image to get a PIL Image object img = Image. open (". /source/test.jpg ") # modify the image size. Parameter: img = img. resize (100,200) # select 45 degrees img = img counterclockwise for the image. rotate (45) # store this image. try: img. save ("test.png") failed t IOError: print "cannot convert"
Test.png
PIL copy and paste the image area:
#-*-Coding: UTF-8-*-#-*-coding: UTF-8-*-from PIL import Imageimport OS # Open the Image to get a PIL Image object img = Image. open (". /source/test.jpg ") # crop the Specified region = img from img. crop (300,300,500,500) # Set the cropping part to 145 degrees region = region counterclockwise. rotate (145) # paste this region to the specified region img. paste (region, (100,100,300,300); # store the image try: img. save ("test.png") failed t IOError: print "cannot convert"
Test.png
In use of tuples, the coordinate origin is in the upper left corner, as shown in Area Division.
Use Matplotlib for basic image operations
Matplotlib introduction:
Matplotlib is a good class library for processing mathematical operations, drawing charts, or drawing points, straight lines, and curves on images. It has more powerful plotting functions than PIL. Matplotlib can draw good bar charts, pie charts, and scatter plots. However, for most computer vision applications, only a few drawing commands are needed. For example, we want to use vertices and lines to express some things, such as interest points, corresponding points, and detected objects.
Use Matplotlib to draw images, points, and lines
#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # Open the Image to get a PIL Image object img = Image. open (". /source/test.jpg ") # Read the image to the array im = array (img) # Draw the image imshow (im) # Some vertices x = [100,100,400,400] y = [200,500,200,500] # plot point plot (x, y, 'r * ') using red star markers *') # plot the Line plot (x [: 2], y [: 2]) connecting the first two points # Add a title to display the title of the drawn image ('plotid: "Test.jpg "') show ()
The show () command first opens the graphical user interface (GUI), and then creates an image window. The graphic user interface will loop through the script and pause until the last image window is closed. In each script, you can only call the show () command once, usually at the end of the script.
You can also run the axis ('off') command to disable the axis display.
Running result
There are many options for controlling the color and style of an image during plotting.
For example:
Plot (x, y) # The default is blue solid line plot (x, y, 'r * ') # Red Star mark plot (x, y, 'go -') # Green Line plot (x, y, 'ks: ') marked with circles # Black dotted line marked with squares
Mark |
Color |
'B' |
Blue |
'G' |
Green |
'R' |
Red |
'C' |
Cyan |
'M' |
Pinhong |
'Y' |
Yellow |
'K' |
Black |
'W' |
White |
Mark |
Line Type |
'-' |
Solid line |
'-' |
Dotted Line |
':' |
Point Line |
Mark |
Shape |
'.' |
Point |
'O' |
Circle |
'S' |
Square |
'*' |
Star |
'+' |
Plus sign |
'X' |
Cross |
Use Matplotlib to draw image outlines
It is very useful to draw the contour of an image (or the contour of other two-dimensional functions. Because the same threshold value must be applied to the pixel values of each coordinate [x, y] to draw a contour, first grayscale the image and then use contour to obtain the contour image.
#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # Read the Image to the array and grayscale im = array (Image. open ('. /source/test.jpg '). convert ('l') # discard color information when displaying gray () # Show contour image contour (im, origin = 'image ') # axis ('equal') # Close axis ('off') show () in the upper left corner of the Origin ()
Running result
Use Matplotlib to draw a Histogram
The histogram of the image is used to characterize the distribution of the pixel values of the image. A certain number of cells (bin) are used to specify the pixel value range. Each cell gets the number of pixels that fall into the inter-cell representation range. (Grayscale) the histogram of an image can be drawn using the hist () function:
The second parameter of the hist () function specifies the number of cells. Note that because hist () only accepts one-dimensional arrays as input, we must flatten the image before creating the image histogram. The flatten () method converts any array into a one-dimensional array based on the row-first principle.
#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # Read the Image to the array and grayscale im = array (Image. open ('. /source/test.jpg '). convert ('l') # histogram image hist (im. flatten (), 128) # show ()
Running result
Use Matplotlib for interactive tagging
The ginput () function in the PyLab library can implement interactive tagging to mark some points or some training data.
#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # Read the Image to the array im = array (Image. open ('. /source/test.jpg ') # display the image imshow (im) print 'Please click 3 points' # obtain and save the click coordinates in [x, y] x = ginput (3) # print 'you clicked: ', xshow ()
The above script first draws an image, and then waits for the user to click three times in the image area of the drawing window. Cheng
[X, y] Of these clicks are automatically saved in the x list.
Running result
you clicked: [(295.22704081632651, 210.72448979591837), (405.43112244897952, 66.846938775510239), (439.1045918367347, 180.11224489795921)]
Conclusion:
This blog introduces some basic python image operations. In addition to the above PIL and Matplotlib, numpy is often used to directly operate image arrays, scipy is used for more complex computing. I will record my learning process and hope it will help you ~