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

opencv入门-函数说明

2018-01-07 17:22 309 查看

函数说明

imread

getStructuringElement

erode

dilate

imread

官方说明一句话就是加载支持格式的图片

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 \*.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)


函数原型

CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );


参数说明

@param filename Name of file to be loaded.
@param flags Flag that can take values of cv::ImreadModes


第二个参数有以下几种格式

//! 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. >0 加载BRG三个通道的图

2. =0 加载单通道的图

3. <0 加载Alpha通道的图,默认为1

- 示例

Mat srcimg = imread(filename);


getStructuringElement

官方说明:返回指定大小和形状的元素操作

Returns a structuring element of the specified size and shape for morphological operations.

参数说明

@param shape Element shape that could be one of cv::MorphShapes

@param ksize Size of the structuring element.

@param anchor Anchor position within the element. The default value \f(−1,−1)\f means that the

anchor is at the center. Note that only the shape of a cross-shaped element depends on the anchor

position. In other cases the anchor just regulates how
ca2b
much the result of the morphological

operation is shifted.

第一个参数

MorphShapes有以下几种

矩形: MORPH_RECT

交叉形: MORPH_CROSS

椭圆形: MORPH_ELLIPSE

第二个参数

就是内核的大小

第三个锚点的位置(-1,-1)表示中心位置,只有十字中心锚点有作用其它的只是形状转换操作

- 示例

Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));


erode 操作结果就是高亮的部份会变小

官方说明:用指定的核元素来腐蚀源图片

The function erodes the source image using the specified structuring element that determines the

shape of a pixel neighborhood over which the minimum is taken:
参数说明

@param src input image; the number of channels can be arbitrary, but the depth should be one of

CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.

@param dst output image of the same size and type as src.

//核,可以用getStructuringElement自定义

@param kernel structuring element used for erosion; if
element=Mat()
, a
3 x 3
rectangular

structuring element is used. Kernel can be created using getStructuringElement.

//默认中心

@param anchor position of the anchor within the element; default value (-1, -1) means that the

anchor is at the element center.

//迭代次数

@param iterations number of times erosion is applied.

//边界模式

@param borderType pixel extrapolation method, see cv::BorderTypes

//边界值

@param borderValue border value in case of a constant border

@sa dilate, morphologyEx, getStructuringElement

示例

erode(srcimg, dstimg, element);
```
## dilate 膨胀操作,就是高亮的部分会放大
- 官方说明
> The function dilates the source image using the specified structuring element that determines the
shape of a pixel neighborhood over which the maximum is taken:
- 参数说明
>@param src input image; the number of channels can be arbitrary, but the depth should be one of
CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
@param dst output image of the same size and type as src\`.
@param kernel structuring element used for dilation; if elemenat=Mat(), a 3 x 3 rectangular
structuring element is used. Kernel can be created using getStructuringElement
@param anchor position of the anchor within the element; default value (-1, -1) means that the
anchor is at the element center.
@param iterations number of times dilation is applied.
@param borderType pixel extrapolation method, see cv::BorderTypes
@param borderValue border value in case of a constant border
- 示例


dilate(srcimg, dstimg, element);


最后整合后的例子

//显示图片
int showimg( const string& filename)
{
Mat img = imread(filename,0);
imshow("test", img);
waitKey();
return 0;
}
//腐蚀操作
int erodeimg(const string & filename)
{
Mat srcimg = imread(filename);
imshow("sourceimg", srcimg);
//进行腐蚀操作
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
Mat dstimg;
erode(srcimg, dstimg, element);
imshow("erodeimg", dstimg);
//waitKey(0);
return 0;
}
//进行膨胀操作
int dilateimg(const string & filename)
{
Mat srcimg = imread(filename);
imshow("sourceimg", srcimg);
//进行膨胀操作
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
Mat dstimg;
dilate(srcimg, dstimg, element);
imshow("dilateimg", dstimg);
waitKey(0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息