OpenCV4教程——3. 图片相关操作

目标

读取、显示和保存一个图像文件。涉及 imread()、imshow() 和 imwrite() 三个函数。

读取图片

作用

从文件中读取图片。

目前支持的图片格式有:

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see the Note section)
  • JPEG 2000 files - *.jp2 (see the Note section)
  • Portable Network Graphics - *.png (see the Note section)
  • WebP - *.webp (see the Note section)
  • Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported)
  • PFM files - *.pfm (see the Note section)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Note section)
  • OpenEXR Image files - *.exr (see the Note section)
  • Radiance HDR - *.hdr, *.pic (always supported)
  • Raster and Vector geospatial data supported by GDAL (see the Note section)

头文件

C++

#include <opencv2/imgcodecs.hpp>

函数原型

C++

Mat cv::imread(const String& filename, int flags = IMREAD_COLOR)

Python

retval = cv.imread(filename[, flags])

输入参数

filename           要读入图片文件名

flags                 标志

返回值

Mat                  OpenCV定义的 n 维稠密矩阵。

调用例子

C++

    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file

Python

retval = cv.imread(filename)

显示图片

作用

在指定的窗口中显示图形。

头文件

C++

#include <opencv2/highgui.hpp>

函数原型

C++

void cv::imshow(const String& winname, InputArray mat)

Python

None = cv.imshow(winname, mat)

输入参数

winname           显示窗口的名字。

mat                  OpenCV定义的 n 维稠密矩阵,来自 imread() 函数。

返回值

无。

调用例子

C++

imshow( "Display window", image );

Python

retval = cv.imread(filename)

保存图片

作用

将 Mat 数据写入到指定文件中。

扫描二维码关注公众号,回复: 10409416 查看本文章

头文件

C++

#include <opencv2/imgcodecs.hpp>

函数原型

C++

void cv::imwrite(const String& winname, 
                 InputArray mat, 
                 const std::vector< int > & params = std::vector< int >())

Python

retval = cv.imwrite(filename, img[, params])

输入参数

filename           保存的文件名字。

mat                  OpenCV定义的 n 维稠密矩阵,要保存的图片数据。

params            指定的保存参数。

返回值

无。

调用例子

C++

    vector<int> compression_params;
    compression_params.push_back(IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(9);
    bool result = false;
    try
    {
        result = imwrite("alpha.png", mat, compression_params);
    }
    catch (const cv::Exception& ex)
    {
        fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
    }
    if (result)
        printf("Saved PNG file with alpha data.\n");
    else
        printf("ERROR: Can't save PNG file.\n");

Python

retval = cv.imwrite("alpha.png", mat)

完整例子

C++

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv ) {
    if( argc != 2){
        cout <<" Usage: " << argv[0] << " ImageToLoadAndDisplay" << endl;
        return -1;
    }
    
    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file
    if (image.empty()) { // Check for invalid input
        cout << "Could not open or find the image" << std::endl ;
        return -1;
    }
    
    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
    imshow( "Display window", image ); // Show our image inside it.
    waitKey(0); // Wait for a keystroke in the window
    
    return 0;
}

Python

# -*- coding:UTF-8 -*-
import cv2

def main():
    img = cv2.imread("1.jpg")
    cv2.imshow("EmptyImage3", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

#当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行
if __name__ == '__main__': 
    main()
发布了268 篇原创文章 · 获赞 309 · 访问量 108万+

猜你喜欢

转载自blog.csdn.net/justidle/article/details/105231875
今日推荐