美文网首页
20、轮廓发现

20、轮廓发现

作者: BigBigGuy | 来源:发表于2019-01-08 16:46 被阅读0次
    image.png
    def contours_function(image):
        '''
            1、高斯模糊
            2、灰度图像
            3、全局二值化
            4、FindCoutours:找出轮廓
        '''
        dst = cv.GaussianBlur(image,(3,3),0)
        gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
        ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY| cv.THRESH_OTSU)
        cv.imshow("Binary", binary)
    
        '''
           cv.findContours:
                mode = cv.RETR_EXTERNAL:表示找出最外层的轮廓
                        cv.RETR_TREE:表示找出所有轮廓
        '''
        cloneImage, contours, hierachy = cv.findContours(
            binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        for i, contour in enumerate(contours):
            cv.drawContours(image, contours, i, (0, 0, 255), 2)
            print(i)
        cv.imshow("Detect_contours", image)
    
    mode = cv.RETR_TREE
    mode = cv.RETR_EXTERNAL

    轮廓填充

    # thickness = -1
     for i, contour in enumerate(contours):
        cv.drawContours(image, contours, i, (0, 0, 255), -1)
    
    
    image.png

    基于Canny边缘提取

    def canny_edge_function(image):
        '''
            Canny边缘提取算法:
                1、进行高斯模糊:因为Canny对噪声比较敏感,所以先高斯模糊降噪
                2、灰度转移:转化为单通道
                3、计算梯度:Sobel/Scharr
                4、
                5、高低阈值输出二值图像
        '''
        blurred = cv.GaussianBlur(image, (3, 3), 0)
        gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
    
        grad_x = cv.Sobel(gray, cv.CV_16SC1, 1, 0)
        grad_y = cv.Sobel(gray, cv.CV_16SC1, 0, 1)
    
        #低阈值:50;高阈值:150
        edge_output = cv.Canny(grad_x,grad_y,50,150)
        cv.imshow("Binary",edge_output)
        return edge_output
    
    def contours_function(image):
        binary = canny_edge_function(image)
        cloneImage, contours, hierachy = cv.findContours(
            binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
        for i, contour in enumerate(contours):
            cv.drawContours(image, contours, i, (0, 0, 255), -1)
            print(i)
        cv.imshow("Detect_contours", image)
    
    
    基于Canny边缘提取

    相关文章

      网友评论

          本文标题:20、轮廓发现

          本文链接:https://www.haomeiwen.com/subject/plhbrqtx.html