一、图像的读取、显示和保存
1、Mat imread(const string& filename, int flags=1)
filename: 读入图像的完整路径
flags: 读入图像的方式
CV_LOAD_IMAGE_GRAYSCALE:单通道灰度图 CV_LOAD_IMAGE_COLOR:三通道彩色图 CV_LOAD_IMAGE_UNCHANGED 保持不变
Mat image = imread("img.jpg",CV_LOAD_IMAGE_UNCHANGED);
2、void imshow(const string& winname, InputArray image)
//InputArray:接口类可以是 Mat、Mat_<T>、Mat_<T, m, n>、vector<T>、vector<vector< T> >、vector<Mat>
winname: 显示图像的窗口名
image: 要显示的图像
- namedWindow("Input");
- imshow("Input", image);
3、bool imwrite(const string& filename, InputArray image, const vector< int>& params=vector<int>())
filename: 保存的文件名
image: 要保存的图像
- params - Format - specific save parameters encoded as pairs paramId_1,
- paramValue_1,
- paramId_2,
- paramValue_2,
- ….The following parameters are currently supported: For JPEG,
- it can be a quality(CV_IMWRITE_JPEG_QUALITY) from 0 to 100(the higher is the better).Default value is 95.对于JPEG,图像质量。For PNG,
- it can be the compression level(CV_IMWRITE_PNG_COMPRESSION) from 0 to 9.A higher value means a smaller size and longer compression time.Default value is 3.对于PNG,压缩等级。For PPM,
- PGM,
- or PBM,
- it can be a binary format flag(CV_IMWRITE_PXM_BINARY),
- 0 or 1.Default value is 1.imwrite("out.jpg", output);
二、复制、翻转、颜色空间转换
1、void Mat::copyTo(OutputArray m) const
2、void Mat::copyTo(OutputArray m, InputArray mask) const
mask 为行列与 srcImage 相同的掩膜矩阵 (可以由图片复制),如果矩阵某元素为零,则对应位置像素无需覆盖,保留 imageROI 中对应位置的像素值。
3、void flip(InputArray src, OutputArray dst, int flipCode)
flipCode: 翻转模式
flipcode = 0:沿 x 轴翻转 flipcode > 0:沿 y 轴翻转 flipcode < 0:x,y 轴同时翻转
4、void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
code:CV_BGR2GRAY, CV_BGR2YCrCb, CV_BGR2HSV……
dstCn: 输出图像通道数
示例
- #include#include#includeusing namespace cv;
- int main() {
- Mat image = imread("img.jpg", CV_LOAD_IMAGE_UNCHANGED);
- if (!image.data) //图像载入失败return -1;namedWindow("Input");imshow("Input",image);Mat output;image.copyTo(output);//复制flip(output,output,-1);//翻转cvtColor(output,output,CV_BGR2RGB);//BGR->RGBimwrite("out.jpg",output);//保存namedWindow("Output");imshow("Output",output);waitKey(0);return 0;}
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: http://www.92to.com/bangong/2017/03-09/18350055.html