博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
opencv源码学习: getStructuringElement函数;
阅读量:4700 次
发布时间:2019-06-09

本文共 2590 字,大约阅读时间需要 8 分钟。

getStructuringElement函数归属于形态学,可以建立指定大小、形状的结构;

原型: 

/** @brief Returns a structuring element of the specified size and shape for morphological operations.The function constructs and returns the structuring element that can be further passed to cv::erode,cv::dilate or cv::morphologyEx. But you can also construct an arbitrary binary mask yourself and use it asthe structuring element.@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 theanchor is at the center. Note that only the shape of a cross-shaped element depends on the anchorposition. In other cases the anchor just regulates how much the result of the morphologicaloperation is shifted. */CV_EXPORTS_W Mat getStructuringElement(int shape, Size ksize, Point anchor = Point(-1,-1));

源码解析:

cv::Mat cv::getStructuringElement(int shape, Size ksize, Point anchor){    int i, j;    int r = 0, c = 0;    double inv_r2 = 0;    CV_Assert( shape == MORPH_RECT || shape == MORPH_CROSS || shape == MORPH_ELLIPSE );        //目前支持三种形状的单元创建: 矩形, 十字形, 椭圆形;    anchor = normalizeAnchor(anchor, ksize);                    //当默认为-1,-1时, 计算anchor;    if( ksize == Size(1,1) )                  //当给定大小为1,1时,表明是一个点, 可以用矩形来表示;        shape = MORPH_RECT;    if( shape == MORPH_ELLIPSE )               //椭圆;    {        r = ksize.height/2;        c = ksize.width/2;        inv_r2 = r ? 1./((double)r*r) : 0;    }    Mat elem(ksize, CV_8U);    for( i = 0; i < ksize.height; i++ )                    //对每一行,计算0,1的范围;    {        uchar* ptr = elem.ptr(i);        int j1 = 0, j2 = 0;        if( shape == MORPH_RECT || (shape == MORPH_CROSS && i == anchor.y) )        //矩形,或十字y锚点时  j2为ksize.width;            j2 = ksize.width;        else if( shape == MORPH_CROSS )            j1 = anchor.x, j2 = j1 + 1;        else                                               //椭圆;        {            int dy = i - r;            if( std::abs(dy) <= r )            {                int dx = saturate_cast
(c*std::sqrt((r*r - dy*dy)*inv_r2));        //计算得到x的偏移; j1 = std::max( c - dx, 0 ); j2 = std::min( c + dx + 1, ksize.width ); } } for( j = 0; j < j1; j++ )                //从这三个for可以看出, (0,j1)之间为 0, (j1, j2)之间为1, (j2, ksize.width)之间为0; ptr[j] = 0; for( ; j < j2; j++ ) ptr[j] = 1; for( ; j < ksize.width; j++ ) ptr[j] = 0; } return elem;}

 

转载于:https://www.cnblogs.com/yinwei-space/p/9833294.html

你可能感兴趣的文章
"ORA-00942: 表或视图不存在 "的原因和解决方法[转]
查看>>
Oauth支持的5类 grant_type 及说明
查看>>
LeetCode - Same Tree
查看>>
Python dict get items pop update
查看>>
[置顶] 程序员必知(二):位图(bitmap)
查看>>
130242014036-(2)-体验敏捷开发
查看>>
constexpr
查看>>
Nginx 流量和连接数限制
查看>>
IE8/9 本地预览上传图片
查看>>
Summary of CRM 2011 plug-in
查看>>
安全漏洞之Java
查看>>
Oracle 组函数count()
查看>>
Session的使用过程中应注意的一个小问题
查看>>
SDK,API,DLL名词解释
查看>>
试探算法
查看>>
jquery.validation.js 使用
查看>>
Nginx 相关介绍
查看>>
leetcode[33]Search in Rotated Sorted Array
查看>>
eval(PHP 4, PHP 5)
查看>>
readelf用法小记
查看>>