The function opencv

Reads the image: cv2.imread (), the first argument: not a file path, the second argument: tells the function how you want to read the picture.

  • cv2.IMREAD_COLOR: reading a color image. Transparency of the image will be ignored.
  • cv2.IMREAD_GRAYSCALE: a read image in grayscale

 Display image: cv2.imshow ()

  • Window will automatically adjust the image size, the first parameter is the name of the window, followed by frankincense

Keyboard bindings function: cv2.waitKey ()

  • Time scale in milliseconds. Waiting for a specific function of a few milliseconds to see if there is keyboard input. A specific few milliseconds, if any key is pressed, this function will return the value ASII code key.
  • The parameters of this function is 0, will wait indefinitely for keyboard input.

Destroy the window: cv2.destroyAllWindows ()

  • You can easily delete any window we created. If you want to delete a specific window can be used cv2.destoryWindow () , enter the name of the window you want to delete in parentheses.

Take Care:

A special case is that you can create a window, and then
re-load the image. In this case, you can decide whether you can adjust the window
size. Function is used to cv2.namedWindow (). Function initial setting
tab is cv2.WINDOW_AUTOSIZE. But if you put the label into
cv2.WINDOW_NORMAL, you can resize the window up. When the image is too large dimensions,
or when you want to add a track bar, resize the window would be useful

import numpy as np
8 import cv2
9
10 cv2.namedWindow('image', cv2.WINDOW_NORMAL)
11 cv2.imshow('image',img)
12 cv2.waitKey(0)
13 cv2.destroyAllWindows()

 

Save the image: cv2.imwrite ( 'test.jpg', img)

  • Using the function cv2.imwrite () to save a picture. First, the need for a file name only after that you want to save the image.

cv2.COLOR_BGR2GRAY converted to grayscale image format BGR

Capturing video with a camera: cv2.VideoCapture ()

  • Its parameters may be an index number of the device, or a video file. Equipment index is a good camera to use in the development of. Usually laptops have built-in camera, so the parameter is 0. Or other may be selected by another camera is set to 1.
Gets camera / video of various properties - Common Functions
  • cv2.VideoCapture.get (propId) access to certain video features, wherein propId is a number from 0 to 18, each number denotes a video attribute (Property Identifier).
  • retval = cv2.VideoCapture.set(propId,value)
  • Some of these values ​​can be cap.set (propId, value) to be modified, value is modified value.
  • For example: to check the width and height of the frame by cap.get (3) and cap.get (4), the default value is 640x480. Now modified to 320x240, using ret = cap.set (3, 320) and ret = cap.set (4, 240).
  • retval, image = cv2.VideoCapture.read ([, image]) fetch, decode and returns the next video frame. Returns the value of true indicate grab success. This function is a combination of Grab () and retrieve (), which is the most convenient way. If there is no frame, the function returns false, and outputs an empty image.
  • retval, image = cv2.VideoCapture.retrieve ([, image [, flag]]), and returns the decoded video frame grab
  • retval = cv2.VideoCapture.grab () to fetch the next frame from a video camera or a file. true to grab success. This function is mainly used for multi-camera.
  • cv2.VideoCapture.release () close the file or video camera equipment.
= cv2.VideoCapture.get retval (ptopId) # to open the file or video camera for video capture device. 

propId common values are as follows: 
"" " 
cv2.CAP_PROP_POS_MSEC: the current location of the video file (MS) 
cv2.CAP_PROP_POS_FRAMES: zero-indexed frame, frame position. 
cv2.CAP_PROP_POS_AVI_RATIO: the relative position of the video file (the start represents 0, 1 represents end) 
cv2.CAP_PROP_FRAME_WIDTH: a frame width of the video stream. 
cv2.CAP_PROP_FRAME_HEIGHT: a frame height of the video stream. 
cv2.CAP_PROP_FPS: frame rate 
cv2.CAP_PROP_FOURCC: codec four-character codes 
frames of a video file: cv2.CAP_PROP_FRAME_COUNT 
cv2.CAP_PROP_FORMAT : retrieve objects returned Mat format (). 
cv2.CAP_PROP_MODE: rear special value, indicating the current capturing mode 

cv2.CAP_PROP_BRIGHTNESS: brightness of an image, the camera is only suitable for supporting 
cv2.CAP_PROP_CONTRAST: image contrast, apply only to the camera 
cv2.CAP_PROP_SATURATION: image saturation, only for the camera
cv2.CAP_PROP_HUE: image tone, apply only to the camera
cv2.CAP_PROP_GAIN: image gain, only for supported cameras 
cv2.CAP_PROP_EXPOSURE: exposure, only for camera support 
cv2.CAP_PROP_CONVERT_RGB: Boolean flag indicating whether the image should be converted to RGB. 
"" "

Save Video

VideoWriter for saving videos.

  • <VideoWriter object> = cv.VideoWriter(filename, fourcc, fps, frameSize[, isColor] )

  Function parameters:

  • filename: save filename
  • fourcc: specifying a video encoder 4 bytecode
  • fps: frame rate
  • frameSize: frame size
  • retval = cv2.VideoWriter_fourcc (c1, c2, c3, c4) connected to the string 4 fourcc code.
    cv.VideoWriter.write (image) will save the image as a frame of video files.
    isColor: If true, the video is color, grayscale or video, the default is true

Guess you like

Origin www.cnblogs.com/leoych/p/12060317.html