如何使用 C++ 在 OpenCV 中儲存影像?


這裡,我們將瞭解如何將 OpenCV 影像儲存到計算機的任何位置。 OpenCV 提供 imwrite() 函式,以將影像儲存到指定檔案中。檔案擴充套件名錶示影像格式。 

該函式的實際格式為 -

imwrite("Destination/Name of the image with extension", Source Matrix)

這裡,“目標”是我們希望儲存影像的位置。在此程式中,我們以“Lakshmi.jpg”的形式儲存影像。我們可以為影像指定任何名稱。“源矩陣”是已載入影像的矩陣。在此程式中,影像載入為“myImage”矩陣。

範例

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc,const char** argv) {
   Mat myImage;//declaring a matrix named myImage//
   myImage = imread("lena.png");//loading the image named lena in the matrix//
   imwrite("lakshmi.jpg", myImage);  
   waitKey(0);//wait till user press any key
   destroyWindow("MyWindow");//close the window and release allocate memory//
   cout << "Image is saved successfully…..";
   return 0;
}

輸出

Image is saved successfully...

更新於: 2021 年 3 月 10 日

5000 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.