【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分钟带你深入级联分类器训练
前言 红胖子,来也! 做图像处理,经常头痛的是明明分离出来了(非颜色的),分为几块区域,那怎么知道这几块区域到底哪一块是我们需要的,那么这部分就涉及到需要识别了. 识别可以自己写模板匹配.特征 ...
随机推荐
- nginx第五天
nginx的全局变量 变量 说明 $args 请求中的参数,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2 $content_length HTTP ...
- Acwing-97-约数之和(整数分解, 递推分治)
链接: https://www.acwing.com/problem/content/99/ 题意: 假设现在有两个自然数A和B,S是AB的所有约数之和. 请你求出S mod 9901的值是多少. 思 ...
- qt类表
- 本机的IP地址无法打开(Vue项目)
1, 首先找到使用vue脚手架建立项目config文件中的index.js文件2, 修改dev里面的host属性值:改成 host: ‘0.0.0.0’3, 最后在局域网下,使用自己的ip进行连接,同 ...
- 求两个数的最大公约数和最小公倍数Java(cvte考题)
//最大公约数 最小公倍数 通过测试 public class GongYue{ public static int gongyue(int m, int n) throws Exception{ i ...
- C#生成的后台文件 ***.vshost.exe
vshost是visual studio宿主应用程序,vs运行调试时是打开的其实是这个文件,这个程序可以让vs跟踪调试信息.而exe则可以直接打开,vs不会跟踪任何这个文件的运行情况.只要引用的程序集 ...
- 百度地图api,点击标注,改变标注marker图标的链接地址
改变选中的图标样式 // 选中高亮标注图片 let mapIcon = ‘./icon.png’; //标注点 let markerArrs = [{},{},....]; // 点击标注点 mark ...
- TTTTTTTTTTTTT CF Good Bye 2015 C- New Year and Domino(CF611C) 二维前缀
题目 题意:给你一个n*m由.和#组成的矩阵,.代表可以放,#代表不可以,问在左上角(px,py)到(右下角qx,qy)这样的一个矩阵中,放下一个长度为2宽度为1的牌有多少种放法: #include ...
- lcez校内模拟赛: 小R与苹果派——题解
题目传送 首先对两个数组排序. 然后预处理出数组p[i]表示b[x]<a[i]的最大的x. 然后我们设f[i][k]表示对于前i个派,我单独选出来k组a[y]>b[y].(即此时有k组a& ...
- Linux 环境安装运行Sqlmap
1.官网下载 .tar.gz 文件 官网地址:http://sqlmap.org/ 2.登录访问linux环境,将压缩包放入/usr/local 路径. 3.在该路径下通过 tar -xzvf f ...