opencv基础(1)——图像读、写、显示

opencv简介:OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。
OpenCV用C++语言编写,它的主要接口也是C++语言,但是依然保留了大量的C语言接口。该库也有大量的Python、Java and MATLAB/OCTAVE(版本2.5)的接口。这些语言的API接口函数可以通过在线文档获得。如今也提供对于C#、Ch、Ruby,GO的支持。
所有新的开发和算法都是用C++接口。一个使用CUDA的GPU接口也于2010年9月开始实现。

1、从硬盘读取图片和显示图片

1.1、IplImage格式

    //读取图片
    IplImage *pSrc = cvLoadImage("/home/mark/Desktop/opencv.jpg", CV_LOAD_IMAGE_COLOR);
    if (pSrc == NULL) return 0;

    // 显示图片
    cvNamedWindow("111", CV_WINDOW_AUTOSIZE); //命名显示图像窗口
    cvShowImage("111", pSrc); //显示图片到指定串口
    cvWaitKey(0);//程序停止

    cvReleaseImage(&pSrc);//释放内存

cvLoadImage函数加载图像格式:
enum
{
/* 8bit, color or not */
    CV_LOAD_IMAGE_UNCHANGED  =-1, 
/* 8bit, gray */
    CV_LOAD_IMAGE_GRAYSCALE  =0,
/* ?, color */
    CV_LOAD_IMAGE_COLOR      =1,
/* any depth, ? */
    CV_LOAD_IMAGE_ANYDEPTH   =2,
/* ?, any color */
    CV_LOAD_IMAGE_ANYCOLOR   =4,
/* ?, no rotate */
    CV_LOAD_IMAGE_IGNORE_ORIENTATION  =128
};

1.2、Mat格式

   //读取图片
    Mat mat = imread("/home/mark/Desktop/opencv.jpg", IMREAD_COLOR);

    //显示图片
    namedWindow("111", WINDOW_AUTOSIZE);
    imshow("111", mat);//显示图片到指定串口
    waitKey();//程序等待

imread函数加载图像格式:
//! Imread flags
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.
    IMREAD_REDUCED_GRAYSCALE_2  = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
    IMREAD_REDUCED_COLOR_2      = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
    IMREAD_REDUCED_GRAYSCALE_4  = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
    IMREAD_REDUCED_COLOR_4      = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
    IMREAD_REDUCED_GRAYSCALE_8  = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
    IMREAD_REDUCED_COLOR_8      = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
    IMREAD_IGNORE_ORIENTATION   = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
};

1.3、测试结果。

2、保存图片

2.1、IplImage格式。

cvSaveImage("/home/mark/Desktop/save.jpg", pSrc);

2.2 Mat格式。

imwrite("/home/mark/Desktop/save.jpg", mat);

3、总结:

3.1、cvLoadImageimread支持的图片格式有:Windows位图文件 - BMP, DIB;JPEG文件 - JPEG, JPG, JPE;便携式网络图片 - PNG;便携式图像格式 - PBM,PGM,PPM;Sun rasters - SR,RAS;TIFF文件 - TIFF,TIF;OpenEXR HDR 图片 - EXR;JPEG 2000 图片- jp2。

3.2、cvSaveImageimwrite可以保存opnecv所支持的所有图片格式。

3.3、IplImage格式的图像需要手动释放内存,而Mat格式的图像不用手动释放内存,Mat有自动内存回收机制。

猜你喜欢

转载自blog.csdn.net/cwj066/article/details/82881100