opencv源碼學習: getStructuringElement函數;

來源:互聯網
上載者:User

標籤:mask   color   rate   enter   源碼   size   nbsp   exp   spec   

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<int>(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;}

 

opencv源碼學習: getStructuringElement函數;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.