zoukankan      html  css  js  c++  java
  • 【python+opencv】轮廓发现

    python+opencv---轮廓发现

    轮廓发现---是基于图像边缘提取的基础寻找对象轮廓的方法,

    所有边缘提取的阈值选定会影响最终轮廓发现的结果。

    介绍两种API使用:

    -cv.findContours 发现轮廓

    -cv.drawContours 绘制轮廓

    *利用梯度避免阈值烦恼

    效果图:

    使用边缘Canny()提取边缘

    高斯模糊+灰度+全局阈值--->二值化图像

    代码:

    import cv2 as cv
    import numpy as np
    
    def edge_demo(image):
        #先将图像高斯模糊去噪
        blurred = cv.GaussianBlur(image,(3,3),0)
        #图像灰度化
        gray = cv.cvtColor(blurred,cv.COLOR_BGR2GRAY)
        #图像边缘提取
        edge_output = cv.Canny(gray,50,150)
        cv.imshow("CannyEdge",edge_output)
        
        #彩色边线
        # dst = cv.bitwise_and(image,gray,mask=edge_output)
        # cv.imshow("Color Edge",dst)
    
        return edge_output
    
    """
        findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy
        drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]])  -> image 
    """
    def contours_demo(image):
        """
        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_image",binary)
        """
        binary = edge_demo(image)
    
        cloneImage,contours,hierarchy = cv.findContours(binary,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
        for i ,contour in enumerate(contours):
            cv.drawContours(image,contours,i,(255,0,0),-2)
            print(i)
        cv.imshow("detect contours",image)
    
    
    src = cv.imread('circles.jpg')
    cv.namedWindow('input_image',cv.WINDOW_AUTOSIZE)
    cv.imshow('input_image',src)
    
    contours_demo(src)
    # edge_demo(src)
    
    cv.waitKey(0)
    cv.destroyAllWindows()

    注意:

    1.Opencv发现轮廓的函数原型为:

    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy

    image参数表示8位单通道图像矩阵,可以是灰度图

    gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)

    ,但更常用的是二值图像

    ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)

    ,一般是经过Canny、拉普拉斯等边缘检测算子处理过的二值图像。

    mode参数表示轮廓检索模式:

    ①CV_RETR_EXTERNAL:只检测最外围轮廓,包含在外围轮廓内的内围轮廓被忽略。

    ②CV_RETR_LIST:检测所有的轮廓,包括内围、外围轮廓,但是检测到的轮廓不建立等级关系,彼此之间独立,没有等级关系,这就意味着这个检索模式下不存在父轮廓或内嵌轮廓。

    ③CV_RETR_CCOMP:检测所有的轮廓,但所有轮廓只建立两个等级关系,外围为顶层,若外围内的内围轮廓还包含了其他的轮廓信息,则内围内的所有轮廓均归属于顶层。

    ④CV_RETR_TREE:检测所有轮廓,所有轮廓建立一个等级树结构,外层轮廓包含内层轮廓,内层轮廓还可以继续包含内嵌轮廓。

    method参数表示轮廓的近似方法:

    ①CV_CHAIN_APPROX_NONE 存储所有的轮廓点,相邻的两个点的像素位置差不超过1,即max (abs (x1 - x2), abs(y2 - y1) == 1。

    ②CV_CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息。

    ③CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法。

    contours参数是一个list,表示存储的每个轮廓的点集合。

    hierarchy参数是一个list,list中元素个数和轮廓个数相同,每个轮廓contours[i]对应4个hierarchy元素hierarchy[i][0] ~hierarchy[i][3],

    分别表示后一个轮廓、前一个轮廓、父轮廓、内嵌轮廓的索引编号,如果没有对应项,则该值为负数。

    offset参数表示每个轮廓点移动的可选偏移量。

    2.Opencv绘制轮廓的函数原型为:

    drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]]) -> image

    imgae参数表示目标图像。

    contours参数表示所有输入轮廓。

    contourIdx参数表示绘制轮廓list中的哪条轮廓, 如果是负数,则绘制所有轮廓。

    color参数表示轮廓的颜色。

    thickness参数表示绘制的轮廓线条粗细,如果是负数,则填充轮廓内部

    lineType参数表示线型。

    hierarchy参数表示有关层次结构的可选信息。

    maxLevel参数表示绘制轮廓的最大级别。 如果为0,则仅绘制指定的轮廓。 如果为1,则该函数绘制轮廓和所有嵌套轮廓。

    如果为2,则该函数绘制轮廓,所有嵌套轮廓,所有嵌套到嵌套的轮廓,等等。 仅当有可用的层次结构时才考虑此参数。

    offset参数表示可选的轮廓偏移参数,该参数可按指定的方式移动所有绘制的轮廓。

    参考:

    https://blog.csdn.net/on2way/article/details/46812121

  • 相关阅读:
    Connected Graph
    Gerald and Giant Chess
    [NOI2009]诗人小G
    四边形不等式小结
    [NOI2007]货币兑换
    Cats Transport
    Cut the Sequence
    Fence
    The Battle of Chibi
    [Usaco2005 Dec]Cleaning Shifts
  • 原文地址:https://www.cnblogs.com/XJT2018/p/9952529.html
Copyright © 2011-2022 走看看