标签:
bool imwrite(const string& filename, InputArray img, const vector<int>& params=vector<int>());
filename
待写入的文件名。保存图像的格式由扩展名决定。
img
一般为一个Mat类型的图像
params
特定格式保存的参数编码:
实例:生成图片并保存到电脑
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> //#include <opencv2/imgcodecs/imgcodecs.hpp> //#include <opencv2/imgproc/imgproc.hpp> #include <vector> void createAlphaMat(cv::Mat &mat) { for (int i=0; i<mat.rows; i++) { for (int j = 0; j<mat.cols; ++j) { cv::Vec4b& rgba = mat.at<cv::Vec4b>(i,j); rgba[0] = UCHAR_MAX; rgba[1] = cv::saturate_cast<uchar>((float (mat.cols-j))/((float)mat.cols)*UCHAR_MAX); rgba[2] = cv::saturate_cast<uchar>((float (mat.rows-i))/((float)mat.rows)*UCHAR_MAX); rgba[3] = cv::saturate_cast<uchar>(0.5*(rgba[1]+rgba[2])); } } } int main(){ // create Mat with alpha channel cv::Mat mat(480, 640, CV_8UC4); createAlphaMat(mat); std::vector<int> compression_params; if(CV_VERSION_MAJOR==3) compression_params.push_back(cv::IMWRITE_PNG_COMPRESSION); // if OpenCV2 comment this else compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); // show image try{ imwrite("alpha.png", mat, compression_params); imshow("Created PNG", mat); fprintf(stdout, "Finish creating image with alpha channel.\n"); cv::waitKey(0); } catch(std::runtime_error& ex){ fprintf(stderr, "Error when converting to PNG: %s\n", ex.what()); return 1; } return 0; }
CMakeLists.txt
cmake_minimum_required (VERSION 2.8) project (WriteImage) # find OpenCV packages find_package( OpenCV REQUIRED PATHS /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/) include_directories( ${OpenCV_INCLUDE_DIRS} ) # add the executable add_executable (WriteImage WriteImage.cxx) target_link_libraries(WriteImage opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
标签:
原文地址:http://www.cnblogs.com/Xiaoyan-Li/p/5674806.html