python library (OpenCV simple to use)

Because it requires the use of the system in python OpenCV library, so everyone here is also a process of using it.

Novice can play again in front of their own would be good.

1. Install cv2

General use pip install cv2 can be.

2. Read the picture

Function: img = cv2.imread (filepath, flag)

  -filepath: read image path

  -flag: which mode is read into the picture

    -cv2.IMREAD_COLOR: the default parameters, ignoring the alpha channel

    -cv2.IMREAD_GRAYSCALE: Load a grayscale.

    -cv2.IMREAD_UNCHANGED: loading an image, which comprises the Alpha channel.

      (Alpha channel represents the degree of transparency that he found it and see)

- which is a feature of note is that even if the library cv2 position of the image does not exist, does not complain, get None.

3. Display image

Function: cv2.imshow (name, img)

  -Name name is defined, img second step is to read the images.

  -cv2.waitKey (0) waiting for keyboard input the name implies, in milliseconds, i.e., specified number of milliseconds to wait to see if there is a keyboard input, if any key is pressed within the waiting time of the ASCII key is returned, the program continues to run. If no key is pressed after the timeout -1. Parameter 0 indicates to wait indefinitely. WaitKey not call it, the window will be fleeting, do not see the picture shows.

  -cv2.destroyAllWindow () destroy all windows

E.g:

cv2.imshow('image',img)

cv2.waitKey(0)

cv2.destroyAllWindows()

4. Save image

Function: cv2.imwrit (file, img, NUM)

   -file is to save the file name

  -img is treated needs to be saved pictures

  -NUM is an optional parameter, it can be omitted. Its specific format: For JPEG, which represents the image quality, with 0 - represents an integer of 100, 95 by default; for png, the third parameter represents the level of compression, the default is 3.

More complex example:

cv2.imwrite('1.png',img, [int( cv2.IMWRITE_JPEG_QUALITY), 95])

cv2.imwrite('1.png',img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

Relatively simple example:

cv2.imwrite('1.png',img)

5. image manipulation

5.1 Flip

Function: cv2.flip (img, flipcode)

  -img, do not say

  -flipcode This parameter is represented rollover effects.

    - is 0, along the x axis, i.e. laterally inverted.

    -> 0, along the y-axis, i.e. the vertical flip.

    - <0, flip along x and y axes.

FIG above first to original, three flipcode following values ​​are: 0, 1, -1

Example: imgflip = cv2.flip (img, 1)

5.2 Copy Image

 

 

 

 Function: imgcopy = img.copy ()

5.3 color space conversion

Function: cv2.cvtColor (img, parameters) 

  -parameters: cv2.COLOR_X2Y,其中X,Y = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS

Example:

img = cv2.cvtColor (img, cv2.COLOR_RGB2GRAY) # color image into a grayscale image

img = cv2.cvtColor (img, cv2.COLOR_GRAY2RGB) # grayscale image into a color image

(to be continued)

Guess you like

Origin www.cnblogs.com/superSaiyan/p/12160907.html