【OpenCV开发】imread和imwrite的类型以及第三个参数关于图片压缩质量等
Windows位图 - *.bmp, *.dib
JPEG文件 - *.jpeg, *.jpg, *.jpe
JPEG 2000文件- *.jp2
PNG图片 - *.png
便携文件格式- *.pbm, *.pgm, *.ppm
Sun rasters光栅文件 - *.sr, *.ras
TIFF 文件 - *.tiff, *.tif
enum ImreadModes {
IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image.
IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL = 8 //!< If set, use the gdal driver for loading the image.
};
= 2:若载入图像的深度为16或32位就返回对应深度的图像,否则将图像转换为8位图像
= 4:图像可被读取为任意可能的彩色格式
= 8:使用文件格式驱动加载图像
> 0返回一个三通道的彩色图像。
= 0返回灰度图像。
< 0返回包含Alpha通道的加载的图像。
CV_EXPORTS_W void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);
enum { WINDOW_NORMAL = 0x00000000, // the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size
WINDOW_AUTOSIZE = 0x00000001, // the user cannot resize the window, the size is constrainted by the image displayed
WINDOW_OPENGL = 0x00001000, // window with opengl support
WINDOW_FULLSCREEN = 1, // change the window to fullscreen
WINDOW_FREERATIO = 0x00000100, // the image expends as much as it can (no ratio constraint)
WINDOW_KEEPRATIO = 0x00000000 // the ratio of the image is respected
};
WINDOW_NORMAL:用户可以改变窗口的大小(没有限制),也可以将一个满屏的窗口转换成常用大小。
CV_EXPORTS_W void imshow(const String& winname, InputArray mat);
typedef const _InputArray& InputArray;
Mat imageTest = imread("F:\\opencvProgram\\TestImage\\帆板迎浪而上.jpg", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
namedWindow("帆板迎浪而上", WINDOW_NORMAL);
imshow("帆板迎浪而上", imageTest);
4.imwrite函数
CV_EXPORTS_W bool imwrite( const String& filename, InputArray img, const std::vector<int>& params = std::vector<int>());
第一个参数:const string&类型的filename,设置为需要写入的文件名就行了(包含后缀)。
Windows位图 - *.bmp, *.dib
JPEG文件 - *.jpeg, *.jpg, *.jpe
JPEG 2000文件- *.jp2
PNG图片 - *.png
便携文件格式- *.pbm, *.pgm, *.ppm
Sun rasters光栅文件 - *.sr, *.ras
TIFF 文件 - *.tiff, *.tif
/*opencv中,输出图像到文件一般用imwrite函数*/
/*函数声明如下:bool imwrite(const string& filename,
InputArray img, const vector<int>& params=vector<int>())*/
#include <vector>
#include <iostream>
#include <opencv2\opencv.hpp> using namespace std;
using namespace cv; void creatAlphaMat(Mat &mat)
{
/*通过转到定义,Mat为一个重载的函数其原型为:*/
/*Mat(int rows, int cols, int type);*/ 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; //unsigned char 的最大值
rgba[1] = saturate_cast<uchar>((float(mat.cols - j)) / ((float)mat.cols)*UCHAR_MAX);
rgba[2] = saturate_cast<uchar>((float(mat.rows - i)) / ((float)mat.rows)*UCHAR_MAX);
rgba[3] = saturate_cast<uchar>(0.5*(rgba[1] + rgba[2]));
}
}
} int main(void)
{
Mat mat(480, 640, CV_8UC4); //类型为8-bit unsigned integers(0..255),矩阵类型为四通道
creatAlphaMat(mat); vector<int>compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); //PNG格式图片的压缩级别
compression_params.push_back(9); try
{
imwrite("透明Alpha值图.png", mat, compression_params);
}
catch (runtime_error& ex)
{
fprintf(stderr, "图像转换成PNG格式发生错误:%s\n", ex.what());
return 1;
} fprintf(stdout, "PNG图片文件的alpha数据保存完毕!\n"); return 0;
}
【OpenCV开发】imread和imwrite的类型以及第三个参数关于图片压缩质量等的更多相关文章
- opencv中Mat与IplImage,CVMat类型之间转换
opencv中对图像的处理是最基本的操作,一般的图像类型为IplImage类型,但是当我们对图像进行处理的时候,多数都是对像素矩阵进行处理,所以这三个类型之间的转换会对我们的工作带来便利. Mat类型 ...
- OpenCV开发笔记(五十六):红胖子8分钟带你深入了解多种图形拟合逼近轮廓(图文并茂+浅显易懂+程序源码)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- OpenCV开发笔记(六十四):红胖子8分钟带你深入了解SURF特征点(图文并茂+浅显易懂+程序源码)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- OpenCV开发笔记(六十五):红胖子8分钟带你深入了解ORB特征点(图文并茂+浅显易懂+程序源码)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- OpenCV开发笔记(六十九):红胖子8分钟带你使用传统方法识别已知物体(图文并茂+浅显易懂+程序源码)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- OpenCV开发笔记(七十二):红胖子8分钟带你使用opencv+dnn+tensorFlow识别物体
前言 级联分类器的效果并不是很好,准确度相对深度学习较低,本章使用opencv通过tensorflow深度学习,检测已有模型的分类. Demo 可以猜测,1其实是人,18序号类是狗 ...
- OpenCV开发笔记(七十三):红胖子8分钟带你使用opencv+dnn+yolov3识别物体
前言 级联分类器的效果并不是很好,准确度相对深度学习较低,上一章节使用了dnn中的tensorflow,本章使用yolov3模型,识别出具体的分类. Demo 320x320,置信度0 ...
- win10 + VS2010 + OpenCV2.4.10重编译OpenCV开发环境搭建
win10 + VS2010 + OpenCV2.4.10重编译OpenCV开发环境搭建 重编译的优点:能够调试的时候看OpenCV的源码. 重编译要得到的东西:Debug版本号和Release版本号 ...
- OpenCV开发笔记(七十一):红胖子8分钟带你深入级联分类器训练
前言 红胖子,来也! 做图像处理,经常头痛的是明明分离出来了(非颜色的),分为几块区域,那怎么知道这几块区域到底哪一块是我们需要的,那么这部分就涉及到需要识别了. 识别可以自己写模板匹配.特征 ...
随机推荐
- try捕获SQL异常
- 一些简单题(2)(Source : NOIP历年试题+杂题)
P3084 [USACO13OPEN]照片Photo 给出$m$个区间$[l_i,r_i]$覆盖$S=[1,n]$,试确定最大特殊点的数使得这每一个区间覆盖且仅覆盖一个特殊点. 如果无解,输出$-1$ ...
- sh_06_元组基本使用
sh_06_元组基本使用 info_tuple = ("zhangsan", 18, 1.75, "zhangsan") # 1. 取值和取索引 print(i ...
- hive 调优(三)tez优化
我们采用亚马逊emr构建的集群,用hive查询的时候报错,FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.e ...
- Zookeeper入门(五)之Linux环境下Zookeeper安装
本文参考地址为:http://www.mamicode.com/info-detail-2243059.html1.安装wget http://archive.apache.org/dist/zook ...
- 后盾网lavarel视频项目---模型一对多关联简单实例
后盾网lavarel视频项目---模型一对多关联简单实例 一.总结 一句话总结: 在模型中定义一个方法来设置一对多关联:return $this->hasMany(Video::class); ...
- HiddenField Class
HiddenField Class Namespace: System.Web.UI.WebControls Assembly: System.Web.dll Represents a hidden ...
- 使用KFold进行训练集和验证集的拆分,使用准确率和召回率来挑选合适的阈值(threshold) 1.KFold(进行交叉验证) 2.np.logical_and(两bool数组都是正即为正) 3.np.logical_not(bool数组为正即为反,为反即为正)
---恢复内容开始--- 1. k_fold = KFold(n_split, shuffle) 构造KFold的索引切割器 k_fold.split(indices) 对索引进行切割. 参数说明:n ...
- DPM(物体检测)
1.DPM(物体检测流程) 1.计算DPM特征图 2.计算响应图 3.使用SVM对响应图进行分类 4.对最后的选框做局部检测识别 DPM的梯度提取方向,将图片中的四个区域进行区分,将有符号梯度方向从0 ...
- 【转】C++友元
转自:https://www.cnblogs.com/BeyondAnyTime/archive/2012/06/04/2535305.html 1.友元函数的简单介绍 1.1为什么要使用友元函数 在 ...