opencv tutorial 1 read and save pictures

• Here you will learn how to read an image, how to display an image, and how to save an
image
• You will learn the following functions: cv2.imread(), cv2.imshow(), cv2.imwrite()
• If you want, I will show you how to use Matplotlib to display a picture

Use the function cv2.imread() to read in the image. The image should be in the working path of the program,
or provide the full path to the function. The
second parameter is to tell the function how to read the image.
• cv2.IMREAD_COLOR: read in a color image. The transparency of the image will be ignored,
which is the default parameter.
• cv2.IMREAD_GRAYSCALE:
read in an image in grayscale mode cv2.IMREAD_UNCHANGED: read in an image, and include the alpha channel of the image

 import numpy as np
 import cv2
 # Load an color image in grayscale
  img = cv2.imread('messi5.jpg',0)

Insert picture description here
4.2 Displaying the image
Use the function cv2.imshow() to display the image. The window will automatically adjust to the image size. The first
parameter is the name of the window, and the second is our image. You can create as many windows as you like, but you must give them different names

cv2.imshow('image',img)
cv2.waitKey(0) 
cv2.destroyAllWindows()

Insert picture description here
cv2.waitKey() is a keyboard binding function. It needs to be pointed out that its time scale is in the order of milliseconds
. The function waits for a certain number of milliseconds to see if there is keyboard input. Within a few milliseconds, if any key is pressed, this function will return the ASCII code value of the key, and the program will continue to run. If there is no keyboard input, return back to a value of -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 specific key is pressed, such as whether the button a is pressed, we will discuss this later.
cv2.destroyAllWindows() can easily delete any windows we created. If
you want to delete a specific window, you can use cv2.destroyWindow(), enter the
name of the window you want to delete in parentheses

Suggestion: In a special case, you can also create a window first and
then load the image. In this case, you can decide whether the window can be resized
. The function used is cv2.namedWindow(). The initial setting function
label is cv2.WINDOW_AUTOSIZE. But if you change the label to
cv2.WINDOW_NORMAL, you can adjust the window size. When the image dimension is too large,
or when you want to add a track bar, it will be useful to adjust the window size.

3 Saving the image
Use the function cv2.imwrite() to save an image. You need a file name first, and then
the image you want to save.

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

The following program will load a grayscale image, display the picture, press the's' key to save and then exit, or
press the ESC key to exit without saving


```cpp
# -*- coding: utf-8 -*-
 """
 Created on Wed Jan 1 20:49:33 2014
 @author: duan
 """
 import numpy as np
 import cv2

img = cv2.imread('messi5.jpg',0)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
1cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()
使用 Matplotlib
Matplotib 是 python 的一个绘图库,里头有各种各样的绘图方法。之后
会陆续了解到。现在,你可以学习怎样用 Matplotib 显示图像。你可以放大图
像,保存它等等。

```cpp
 # -*- coding: utf-8 -*-
"""
Created on Wed Jan 1 20:49:33 2014
 @author: duan
"""
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()

Insert picture description here
See: Matplotib has a variety of plotting options. See Matplotib docs for details. We will also
learn some notes one after another: **Color images are loaded in BGR mode when using OpenCV. But Matplotib is in RGB mode. **So if the color image has been read by OpenCV, it will not be displayed correctly by Matplotib. Please see the exercise for details.

Guess you like

Origin blog.csdn.net/qq_43543515/article/details/115266040