opencv的图像载入、显示和输出

来源:互联网 发布:linux查看服务列表 编辑:程序博客网 时间:2024/05/17 05:57

主要讲解:imread   imshow   namedWindow    imwrite    函数


1、关于opencv的命名空间

      opencv中的C++类或者函数类都是定义在命名空间cv中的,两种方法可以访问,一种是在代码开头位置加上using namespace cv;

另外一种是在使用函数时加cv::.

2、关于Mat类型

      Mat类是用于保存图像以及其他矩阵数据的数据结构。

3、图像的读入与显示

      3.1    imread函数

 Loads an image from a file.

C++: Mat imread(const String& filename, int flags=IMREAD_COLOR )
Python: cv2.imread(filename[, flags]) → retval
C: IplImage* cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
C: CvMat* cvLoadImageM(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
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).

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ). Currently, the following file formats are supported:

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
  • JPEG 2000 files - *.jp2 (see the Notes section)
  • Portable Network Graphics - *.png (see the Notes section)
  • WebP - *.webp (see the Notes section)
  • Portable image format - *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Notes section)

Note

  • The function determines the type of an image by the content, not by the file extension.
  • On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image (libjpeg, libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware that currently these native image loaders give images with different pixel values because of the color management embedded into MacOSX.
  • On Linux*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for codecs supplied with an OS image. Install the relevant packages (do not forget the development files, for example, “libjpeg-dev”, in Debian* and Ubuntu*) to get the codec support or turn on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.

Note

 

In the case of color images, the decoded images will have the channels stored in B G R order.

  Mat img=imread("lena.jpg",CV_LOAD_IMAGE_ANYDEPTH );//载入最真实的图像

   Mat img=imread("lena.jpg",0);//灰度图像

   Mat  img=imread("lena.jpg",1);//载入三通道的彩色图像

   Mat  img=imread("lena.jpg");


3.2   namedWindow函数

namedWindow

Creates a window.

C++: void namedWindow(const String& winname, int flags=WINDOW_AUTOSIZE )
Python: cv2.namedWindow(winname[, flags]) → None
C: int cvNamedWindow(const char* name, int flags=CV_WINDOW_AUTOSIZE )
Parameters:
  • name – Name of the window in the window caption that may be used as a window identifier.
  • flags –

    Flags of the window. The supported flags are:

    • WINDOW_NORMAL If this is set, the user can resize the window (no constraint).
    • WINDOW_AUTOSIZE If this is set, the window size is automatically adjusted to fit the displayed image (see imshow() ), and you cannot change the window size manually.
    • WINDOW_OPENGL If this is set, the window will be created with OpenGL support.

The function namedWindow creates a window that can be used as a placeholder for images and trackbars. Created windows are referred to by their names.

If a window with the same name already exists, the function does nothing.

You can call destroyWindow() or destroyAllWindows() to close the window and de-allocate any associated memory usage. For a simple program, you do not really have to call these functions because all the resources and windows of the application are closed automatically by the operating system upon exit.

Note

 

Qt backend supports additional flags:

  • CV_WINDOW_NORMAL or CV_WINDOW_AUTOSIZE: CV_WINDOW_NORMAL enables you to resize the window, whereas CV_WINDOW_AUTOSIZE adjusts automatically the window size to fit the displayed image (see imshow() ), and you cannot change the window size manually.
  • CV_WINDOW_FREERATIO or CV_WINDOW_KEEPRATIO: CV_WINDOW_FREERATIO adjusts the image with no respect to its ratio, whereas CV_WINDOW_KEEPRATIO keeps the image ratio.
  • CV_GUI_NORMAL or CV_GUI_EXPANDED: CV_GUI_NORMAL is the old way to draw the window without statusbar and toolbar, whereas CV_GUI_EXPANDED is a new enhanced GUI.

By default, flags == CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED





   void  namedWindow(const string&winname,int flags=WINDOW_AUTOSIZE);

       WINDOW_NORMAL设置了这个值,用户可以改变窗口的大小(没有限制)

   WINDOW_AUTOSIZE如果设置了这个值,窗口大小会自动调整以适应所显示的图像,但不能手动改变窗口大小

   WINDOW_OPENGL 如果设置这个值的话,窗口创建的时候便会支持OPENGL

函数剖析:

首先需要注意的是,它有默认值WINDOW_AUTOSIZE,所以,一般情况下,这个函数我们填一个变量就行了。

namedWindow函数的作用是,通过指定的名字,创建一个可以作为图像和进度条的容器窗口。如果具有相同名称的窗口已经存在,则函数不做任何事情。

我们可以调用destroyWindow()或者destroyAllWindows()函数来关闭窗口,并取消之前分配的与窗口相关的所有内存空间。

但话是这样说,其实对于代码量不大的简单小程序来说,我们完全没有必要手动调用上述的destroyWindow()或者destroyAllWindows()函数,因为在退出时,所有的资源和应用程序的窗口会被操作系统会自动关闭。

3.3imshow函数

void imshow(const string&winname, InputArray mat); 

imshow 函数详解:

imshow 函数用于在指定的窗口中显示图像。如果窗口是用CV_WINDOW_AUTOSIZE(默认值)标志创建的,那么显示图像原始大小。否则,将 图像进行缩放以适合窗口。而imshow 函数缩放图像,取决于图像的深度:

如果载入的图像是8位无符号类型(8-bit unsigned),就显示图像本来的样子

16位无符号类型(16-bit unsigned)或32位整型(32-bit integer),便用像素值除以256。也就是说,值的范围是[0,255 x 256]映射到[0,255]

如果图像是32位浮点型(32-bit floating-point),像素值便要乘以255。也就是说,该值的范围是[0,1]映射到[0,255]。


3.4输出图像到文件———imwrite函数

bool imwrite(const string&filename,InputArray img,const vector<int>& params=vector<int>());

 第一个参数,const string&类型的filename,填需要写入的文件名就行了,带上后缀,比如,“123.jpg”这样。

 第二个参数,InputArray类型的img,一般填一个Mat类型的图像数据就行了。

 第三个参数,const vector<int>&类型的params,表示为特定格式保存的参数编码,它有默认值vector<int>(),所以一般情况下不需要填写。而如果要填写的话,有下面这些需要了解的地方:

  1、对于JPEG格式的图片,这个参数表示从0到100的图片质量(CV_IMWRITE_JPEG_QUALITY),默认值是95.

2.、 对于PNG格式的图片,这个参数表示压缩级别(CV_IMWRITE_PNG_COMPRESSION)从0到9。较高的值意味着更小的尺寸和更长的压缩时间,而默认值是3。

3、 对于PPM,PGM,或PBM格式的图片,这个参数表示一个二进制格式标志(CV_IMWRITE_PXM_BINARY),取值为0或1,而默认值是1。

函数解析:

imwrite函数用于将图像保存到指定的文件。图像格式是基于文件扩展名的,可保存的扩展名和imread中可以读取的图像扩展名一样,为了方便查看,我们在这里再列一遍:

    • Windows位图 - *.bmp, *.dib
    • JPEG文件 - *.jpeg, *.jpg, *.jpe
    • JPEG 2000文件- *.jp2
    • PNG图片 - *.png
    • 便携文件格式- *.pbm, *.pgm, *.ppm
    • Sun rasters光栅格式 - *.sr, *.ras
    • TIFF 文件 - *.tiff, *.tif

0 0
原创粉丝点击