opencv学习笔记之读取,修改,保存图像
2016-10-25 10:08
561 查看
直接附上程序及效果图:
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc,char** argv)
{
Mat Img;//原始图像
Img=imread("1.jpg",CV_LOAD_IMAGE_COLOR);
if(!Img.data)
{
cout<<"could not open"<<endl;
return -1;
}
Mat gray_Img;//灰度图像
cvtColor(Img,gray_Img,CV_BGR2GRAY);
imwrite("gray_Img.jpg",gray_Img);
namedWindow("Img",WINDOW_AUTOSIZE);
namedWindow("gray_Img",WINDOW_AUTOSIZE);
imshow("Img",Img);
imshow("gray_Img",gray_Img);
waitKey(0);
return 0;
}解析:
1、首先,必须包含所需的库文件及避免命名重复;
2、imread 函数:Loads an image from a file
C++: Mat imread(const
string& filename, int flags=1 )
Python: cv2.imread(filename[,
flags]) → retval
Parameters: filename –
Name of file to be loaded.
flags –Flags
specifying the color type of a loaded image:
CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
CV_LOAD_IMAGE_COLOR
- If set, always convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
>0 Return a 3-channel color image.
Note
In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha
channel).
3、cvtColor
函数:Converts an image from one color space to another.
c++: void cvtColor(InputArray src,
OutputArray dst, int code, int dstCn=0 )
Python: cv2.cvtColor(src,
code[, dst[, dstCn]]) →
dst
Parameters:src –
input image:
8-bit unsigned, 16-bit unsigned ( CV_16UC... ),
or single-precision floating-point. dst –
output image of the same size and depth as src. code –
color space conversion code
(RGB
![](http://docs.opencv.org/2.4/_images/math/1894d1c8d154f0462cc2f2638ba5792087e8a387.png)
GRAY
(CV_BGR2GRAY,CV_RGB2GRAY,CV_GRAY2BGR,CV_GRAY2RGB)
RGB
![](http://docs.opencv.org/2.4/_images/math/1894d1c8d154f0462cc2f2638ba5792087e8a387.png)
YCrCb
(CV_BGR2YCrCb,CV_RGB2YCrCb,CV_YCrCb2BGR,CV_YCrCb2RGB)
RGB
![](http://docs.opencv.org/2.4/_images/math/1894d1c8d154f0462cc2f2638ba5792087e8a387.png)
HSV
(CV_BGR2HSV,CV_RGB2HSV,CV_HSV2BGR,CV_HSV2RGB )). dstCn –
number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code
4、imwrite函数:Saves an image to a specified file.
C++: bool imwrite(const
string& filename, InputArray img,
const vector<int>& params=vector<int>() )
Python: cv2.imwrite(filename,
img[, params]) →
retval
Parameters:filename –
Name of the file. img –
Image to be saved. 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.
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.
For PPM, PGM, or PBM,
it can be a binary format flag ( CV_IMWRITE_PXM_BINARY ),
0 or 1. Default value is 1.
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc,char** argv)
{
Mat Img;//原始图像
Img=imread("1.jpg",CV_LOAD_IMAGE_COLOR);
if(!Img.data)
{
cout<<"could not open"<<endl;
return -1;
}
Mat gray_Img;//灰度图像
cvtColor(Img,gray_Img,CV_BGR2GRAY);
imwrite("gray_Img.jpg",gray_Img);
namedWindow("Img",WINDOW_AUTOSIZE);
namedWindow("gray_Img",WINDOW_AUTOSIZE);
imshow("Img",Img);
imshow("gray_Img",gray_Img);
waitKey(0);
return 0;
}解析:
1、首先,必须包含所需的库文件及避免命名重复;
2、imread 函数:Loads an image from a file
C++: Mat imread(const
string& filename, int flags=1 )
Python: cv2.imread(filename[,
flags]) → retval
Parameters: filename –
Name of file to be loaded.
flags –Flags
specifying the color type of a loaded image:
CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
CV_LOAD_IMAGE_COLOR
- If set, always convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
>0 Return a 3-channel color image.
Note
In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha
channel).
3、cvtColor
函数:Converts an image from one color space to another.
c++: void cvtColor(InputArray src,
OutputArray dst, int code, int dstCn=0 )
Python: cv2.cvtColor(src,
code[, dst[, dstCn]]) →
dst
Parameters:src –
input image:
8-bit unsigned, 16-bit unsigned ( CV_16UC... ),
or single-precision floating-point. dst –
output image of the same size and depth as src. code –
color space conversion code
(RGB
![](http://docs.opencv.org/2.4/_images/math/1894d1c8d154f0462cc2f2638ba5792087e8a387.png)
GRAY
(CV_BGR2GRAY,CV_RGB2GRAY,CV_GRAY2BGR,CV_GRAY2RGB)
RGB
![](http://docs.opencv.org/2.4/_images/math/1894d1c8d154f0462cc2f2638ba5792087e8a387.png)
YCrCb
(CV_BGR2YCrCb,CV_RGB2YCrCb,CV_YCrCb2BGR,CV_YCrCb2RGB)
RGB
![](http://docs.opencv.org/2.4/_images/math/1894d1c8d154f0462cc2f2638ba5792087e8a387.png)
HSV
(CV_BGR2HSV,CV_RGB2HSV,CV_HSV2BGR,CV_HSV2RGB )). dstCn –
number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code
4、imwrite函数:Saves an image to a specified file.
C++: bool imwrite(const
string& filename, InputArray img,
const vector<int>& params=vector<int>() )
Python: cv2.imwrite(filename,
img[, params]) →
retval
Parameters:filename –
Name of the file. img –
Image to be saved. 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.
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.
For PPM, PGM, or PBM,
it can be a binary format flag ( CV_IMWRITE_PXM_BINARY ),
0 or 1. Default value is 1.
相关文章推荐
- OpenCV(C++接口)学习笔记1-图像的读取、显示、保存
- 【OpenCV学习笔记】三、图像读取、显示及保存
- OpenCV(C++接口)学习笔记1-图像读取、显示、保存
- [学习OpenCV攻略][012][读取、修改、保存图像]
- opencv 2.x学习笔记(二)修改和保存图像
- Python OpenCV学习笔记之:图像读取,显示及保存
- OpenCV学习笔记1-读取图像/程序计时
- OpenCV学习笔记(4)视频文件的读取与保存(2010-01-23 更新)
- Opencv学习笔记(2)模块,图像读取、显示、叠加、融合、颜色分离、亮度、对比度
- 【OpenCV学习笔记5】读取图像中任意点的像素值
- OpenCV学习笔记(4)视频文件的读取与保存
- 【OpenCV应用笔记】(彩色/灰度)图像像素值读取并保存到txt文件
- opencv学习笔记2——载入,显示,复制,旋转,保存图像及灰度图
- OpenCV学习笔记---- 挨个读取图像文件夹下的所有文件,进行样本训练
- opencv学习笔记 一 载入、显示和保存图像
- OpenCV之Python学习笔记(1)(2): 图像的载入、显示和保存 图像元素的访问、通道分离与合并
- openCV-Python笔记一:图像的读取、显示和保存
- OpenCV2学习笔记04:图像的读取与显示
- OpenCV学习笔记(一):读取、显示、保存图片
- OpenCV入门_学习笔记_读取、显示、处理图像