Video image processing

First, the read image data

1. The read image using PIL

Python Imaging Library library contains many, which is commonly used Image, reads an image by using the open method which is used as follows:

import Image # import library file = 'cat.jpg' # define the picture Address img = Image.open (file, mode = "r") # read the file contents img.show () # display image content

The key is to open, which comprises two parameters:

  • file: file object name can be a file name, image files can also be a string.
  • mode: open mode, the default mode is only r, otherwise it will error; when the file is an image string, the system will call rb mode read.

After passing through open reading will return a file object image, all subsequent image processing is based on the object. After the above code is executed by img.show () system calls the default image browser to view open an image for viewing.

▲ calls img.show () display image

This object contains a number of methods can be used to print the output file attributes, such as size, format, color mode, and the like.

print ( 'img format:', img.format) # print image format print ( 'img size:', img.size) # print image size print ( 'img mode:', img.mode) # image color mode printing

After the above code is returned execution results as follows:

('img format: ', 'JPEG')('img size: ', (435, 361))('img mode: ', 'RGB')

Wherein the type of the image format is the image itself, such as jpg, gif, png, etc.; the size of the image is an image resolution, the example dimensions are 435 × 361 (in pixels); mode image refers to the color mode, the example The image is RGB mode.

Second, the read image using OpenCV

OpenCV read and display image has mainly two methods, first is to use cv library, cv2 second is to use the library.

The first: reading an image using cv

Import cv2.cv AS CV # import library 
File = ' cat.jpg '  # define the address of the picture 
IMG = cv.LoadImage (File) # load image 
cv.NamedWindow ( ' a_window ' , cv.CV_WINDOW_AUTOSIZE) # Create an adaptive window with to present an image 
cv.ShowImage ( ' a_window ' , IMG) # display image 
cv.WaitKey (0) # display parameters used with
View Code

The second: reading an image using cv2

import cv2 # import library file = 'cat.jpg' # define picture address img = cv2.imread (file) # read image cv2.imshow ( 'image', img) # display image cv2.waitKey (0) # display parameters used in conjunction with

By calling the PIL is the default image display tools, and OpenCV is displayed in an image by image function itself created.

Further, both methods have a waitKey () method, the role of which is to bind a function keyboard, wherein the parameter indicates the number of milliseconds to wait. After performing the method, the program will wait for a specific number of milliseconds, to see if the keyboard input, and then returns the corresponding ASCII value. If its argument is 0, then wait indefinitely until the keyboard input.

I usually read the image using the second method, because the method is more simple. Wherein imread method details are as follows:

grammar

cv2.imread(filename[, flags])

description

Reads the image content, if you can not read the image information is returned empty, supported image formats, including almost daily scenes in all formats, including:

  • Windows bitmaps file:. * Bmp, * dib.
  • JPEG file:... * Jpeg, * jpg, * jpe
  • JPEG 2000 file:. * Jp2
  • PNG file:. * Png
  • WebP file:. * Webp
  • Moving image format:.... * Pbm, * pgm, * ppm * .pxm, * pnm
  • Sun rasters file:. * Sr, * ras.
  • TIFF files:. * Tiff, * tif.
  • OpenEXR file:. * Exr
  • Radiance HDR file:. * Hdr, * pic.

parameter

  • Required filename, strings, images address.
  • Alternatively flags, int-type or corresponding character string, color reading mode. If the flag> 0 or cv2.IMREAD_COLOR, having read the R / G / B three-channel color image; flag = 0 or if cv2.IMREAD_GRAYSCALE, the read gradation image; if the flag

return

Image content, NULL if the image can not be read is returned.

Tip: In addition to using OpenCV image display method comes outside, and often matplotlib with OpenCV image display, this scenario is more common. It can be borrowed in combination with powerful Matplotlib image display capability of the image contrast and reference images, and output different modes.

Third, the video data is read

The most commonly used Python to read the video library also Opencv. In this paper, called Megamind.avi video as an example, the following code sample period of the read video content:

Import CV2 # import library 
CAP = cv2.VideoCapture ( " tree.avi " ) # obtained video object 
Status = cap.isOpened () # determines whether the file opened correctly known 
IF Status: # if properly opened, the attribute information of the video is obtained 
frame_width cap.get = (. 3) # acquire frame width 
FRAME_HEIGHT cap.get = (. 4) # acquire frame height 
frame_count cap.get = (. 7) # obtain the total number of frames 
frame_fps cap.get = (. 5) # obtain the frame rate 
Print ( ' Frame width: ' , frame_width) # printout 
Print ( 'height Frame: ' , FRAME_HEIGHT) # printout 
Print ( ' Frame COUNT: ' , frame_count) # printout 
Print ( ' Frame FPS: ' , frame_fps) # printout 
Success, Frame = cap.read () # reads the video of a 
the while Success: # If the read state is True 
cv2.imshow ( ' vidoe frame ' , frame) # show frame image 
Success, frame = cap.read () # Get the next frame 
k = cv2.waitKey (1000 / int (frame_fps)) #Each frame playback certain delay, while waiting for an instruction input 
IF K == 27: # If the key is detected during the waiting period the ESC 
BREAK  # exit the loop cv2.destroyAllWindows () # close all windows 
cap.release () # released video file object
View Code

The code is divided into four parts, separated by an empty line.

The first portion 3 is a front row, a pilot storage, and then reads the video file and obtains the video object, and then obtain a video reading state. The key is VideoCapture, used to read the image.

grammar

cv2.VideoCapture(VideoCapture ID|filename|apiPreference)

description

Reading device or video file, and create a video object instance

parameter

Required, VideoCapture ID | filename

VideoCapture ID: int type, ID assignment system device objects, ID, default device object is zero.

Filename:

  • The name of the video files, strings, such as abc.avi. Under the current version only supports avi format.
  • Sequence of images, character string, e.g. img_% 2d.jpg (image sequence including img_00.jpg, img_01.jpg, img_02.jpg, ...)
  • Video URL address, strings, such as protocol: // host: port / script_name script_params | auth?
  • apiPreference: int type, use the API background

Guess you like

Origin www.cnblogs.com/lpapython/p/11101042.html