Python computer vision and python vision

Source: Internet
Author: User

Python computer vision and python vision

This article comes from the book python computer visual programming.
I am a beginner. If you find any errors in this article, please leave a message to me. Thank you.

PIL Module

The entire process of the PIL module isPython Imaging LibraryIs a free Image Processing Module in python.

Open Image

The PIL module is commonly used in its Image class. To open an Image, you must first import the Image class.
from PIL import Image,
Then callImageOfopenMethod.
For example

From PIL import Imageimage = Image. open ("smallpi.jpg") # Return an Image object print (image) # result <PIL. required imageplugin. required imagefile image mode = RGB size = 800x450 at 0x4731348>
 
Image Storage and format conversion

Image Storage Uses the Image objectsave()Method. The input parameter is the name of the image file to be saved.
When different extensions are passed in, the image format is automatically converted according to the extension.
For example

From PIL import Imageimage = Image. open ("smallpi.jpg") # open the jpg image file Image. save ("smallpi.png") # save the image and convert it to png.
 
 
Convert to grayscale image

After obtaining the Image object, call itsconvert()Method, input parameters"L"That is, the grayscale image object of the image can be returned.

From PIL import Imageimage = Image. open ("smallpi.jpg") image_gray = image. convert ("L") # convert to grayscale image print (image_gray) # result <PIL. image. image image mode = L size = 800x450 at 0x46AD648>

 

Image Object and Image matrix convert Image object to Image Matrix

To convert an Image object to an Image matrix, you only need to use the Image objectnumpy.array()Parameter.

Import numpy as npfrom PIL import Imageimage = Image. open ("smallpi.jpg") image_array = np. array (image) print (image_array) # result [[[177 177 177 177] [177 177 176] [176 176]..., # omitted here ..., [232 232 232] [232 232 232] [232 232 232]

 

 

Convert an Image matrix to an Image object

The Image matrix is converted into an Image object throughImageModulefromarray()Method.

Import numpy as npfrom PIL import Imageimage = Image. open ("smallpi.jpg") image_array = np. array (image. convert ("L") image_array = 255-image_array # Image matrix processing, reversed Grayscale Images # reversed: Black to white, white to black image2 = Image. fromarray (image_array) print (image2) # result <PIL. image. image image mode = RGB size = 800x450 at 0x4753748>

 

 
Image Display

The matplotlib module is required for image display.
First, you need to go to matplotlib. pyplot.
import matplotlib.pyplot as plt
Then, callimshow()Method to pass in the Image object.

From PIL import Imageimport matplotlib. pyplot as pltimage = Image. open ("smallpi.jpg") plt. imshow (image) # Draw the image imageplt. show () # Call the show () method, otherwise the image will only be displayed in the memory

  

Image Display result (with coordinate axis)

To remove the coordinate axis, you only need to call the pyplotaxis()Method, input"off"Parameters

From PIL import Imageimport matplotlib. pyplot as pltimage = Image. open ("smallpi.jpg") plt. imshow (image) # Draw the image imageplt. axis ("off") # Remove the axis plt. show () # Call the show () method, otherwise the image will only be displayed in the memory 

Image Display result (without coordinate axes)

To display grayscale images, import the cm module of matplotlib.
import matplotlib.cm as cm
Then usepyplot.show()Enter the keyword Parametercmap=cm.gray.

From PIL import Image import matplotlib. pyplot as pltimport matplotlib. cm as cm image = Image. open ("smallpi.jpg") # open the image image_gray = image. convert ("L") # convert to a grayscale image plt. subplot (2, 1, 1) plt. imshow (image_gray) # The keyword parameter cmap = cm is not input. grayplt. axis ("off") # Remove the axis plt. subplot (2, 1, 2) plt. imshow (image_gray, cmap = cm. gray) # specify cmap = cm. grayplt. axis ("off") # Remove the axis plt. show () # display images 

Display result
Upper: No cmap is specified. Lower: Specify cmap = cm. gray

Create a thumbnail

You can usethumbnail()To specify the size of the thumbnail, as shown in figurethumbnail((128,128)).
For example

from PIL import Imageimage = Image.open("smallpi.jpg")image_thumbnail = image.thumbnail((128,128))image.save("thumbnail.jpg") 

Result:

Copy and paste Area

Copying a region refers to capturing a part of an Image and using this part as a new Image object.
The method for copying a region iscrop()The parameter is a tuple containing four elements. It is used to specify the upper left corner and the lower right corner of the truncated area.

From PIL import Imageimage = Image. open ("smallpi.jpg") # open the image box = (300,100,500,300) # capture the region image_crop = image. crop (box) # copy the image in the specified screenshot area image_crop.save ("image_crop.jpg") # Save

 

The saved screenshot region image is

 

The pasting area refers to placing another image in the specified image. The method is as follows:paste(). This method has two parameters: the first parameter is the image to be pasted in, and the second parameter is the pasting area.

From PIL import Imageimage = Image. open ("smallpi.jpg") box = (300,100,500,300) # extract part of image_crop = image. crop (box) # in order to see the paste effect, the screenshot part is now converted to 180 degrees image_crop = image_crop.transpose (Image. ROTATE_180) # convert to an image of 180 degrees. paste (image_crop, box) # paste the image after 180 degrees to the original image. save ("image_paste.jpg ")
Paste the original image

 

Resize and rotate Images

The dimension adjustment method is as follows:resize(), The parameter is a tuple and the adjusted size is specified, as shown in figureResize (128,128 )).
Image rotation method:
Rotate (), The parameter is the rotation angle (numeric value, unit is degrees), counterclockwise, suchRotate (45 )'

From PIL import Imageimage = Image. open ("smallpi.jpg") image_resize = image. resize (200,200) # resize image_rotate = image. rotate (45) # image rotation # image. transpose () can also rotate the Image, but can only rotate an integer multiple of 90 degrees # The parameter is Image. ROTATE_90 Rotate 90 degrees #180 degrees, 270 degrees, and so on image_resize.save ("image_resize.jpg") image_rotate.save ("image_rotate.jpg ")

Size adjusted to 200*200

 

Rotate the image 45 degrees counterclockwise

 

Image Histogram

The image histogram is used to measure the distribution of pixel values in an image, that is, the number of occurrences of different pixel values. The method is called.matplotlib.pyplotOfhistMethod, parameters are passed into the image pixel sequence and the number of statistical intervals.

From PIL import Imageimport matplotlib. pyplot as pltimport matplotlib. cm as cm # Open the image and convert it into a grayscale Image = image. open ("smallpi.jpg "). convert ("L") image_array = np. array (image) plt. subplot (2, 1, 1) plt. imshow (image, cmap = cm. gray) plt. axis ("off") plt. subplot (2, 1, 2) plt. hist (image_array.flatten (), 256) # flatten converts a matrix into a one-dimensional Sequence plt. show ()

Result:

Grayscale conversion

For a grayscale image, each pixel is represented by a value ranging from 0 to 255. 0 indicates black, and the darker the pixel is, 255 indicates white, and the closer the pixel is.
Grayscale conversion is to convert a grayscale value from one value to another through a specific function.
Three gray transformations are listed here.

1. [reversed] The converted gray value = 255 −original gray value

2. [converted to 255.0-100] The transformed gray scale value = the original gray scale value 100 ÷ +

3. [pixel square] The transformed gray value = 255.0 Gb/s (original gray value 255.0) 2

From PIL import Imageimport matplotlib. pyplot as pltimport matplotlib. cm as cmimage = Image. open ("smallpi.jpg "). convert ("L") image_array = np. array (image) x = np. arange (255) # reverse plt. subplot (3, 2, 1) plt. plot (x, 255-x) # plot the transform function image plt. subplot (3, 2, 2) plt. imshow (Image. fromarray (255-image_array), cmap = cm. gray) plt. axis ("off") # convert to 100-200plt.subplot (3,2, 3) plt. plot (x, (x/255.0) * 100 + 100) # plot the transform function image plt. subplot (3, 2, 4) plt. imshow (Image. fromarray (image_array/255.0) * 100 + 100), cmap = cm. gray) plt. axis ("off") # pixel squared plt. subplot (3, 2, 5) plt. plot (x, 255 * (x/255.0) ** 2) # plot the transform function image plt. subplot (3, 2, 6) plt. imshow (Image. fromarray (255 * (image_array/255.0) ** 2), cmap = cm. gray) plt. axis ("off") plt. show ()

  

'The result is as follows (the conversion function is on the left and the image transformation result is on the right)

Histogram equalization

 

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.