OPENCV 3. 图像的加载、保存与显示

a. 加载图像

CVAPI(IplImage*) cvLoadImage( const char* filename, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR));

第2个参数为加载图像的方式,如灰度图像,原图等;

//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
//};

如果想以图像最原始的状态进行加载,则可设该值为:

CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR

b.保存文件

/* save image to file */
CVAPI(int) cvSaveImage( const char* filename, const CvArr* image,
                        const int* params CV_DEFAULT(0) );

c.显示图像

/* create window */
CVAPI(int) cvNamedWindow( const char* name, int flags CV_DEFAULT(CV_WINDOW_AUTOSIZE) );

name:窗口的标志,如果已经有同名窗口,则该函数什么也不干。

/* display image within window (highgui windows remember their content) */
CVAPI(void) cvShowImage( const char* name, const CvArr* image );
/* wait for key event infinitely (delay<=0) or for "delay" milliseconds */
CVAPI(int) cvWaitKey(int delay CV_DEFAULT(0));

显示一幅图像时,需要等待一定的时间,否则窗口会一闪而过而来不及看清;

下面的例子加载一副图像,并在指定位置插入一幅LOGO的图像:

#include  
#include   
#include   

using namespace cv;  

int main()  
{ 
    // 加载两幅图像
    IplImage * pData = cvLoadImage("D:\\Image\\Desert.jpg", CV_LOAD_IMAGE_COLOR);
    IplImage * pLogo = cvLoadImage("D:\\Image\\Penguins.jpg", CV_LOAD_IMAGE_COLOR);

    //显示这两幅图像
    cvNamedWindow("Desert",CV_WINDOW_AUTOSIZE );
    cvShowImage("Desert", pData);
    cvNamedWindow("pLogo",CV_WINDOW_AUTOSIZE );
    cvShowImage("pLogo", pData);

    // 设置ROI区域,进行拷贝和还原ROI区域
    cvSetImageROI(pData, cvRect(10, 10, pLogo->width, pLogo->height));
    cvCopy(pLogo, pData);
    cvResetImageROI(pData);  

    // 保存图像
    int p[3] = {CV_IMWRITE_JPEG_QUALITY, 100, 0}; 
    cvSaveImage("D:\\Image\\Desert-Penguins.jpg", pData, p);

    // 显示图像
    cvNamedWindow("desert",CV_WINDOW_AUTOSIZE );    
    cvShowImage("desert", pData);   
    cvWaitKey(0);

    // 释放内存资源
    cvReleaseImage(&pData);
    cvReleaseImage(&pLogo);

    return 0;
} 

你可能感兴趣的:(OPENCV 3. 图像的加载、保存与显示)