opencv3中的imwrite函数详解
2017-05-09 15:57
816 查看
opencv3中的imwrite函数是用来输出图像到文件,其声明如下:
第一个参数const String& filename表示需要写入的文件名,必须要加上后缀,比如“123.png”。
第二个参数InputArray img表示Mat类型的图像数据。
第三个参数const std::vector& params表示为特定格式保存的参数编码,它有一个默认值std::vector< int >(),所以一般情况下不用写。
示例程序:
以上代码会在D盘根目录生成一个Alppa.png。
CV_EXPORTS_W bool imwrite( const String& filename, InputArray img, const std::vector<int>& params = std::vector<int>()); /** @brief Reads an image from a buffer in memory. The function imdecode reads an image from the specified buffer in the memory. If the buffer is too short or contains invalid data, the function returns an empty matrix ( Mat::data==NULL ). See cv::imread for the list of supported formats and flags description. @note In the case of color images, the decoded images will have the channels stored in **B G R** order. @param buf Input array or vector of bytes. @param flags The same flags as in cv::imread, see cv::ImreadModes. */
第一个参数const String& filename表示需要写入的文件名,必须要加上后缀,比如“123.png”。
第二个参数InputArray img表示Mat类型的图像数据。
第三个参数const std::vector& params表示为特定格式保存的参数编码,它有一个默认值std::vector< int >(),所以一般情况下不用写。
示例程序:
#include <opencv2/opencv.hpp> #include <vector> using namespace std; using namespace cv;//包含cv命名空间 void createAlpha(Mat&); int main() { Mat mat(480,360,CV_8UC4);//创建带Alpha通道的Mat createAlpha(mat); vector<int>compression_params; compression_params.push_back(IMWRITE_PNG_COMPRESSION); compression_params.push_back(9); try { imwrite("D:\\Alppa.png",mat,compression_params); imshow("generated png file", mat); fprintf(stdout,"generate a png file in D\n"); } catch (runtime_error& ex){ fprintf(stderr,"generate file error:%s\n",ex.what());//捕获错误 return 1; } return 0; } void createAlpha(Mat &mat) { for (int i = 0;i<mat.rows;++i) { for (int j = 0; j < mat.cols; ++j) { Vec4b &rgba = mat.at<Vec4b>(i,j); rgba[0] = UCHAR_MAX; rgba[1] = saturate_cast<uchar>((float (mat.cols -j))/((float)mat.cols)*UCHAR_MAX); rgba[2] = saturate_cast<uchar>((float (mat.cols -i))/((float)mat.cols)*UCHAR_MAX); rgba[3] = saturate_cast<uchar> (0.5*(rgba[1]+rgba[2])); } } }
以上代码会在D盘根目录生成一个Alppa.png。
相关文章推荐
- 基础学习笔记之opencv(24):imwrite函数的使用
- OpenCV中Hough函数各参数详解
- Opencv之imwrite()函数的用处
- opencv:dft()函数详解
- opencv透视N点法函数详解
- 基础学习笔记之opencv(24):imwrite函数的使用
- Opencv--waitKey()函数详解
- 基础学习笔记之opencv(24):imwrite函数的使用
- OpenCV各种函数详解#include<QDebug> qDebug() << "hello";
- 关于opencv的Mat类型的imwrite()函数写入.jpg图像像素值会改变的解决方案
- HOG算子——计算方式,opencv函数详解,行人检测code
- 基础学习笔记之opencv(24):imwrite函数的使用
- Opencv输出图像函数-imwrite
- opencv之imwrite函数使用
- OpenCV实现FloodFill泛洪填充算法的代码及相关函数详解
- [转]opencv轮廓提取(findcontours函数详解)
- opencv中imgproc库中函数详解(1)
- opencv imwrite()函数 崩溃 失败
- 详解OpenCV中的cvCreateMat()函数
- 【OpenCV3图像处理】查找二值图像的边缘 函数findContours()详解