OpenCV study notes 01--Basic image processing operations--read, display, save

       

content

(1) Read the image

(2) Display image

(3) Save the image


       After reading a lot of books and materials on image processing, I am going to summarize the knowledge points related to image processing, so as to increase the impression and use this knowledge in the future.

       The concept of an image is believed to be familiar to most people. If you want a computer to process an image, of course, it is to convert an image into a number to represent it. An image is composed of pixel values ​​one by one. processing, and the term image processing is also derived from it.

(1) Read the image

       OpenCV is a powerful image processing function library. It encapsulates each function into a function. We can directly process our image through the function.

cv2.imread(picturename,parameters)

where picturename is the name of the image we read,

The value of parameters can take -1, 0, 1, 2, 4, 8 to mark the type of image file to be read

When -1 is taken: the original format of the image is read

When 0: adjust the image to a single-channel grayscale image

When 1 is taken: adjust the image to a three-channel RGB image, if there is no parameter parameter, the default is to take 1

When taking 2: unchanged if the depth of the image is 16 or 32, otherwise converted to an 8-bit image

When taking 4: read the image in any possible color

When taking 8: use the gdal driver to load the image

There is no need to memorize these values. When you use them, you can check them. The default value is 1.

Example:

import cv2
pictureName = r'C:\Users\LBS\Desktop\01.jpg'
picture = cv2.imread(pictureName, 1)
print(picture)

result:

 The following 255 is the value of each pixel represented, that is, the pixel value, indicating that the picture was successfully read.

(2) Display image

       After we read the image, what we print out is an array matrix, which is composed of numbers. How to present it in the form of an image? Use the following function:

imshow('windowsname',picturename)

windowsname means the name of the window, that is, the image you will display occupies a window, then you can give this window an alias windowsname, such as the window name lession1 in the screenshot below.

picturename is the image file we read through imread above.

Example:

import cv2
pictureName = r'C:\Users\LBS\Desktop\01.jpg'
picture = cv2.imread(pictureName, 1)
cv2.imshow('lession1', picture)
key = cv2.waitKey(0)

Result: If waitkey is not used, the image will flash by. The following will introduce the usefulness of this function

      Let's talk about the waitkey function

reval = waitKey(time)

time represents the time to wait for the keyboard to be pressed. If there is no such time, the default value is 0. When the value is 0 or a complex number, it means that it has been waiting for the user to press the keyboard. If the keyboard is not pressed, the program will stop. to here.

reval indicates the returned value. If the keyboard is not pressed, the return value is -1. If a key is pressed, the value corresponding to the ascii code corresponding to the key is displayed.

Next, we use an example to illustrate the function of this function

import cv2
pictureName = r'C:\Users\LBS\Desktop\01.jpg'
picture = cv2.imread(pictureName, 1)
cv2.imshow('lession1', picture)
# 当程序执行到这里的时候,我希望用户按下键盘的任意键,否则的话,我下面打印的这句话不会执行
key = cv2.waitKey(0)
print('我按下键盘之后我才能执行这句话哦')

result:

 There are two other functions that everyone knows about.

Close a window destroyWindow(picturename),

Close all currently displayed windows destroyAllWindows()

From the function name, we should be able to see that it means to close the window of the image we display, either one or all of them.

(3) Save the image

imwrite('pathname',picturename)

pathname indicates which folder you want to save to. You can use an absolute path or a relative path. It also includes the extension of the image, that is, the format of the save. If the extension is not written, you can use it separately after the picturename. Specifies the save format suffix.

picturename is the image file we want to save

Example:

import cv2
pictureName = r'C:\Users\LBS\Desktop\01.jpg'
picture = cv2.imread(pictureName, 1)
cv2.imshow('lession1', picture)
cv2.imwrite('picture01.jpg', picture)

Result: The path I save is the folder where the current program is written, so I just need to save it with another name. If I want to save to another folder, I may need C:\Destop\. ..etc prefix.

 Summary: The above is the first note about image processing. Let’s look at my personal time. If I have the ability, I will summarize the relevant knowledge points into notes for your reference.

Please indicate the source.

The next article is about the basics of image processing.

Guess you like

Origin blog.csdn.net/BaoITcore/article/details/120687213