OpenCv之读取修改保存图像
目标
图像读取 imread
图像颜色空间的转换 cvtColor
图像保存至硬盘 (using imwrite)
一、代码
#include <iostream>
#include<string>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
static void test02() {
Mat image;
const string path("E:/picture/renwu1.jpg");
const string input_image = "input image";
const string output_image = "gray image";
image = imread(path);
if (!image.data) {
cout << "load image is failed..." << endl;
return;
}
Mat gray_image;
cvtColor(image, gray_image, COLOR_BGR2GRAY);
imwrite("E:/picture/write/Gray_image.jpg", gray_image);
namedWindow(input_image, WINDOW_AUTOSIZE);
namedWindow(output_image, WINDOW_AUTOSIZE);
imshow(input_image, image);
imshow(output_image, gray_image);
waitKey(0);
}
二、结果
检查你的输出文件夹,将发现一个新的.jpg文件。文件名为 Gray_image.jpg :