用OPENCV打开并显示图像是需要简单的两个函数就可以实现

直接上代码:

BOOL CAutoImage::Openshow(CString strFullpath)

{

    cv::String tstr = strFullpath.GetBuffer();

    cv::Mat mImage  = cv::imread(tstr);

    if (mImage .data == NULL)

    {//打开失败

        return 1;

    }  

    cv:: imshow("test", mImage);

waitKey();

    return 0;

}

慢慢解说:

函数1: imread

函数原型:Mat imread(const String& filename,int flags = IMREAD_COLOR);

  返回Mat对象;

参数filename: 待打开图片的绝对地址,需要注意的是,并不是所有文件都可以用它打开,它支持的文件如下;函数识别不是依靠文件的后缀名,而是依靠内容的编码格式;

需要注意的是imread读取数据时会重新排列数据。

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 *.pxm, *.pnm (always supported)

Sun rasters -                                             *.sr, *.ras (always supported)

TIFF files -                                                  *.tiff, *.tif (see the Notes section)

OpenEXR Image files -                           *.exr (see the Notes section)

Radiance HDR -                                       *.hdr, *.pic (always supported)

Raster and Vector geospatial data supported by Gdal (see the Notes section)

  参数flags:打开的参数,这个非常重要,因为如果设置不合适的话,很容易出现预想之外的效果。它可以指导将原图读取时进行一定的转换。默认值是IMREAD_LOAD_GDAL。因此,如果是想直接处理原图,应该设置为IMREAD_UNCHANED。

         IMREAD_UNCHANGED

If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).

         IMREAD_GRAYSCALE

If set, always convert image to the single channel grayscale image.

IMREAD_COLOR

If set, always convert image to the 3 channel BGR color image.

IMREAD_ANYDEPTH

If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.

IMREAD_ANYCOLOR

If set, the image is read in any possible color format.

IMREAD_LOAD_GDAL

If set, use the gdal driver for loading the image.

IMREAD_REDUCED_GRAYSCALE_2

If set, always convert image to the single channel grayscale image and the image size reduced 1/2.

IMREAD_REDUCED_COLOR_2

If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.

IMREAD_REDUCED_GRAYSCALE_4

If set, always convert image to the single channel grayscale image and the image size reduced 1/4.

IMREAD_REDUCED_COLOR_4

If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.

IMREAD_REDUCED_GRAYSCALE_8

If set, always convert image to the single channel grayscale image and the image size reduced 1/8.

IMREAD_REDUCED_COLOR_8

If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.

函数2: imshow

函数原型:void::imshow ( const String & winname,InputArray mat)

返回结果:无

参数winname:显示窗口名称

参数mat:需要显示的图像对象,需要注意:根据图像的深度,imshow函数会自动对其显示灰度值进行缩放,规则如下:

  1. 如果图像数据类型是8U8位无符号),则直接显示;
  2. 如果图像数据类型是16U16位无符号)或32S32位有符号整数),则imshow函数内部会自动将每个像素值除以256并显示,即将原图像素值的范围由[0~255*256]映射到[0~255];
  3. 如果图像数据类型是32F32位浮点数)或64F64位浮点数),则imshow函数内部会自动将每个像素值乘以255并显示,即将原图像素值的范围由[0~1]映射到[0~255](注意:原图像素值必须要归一化)

备注:

imshow以后需要调用waitKey()函数,不然窗口就会一闪而过!

你可能感兴趣的:(机器视觉,opencv)