您的位置:首页 > 运维架构

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 

 GRAY
(CV_BGR2GRAY,CV_RGB2GRAY,CV_GRAY2BGR,CV_GRAY2RGB)
 
                 RGB 

 YCrCb
(CV_BGR2YCrCb,CV_RGB2YCrCb,CV_YCrCb2BGR,CV_YCrCb2RGB)
 
                 RGB 

 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