[openvc]4.创建一张图片并保存到本地

本文实现的功能就是生成一张绿色图片,然后保存到本地。
这里也是直接贴出代码了:

#include "opencv2/highgui/highgui.hpp"
#include 

using namespace cv;
using namespace std;

int main(int argc, const char** argv)
{
    Mat img(650, 600, CV_16UC3, Scalar(0, 50000, 0)); //创建一张图片,Blue=0,Green=50000,Red=0

    if (img.empty()) 
    {
        cout << "ERROR : Image cannot be loaded..!!" << endl;
        //system("pause"); //wait for a key press
        return -1;
    }
    vector<int> compression_params; //图片参数
    compression_params.push_back(CV_IMWRITE_PNG_BILEVEL); //png
    compression_params.push_back(98); //图片质量
    bool bSuccess = imwrite("D:/TestImage.jpg", img, compression_params); //保存到本地

    if (!bSuccess)
    {
        cout << "ERROR : Failed to save the image" << endl;
        system("pause"); 
    }
    //namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
    imshow("MyWindow", img); 

    waitKey(0);  //wait for a keypress

    destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

    return 0;
}

运行效果如下:
[openvc]4.创建一张图片并保存到本地_第1张图片

你可能感兴趣的:(opencv)