Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write pictures in OpenCV basic operation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will show you how to read and write pictures in the basic operation of OpenCV. The editor thinks that the content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

1. The function for reading images is: cv2.imread

The cv2.imread function syntax is as follows:

Cv2.imread (filename,flag=1)

The argument explanation of the cv2.imread function:

Filename: the relative address of the image to be read (same as the open function in the file)

Flag: sets the format for reading. The default is 1, which means it is read in the way of three channels of BGR; if it is 0, it is read in a single channel of grayscale images.

2. The function cv2.imwrite for saving images:

Cv2.imwrite function syntax:

Cv2.imwrite (filename,img)

Parameter interpretation of cv2.imwrite function

Filename: the name of the picture to be saved and the relative address of the program.

Img: the matrix form of the picture to save.

Program function: convert the GBR format pictures in the .py folder into grayscale images.

Program demonstration:

Program code:

Import cv2import numpy as npImg=cv2.imread ('1.jpggift penny 0) cv2.imwrite (' 1.jpg skill pencil Img)

If you want to change the format of the picture, you can also change .jpg to .png when saving the picture.

In this way, when the program is finished, you will find the same image 1.png as 1.jpg in the .py file.

Import cv2import numpy as npImg=cv2.imread ('1.jpggift penny 0) cv2.imwrite (' 1.pngpen pensionary Img)

This is nothing more than a good way to convert the image format.

3. Picture display function cv2.imshow

Picture display function: cv2.imshow

The syntax of the function cv2.imshow:

Cv2.imshow (name,img)

The argument explanation of the cv2.imshow function:

Name: the name of the display window

Img: the matrix form of a picture

Picture delay function cv2.waitKey

Function cv2.waitKey syntax

Cv2.waitKey (time)

Cv2.waitKey function argument explanation:

Time represents the time to wait, in milliseconds.

This function can be understood that during time time, the computer waits for commands on our keyboard, and if the program does not wait for keystroke instructions within time time, it automatically enters the next frame. Because this is a picture, there is no next frame, so the window closes automatically.

It should be noted that if we set time to 0, instead of waiting for 0 milliseconds to enter the next frame, we will stop at the current frame and press a key to enter the next frame, so when displaying a single picture, we usually use the

Cv2.waitKey (0)

Code example:

Import cv2import numpy as npimg=cv2.imread ('1.jpg recording recording cv2.IMREADDCOLOR) cv2.imshow (' image',img) # add a delay function cv2.waitKey (0) in order to keep the picture in the program window for a long time; cv2.imwrite ('ba_ck_.png',img) # if you use the cv2.imshow function, be sure to follow a function that destroys the window, cv2.destroyAllWindows ()

Program improvement (1):

In the above program, after displaying the picture, press any key on the keyboard, and the picture will be closed. If we press a specific build to close the program, we can judge by an if statement. If you don't press the Q key, you stay in the while loop until we press the Q key.

Import cv2import numpy as npimg=cv2.imread ('1.jpgjournal journal cv2.IMREADDCOLOR) cv2.imshow (' image',img) while 1: if cv2.waitKey (0) = ord ('q'): break else: pass#continuecv2.imwrite ('ba_ck_.png',img) # if you use the cv2.imshow function, be sure to follow a function that destroys the window, cv2.destroyAllWindows ()

Program improvement (2):

In the above program, after we press Q to exit the display, the program will be saved automatically. If we want to press Q to exit directly, press s to exit and save, we need to add an if conditional statement.

Import cv2import numpy as npimg=cv2.imread ('1.jpgendry cv2.IMREADOCOLOR) cv2.imshow (' image',img) while 1: if cv2.waitKey (0) = ord ('q'): break if cv2.waitKey (0) = = ord ('s'): cv2.imwrite ('ba_ck_.png',img) break# if the cv2.imshow function is used The following must be followed by a window destruction function cv2.destroyAllWindows () attached, OpenCV: read the picture summary

When typing this code, I made a lot of mistakes. I always thought there was something wrong with the configuration, and then I kept troubleshooting the configuration. As a result, I put Mat img = imread ("C:\ Users\\ sq.fu\\ Desktop\ pic.jpg"); this sentence was written as Mat img = imread ("C:\ Users\\ sq.fu\\ Desktop pic.jpg"); fortunately, I finally found it.

# include # include using namespace cv; void main () {Mat img = imread ("C:\\ Users\\ sq.fu\\ Desktop\ pic.jpg"); / / Mat img = imread ("C:\\ Users\\ sq.fu\\ Desktop\\ pic.jpg"); namedWindow ("test"); imshow ("test", img); waitKey (100);}

These are all the contents of how to read and write pictures in OpenCV basic operations. For more information about how to read and write pictures in OpenCV basic operations, you can search the previous articles or browse the following articles to learn! I believe the editor will add more knowledge to you. I hope you can support it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report