SoFunction
Updated on 2024-10-29

OpenCV Basic Operation Guide for Reading and Writing Images

1, the function that does the image reading is:

The function syntax is as follows:

(filename,flag=1)

Explanation of the function's parameters:

filename: the relative address of the image to be read (in the same way as the file is read by the open function)

flag: set the format of reading, default is 1, it means read according to the BGR three-channel way; if it is 0, it will read in the way of single channel of grayscale map.

2. Functions for saving pictures.

Function Syntax:

(filename,img)

Explanation of function parameters

filename: the name of the image to be saved, the relative address of the program.

img: the matrix form of the image to be saved.

Program Function: Converts GBR format images in the .py folder into grayscale images.

Program Demo:

Program Code:

import cv2
import numpy as np
Img=('',0)
('',Img)

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

This way you will find a picture with the same image in the .py file at the end of the program run.

 

import cv2
import numpy as np
Img=('',0)
('',Img)

This has nothing but a great way to convert image formats.

3, the display function of the picture

Picture display function:

The syntax of the function:

(name,img)

Explanation of the function's parameters:

name: name of the display window

img: matrix form of the image

Image Delay Functions

function syntax

(time)

Explanation of function parameters:

time indicates the waiting time in milliseconds.

This function can be understood like this, in time time, the computer will wait for the command from our keyboard, if the program does not wait for the key command in time time, he will automatically go to the next frame. Because this is a picture, there is no next frame, so it will automatically close the window.

It should be noted here that if we set time to 0, we are not waiting for 0 milliseconds to enter the next frame, but stop at the current frame, and only enter the next frame if there is a keystroke, so when displaying a single image, it is common to use the

(0);

Code Example:

import cv2
import numpy as np
img=('',cv2.IMREAD_COLOR)
('image',img)
# To keep the image in the program window for a long time, add another delay function.
(0);
('ba_ck_.png',img)
# If a function is used, it must be followed by a function that destroys the window
()

Program Improvement (1):

In the above program, after displaying the picture, pressing any key on the keyboard closes the picture. If we were to press a specific build to close the program, we could determine this with an if statement. If we press anything other than the q key, we stay in a while loop until we press the q key.

import cv2
import numpy as np
img=('',cv2.IMREAD_COLOR)
('image',img)
while 1:
    if (0)==ord('q'):
        break
    else:
        pass#continue
('ba_ck_.png',img)
# If a function is used, it must be followed by a function that destroys the window
()

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 and press s to exit and save, we need to add another if conditional statement.

import cv2
import numpy as np
img=('',cv2.IMREAD_COLOR)
('image',img)
while 1:
    if (0)==ord('q'):
        break
    if (0)==ord('s'):
        ('ba_ck_.png',img)
        break
# If a function is used, it must be followed by a function that destroys the window
()

Attachment, OpenCV: read image summary

In knocking this code, made a lot of mistakes, always thought it was a problem with the configuration, and then have been troubleshooting the configuration, it turned out to be the wrong path way ,, put Mat img = imread("C:\\\Users\\\\Desktop\\"); this sentence is written as Mat img = imread("C:\\\Users\\\\ Desktop "); , luckily I finally found it!

#include <iostream>  
#include <opencv2\core\>  
#include <opencv2\imgproc\>  
#include <opencv2\highgui\>  
  
using namespace cv;  
  
void main( )  
{  
    Mat img = imread("C:\\Users\\\\Desktop\\");  //  Mat img = imread("C:\\Users\\\\Desktop\\");  
 
    namedWindow("test");  
   imshow("test",img);  
    waitKey(100);  
}  

summarize

to this article on the OpenCV basic operation guide to the picture of the read and write the article is introduced to this, more related to OpenCV picture of the read and write content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!