02.OpenCV-GUI- image

This paper describes opencv read into the image, image display and save images 

Reference code opencv (python) 

import numpy as np #引入numpy
import cv2  #引入opencv

img = cv2.imread('messi5.jpg',0) #读入图片
cv2.imshow('image',img) #显示图片,参数为窗口名,图片名
k = cv2.waitKey(0)  #延时
if k == 27:         #按下ESC 则跳出
    cv2.destroyAllWindows()
elif k == ord('s'): # 按下 s 则保存退出
    cv2.imwrite('messigray.png',img) #保存图片,参数为保存的路径和图片名,图片名
    cv2.destroyAllWindows()

note:

cv2.imread ( 'messi5.jpg', 0) before the name to add pictures inside path

import cv2 opencv2.0 version is not introduced

Window displays:

Code Description:

A read image
  using a function cv2.imread () reads an image. This image should be in the path of this program of work, or provide a full path to the function, the second argument is to show how the function should read this picture.

  • cv2.IMREAD_COLOR: reading a color image. Transparency of the image will be ignored, which is the default parameter.
  • cv2.IMREAD_GRAYSCALE: a read image in grayscale
  • cv2.IMREAD_UNCHANGED: reading an image, and includes an alpha channel image

Displaying an image
  using the function cv2.imshow () to display an image. Window will automatically adjust the image size. The first parameter is the name of the window, followed by our image. You can create as many windows as you like, but you must give them different names

Save the image
  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.waitKey () function is a key bindings. It should be noted that it is the time scale of 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 returns the ASCII value of the key, will continue to run the program. If there is no keyboard input, the return value is -1, if we set the parameters of this function is zero, then it will wait indefinitely for keyboard input. It can also be used to detect whether a particular key is depressed, for example, a key has been pressed, then we will discuss this later.
cv2.destroyAllWindows () can easily delete any window we created. If you want to delete a specific window can be used cv2.destroyWindow (), enter the name of the window you want to delete in parentheses.

Use Matplotlib
  Matplotib python is a drawing library, inside there are a variety of drawing methods, very similar to matlab, the future will continue to update the code related to learning

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('messi5.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.show()

 

Guess you like

Origin blog.csdn.net/weixin_42572978/article/details/92606321