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 數據寫入到指定文件中。

頭文件

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()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章