QT+opencv [opencv learning] OpenCV reads, displays and saves images

Table of contents

 

1. OpenCV reads images

OpenCV reading function

parameter:

2. OpenCV display image

imshow function

imshow function function

imshow function prototype

3. OpenCV saves images

4. Results and code


 

1. OpenCV reads images

OpenCV allows us to perform various operations on images, but to do this we need to read an image file as input, which we can then perform various operations on. OpenCV provides the following functions for reading and writing images.

OpenCV reading function

The imread() function loads an image from the specified file and returns. The syntax is:

cv2.imread(filename[,flag])

parameter:

filename: the name of the file to be loaded

flag: This flag specifies the color type of the loaded image:

       IMREAD_UNCHANGED            = -1, //!<如果设置,返回加载的图像是(alpha通道,否则它将被裁剪)。忽略EXIF方
       IMREAD_GRAYSCALE            = 0,  //!<如果设置,始终将图像转换为单通道灰度图像(编解码器内部转换)。
       IMREAD_COLOR                = 1,  //!<如果设置,总是将图像转换为3通道BGR彩色图像。
       IMREAD_ANYDEPTH             = 2,  //!<如果设置,当输入有相应深度时返回16位/32位图像,否则转换为8位图像。
       IMREAD_ANYCOLOR             = 4,  //!<如果设置,图像将以任何可能的颜色格式读取。
       IMREAD_LOAD_GDAL            = 8,  //!<如果设置,使用gdal驱动程序加载图像。
       IMREAD_REDUCED_GRAYSCALE_2  = 16, //!<如果设置,始终将图像转换为单通道灰度图像,图像大小减小1/2。
       IMREAD_REDUCED_COLOR_2      = 17, //!<如果设置,总是将图像转换为3通道BGR彩色图像,图像大小减小1/2。
       IMREAD_REDUCED_GRAYSCALE_4  = 32, //!<如果设置,始终将图像转换为单通道灰度图像,图像大小减小1/4。
       IMREAD_REDUCED_COLOR_4      = 33, //!<如果设置,总是将图像转换为3通道BGR彩色图像,图像大小减小1/4。
       IMREAD_REDUCED_GRAYSCALE_8  = 64, //!<如果设置,始终将图像转换为单通道灰度图像,图像大小减小1/8。
       IMREAD_REDUCED_COLOR_8      = 65, //!<如果设置,总是将图像转换为3通道BGR彩色图像,图像大小减小1/8。
       IMREAD_IGNORE_ORIENTATION   = 128 //!<如果设置了,不要根据EXIF的方向标志旋转图像。

If the image cannot be read due to an unsupported file format, missing file, unsupported or invalid format, the imread() function returns a matrix. Currently, the following file formats are supported.

Window bitmaps - *.bmp, *.dib
JPEG files - *.jpeg, *.jpg, *.jpe
Portable network graphics - *.png
Portable image formats - *.pbm, *.pgm, *.ppm
TIFF files - *.tiff, *.tif

Note: The channels of color images and decoded images will be stored in BGR order.

2. OpenCV display image

imshow function

imshow function function

The function function of imshow is also very simple, and the name can also be seen, which is the abbreviation of image show. Imshow is responsible for displaying the image in the window and displaying it through the device screen.

imshow function prototype

CV_EXPORTS void imshow(const String& winname, const ogl::Texture2D& tex);

parameter:

  • no return value
  • Parameter 1, the displayed window name. You can use the cv::namedWindow function to create a window. If not, the imshow function will automatically create it.
  • The image that needs to be displayed

 

3. OpenCV saves images

The OpenCV  imwrite() function is used to save the image to the specified file. The file extension defines the image format. The syntax is as follows:

Function prototype:
CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,
              const std::vector<int>& params = std::vector<int>());

 

Function parameters:

  • filename: The file name to which the image needs to be saved, the format in which the image is to be saved, and the suffix.
  • img: The image to save.
  • params: Represents the parameter encoding saved for a specific format.

IMWRITE_JPEG_QUALITY      			//!对于JPEG,它的质量可以从0到100(越高越好)。缺省值为95。
IMWRITE_JPEG_PROGRESSIVE  			//!<启用JPEG特征,0或1,默认为False。
IMWRITE_JPEG_OPTIMIZE     			//!<启用JPEG特征,0或1,默认为False。
IMWRITE_JPEG_RST_INTERVAL 			//!< JPEG重启间隔,0 - 65535,默认为0 -不重启。
IMWRITE_JPEG_LUMA_QUALITY 			//!<单独的亮度质量等级,0 - 100,默认为0 -不使用。
IMWRITE_JPEG_CHROMA_QUALIT			//!<分离色度质量等级,0 - 100,默认为0 -不使用。
IMWRITE_PNG_COMPRESSION   			//!<对于PNG,它可以是0到9之间的压缩级别。数值越高,压缩时间越长,大小越小。如果指定,strategy将更改为IMWRITE_PNG_STRATEGY_DEFAULT (Z_DEFAULT_STRATEGY)。默认值为1(最佳速度设置)。
IMWRITE_PNG_STRATEGY      			//!< cv::ImwritePNGFlags之一,默认为IMWRITE_PNG_STRATEGY_RLE。
IMWRITE_PNG_BILEVEL       			//!<二进制级别PNG, 0或1,默认为0。
IMWRITE_PXM_BINARY        			//!<对于PPM, PGM或PBM,它可以是二进制格式标志,0或1。缺省值为1。
IMWRITE_EXR_TYPE          			//!< override EXR存储类型(默认为FLOAT (FP32))
IMWRITE_EXR_COMPRESSION   			//!< override EXR压缩类型(ZIP_COMPRESSION = 3是默认值)
IMWRITE_WEBP_QUALITY      			//!对于WEBP,它的质量可以从1到100(越高越好)。默认情况下(没有任何参数),如果质量高于100,则使用无损压缩。
IMWRITE_PAM_TUPLETYPE     			//!<对于PAM,将TUPLETYPE字段设置为为该格式定义的相应字符串值
IMWRITE_TIFF_RESUNIT 				//!<对于TIFF,用于指定要设置的DPI分辨率单位;有关有效值,请参阅libtiff文档
IMWRITE_TIFF_XDPI 					//!<对于TIFF,用于指定X方向DPI
IMWRITE_TIFF_YDPI 					//!<对于TIFF,用于指定Y方向DPI
IMWRITE_TIFF_COMPRESSION 			//!<对于TIFF,用于指定图像压缩方案。有关压缩格式对应的整数常量,请参阅libtiff。注意,对于深度为CV_32F的图像,只使用libtiff的SGILOG压缩方案。对于其他支持的深度,压缩方案可以通过该标志指定;LZW压缩是默认的。
IMWRITE_JPEG2000_COMPRESSION_X1000  //!<对于JPEG2000,用于指定目标压缩率(乘以1000)。取值范围为0 ~ 1000。缺省值为1000。

Return value: If the imwrite() function returns True, it means that the file is successfully written to the specified file.

Notice:

The imwrite function selects the format of the image based on the file extension. Generally, only 8-bit single-channel or 3-channel (with BGR channel order) images can be saved using this function, with the following exceptions:

For PNG, JPEG2000 and TIFF formats, 16-bit unsigned (CV_16U) images can be saved.
32-bit floating point (CV_32F) images can be saved to PFM, TIFF, OpenEXR and Radiance HDR formats; 3-channel (CV_32FC3) TIFF images saved using LogLuv high dynamic range encoding (4 bytes per pixel)
can be saved using this function with PNG image with alpha channel. To do this, create an 8-bit (or 16-bit) 4-channel image BGRA with the alpha channel last. Fully transparent pixels should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535.
If the format, depth or channel order is different, use Mat::convertTo and cv::cvtColor to convert before saving. Alternatively, use the generic FileStorage I/O functions to save the image to XML or YAML format.

4. Results and code

Code:

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/imgproc/types_c.h>
using namespace cv;


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Mat img=cv::imread("E:/1WT/18.OCR/1.Demo/OCRtest/test.bmp");

    imshow("Image", img);

    imwrite("save.jpg",img);

    qDebug()<<"1243";
}

result: 

11de34dd3b1e45ca85332ac264bd008a.png

71894c51f56c4ac28373b4b1287cb01e.png

 

 

 

Guess you like

Origin blog.csdn.net/qq_43445867/article/details/128422521