OpenCV learning notes - day1 (image reading, display and saving imread, imshow, namedWindow, imwrite function)

day1 - image reading, display and saving

 use OpenCV Development module, open a picture in the host directory,
 And displayed on the desktop

Function:

imread, imshow, namedWindow and imwrite functions in the highug module

  1. imread() reads the image
    Mat imread(const string& filename, int flags=1)
parameterexplain
filenameRead file address file name
flagsThe color category of the read image (flags=1: (default parameter) indicates that the 3-channel image is returned; CV_LOAD_IMAGE_ANYEPTH: returns a 16bit or 32bit image (when the same depth is input), otherwise returns an 8bit image; CV_LOAD_IMAGE_COLOR: returns a monochrome image; CV_LOAD_IMAGE_GRAYSCALE: return as channel image; 0: return to single channel image< 0: no channel conversion for image; > 0: cast to 3-channel image.

Example 1

//Image reading
Mat scr = imread("D:\\study\\OpenCV study\\pictures\\image\\1.jpg");
  1. , imshow() displays an image in the specified window
    void imshow(const String& winname, InputArray mat);
parameterexplain
winnameConst string & the name of the window ID to be displayed
matmat of InputArray type refers to the image to be displayed

notes: imshow() 1, only 8-bit and floating-point numbers are supported to display RGB color space;
2. RGB color space: B G R three channels, three color combination 255255255 and transparent channel

Example 2

imshow("Window 1",scr);//Displayed in created window 1
  1. namedWindow() creates a new window
    void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);
parameterexplain
winnameConst string & the name of the window used as the window identifier
flagsThe identification of the window. Generally, the default is windows_ Autosize constrains the size of the window by the displayed image

notes:
CV_WINDOW_NORMAL changes the window to normal size or makes it resizable
CV_WINDOW_AUTOSIZE constrains the size of the window by the displayed image. The window is not resizable
CV_WINDOW_FULLSCREEN changes the window to full screen
CV_WINDOW_FREERATIO enables windows to resize without ratio constraints
CV_ WINDOW_ Keeratio enables the window to resize, but retains the scale of the displayed image

Example 3

namedWindow("Window 1", WINDOW_NORMAL);//Create windows_ Freeratio window
  1. waitkey() int waitKey(int delay = 0);
    The function of waitKey function is to refresh the image continuously. The frequency and time is delay, and the unit is ms. the return value is the value pressed by the current keyboard. When there is no key, it returns - 1

Example 4

waitKey(0);//Infinite loop
while(1){ if(waitKey(100)==27)break; }
//In this program, tell OpenCv to wait for the user to trigger the event,
//The waiting time is 100ms. If the user presses ESC(ASCII code is 27) within this time period, the cycle will jump out; otherwise, the cycle will jump out

code:

quickopencv.h

//Define class
#pragma once

#include <opencv2\highgui.hpp>

#include <opencv2\imgproc.hpp>

using namespace cv;
//Define class
class QuickDemo{
public:
	void colorSpace_Demo(Mat &image);//Color space conversion function 2021-12-24
};

QuickDemo.cpp

#include <iostream>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>

using namespace cv;
using namespace std;

int main()
{
	Mat scr = imread("D:\\study\\OpenCV study\\pictures\\image\\1.jpg");//Open a diagram
	if (!scr.data == 1)//Air judgment
		return -1;
	namedWindow("Window 1", WINDOW_FREERATIO);//Create windows_ Freeratio window
	imshow("Window 1",scr);//Show in created window
	waitKey(0);//Delay 0 - > constant delay 1 - > delay 1ms
	return 0;
}

notes:
Conditions for blank judgment (as shown in the complete code above):
1,scr.empty();// Judge whether it is a form. if it is not loaded successfully, it is blank. print: could not laod image;
2,scr.data;//if(!scr.data)==1 failed to read

Keywords: OpenCV AI Computer Vision

Added by MoMoMajor on Mon, 03 Jan 2022 18:28:19 +0200