膨脹 dilate

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    #include <iostream>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/opencv.hpp>
     
    using namespace cv ;
    using namespace std ;
    Mat src; Mat src_gray;
    int thresh = 100;
    int max_thresh = 255;
    RNG rng(12345);
    void thresh_callback(int, void* );
    int main( int, char** argv )
    {
        src = cv::imread("/Users/powenko/Desktop/apple.png");
        if (src.empty())
        {
            cerr << "No image supplied ..." << endl;
            return -1;
        }
        const char* source_window = "Source";
        namedWindow( source_window, WINDOW_AUTOSIZE );
        imshow( source_window, src );
        createTrackbar( " erode thresh:", "Source", &thresh, max_thresh, thresh_callback );
        thresh_callback( 0, 0 );
        waitKey(0);
        return(0);
    }
    void thresh_callback(int, void* )
    {
        Mat canny_output;
        vector<vector<Point> > contours;
        vector<Vec4i> hierarchy;
        //kernel的形狀----矩形: MORPH_RECT  交叉形: MORPH_CROSS   椭圆形 : MORPH_ELLIPSE
        Mat drawing;
        if(thresh>0){
            Mat element = getStructuringElement(MORPH_RECT,  Size(thresh,thresh));
            dilate(src, drawing, element);
        }else{
            drawing=src;
             
        }
        namedWindow( "Contours", WINDOW_AUTOSIZE );
        imshow( "Contours", drawing );
    }

    screen-shot-2016-11-28-at-12-11-45-pm