使用OpenCV在对象周围绘制轮廓

时间:2019-06-22 00:15:26

标签: python python-3.x image opencv image-processing

我正在尝试在两个重叠的对象上绘制轮廓。在这里,我拍了两支笔的照片。 但是它不能完美地绘制轮廓。里面有一些小轮廓。如何删除它?

这是我的原始照片

enter image description here

和结果

enter image description here

    bootstrap_css:
        inputs:
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/less/bootstrap.less"
        filters:
            - lessphp
            - cssrewrite
        output: css/bootstrap.css

    bootstrap_js:
        inputs:
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/affix.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/alert.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/button.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/carousel.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/collapse.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/dropdown.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/modal.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/tooltip.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/popover.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/scrollspy.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/tab.js"
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/transition.js"
        filters: [?yui_js]
        output: js/bootstrap.js
    fonts_glyphicons_eot:
        inputs:
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/fonts/glyphicons-halflings-regular.eot"
        output: "fonts/glyphicons-halflings-regular.eot"
    fonts_glyphicons_svg:
        inputs:
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/fonts/glyphicons-halflings-regular.svg"
        output: "fonts/glyphicons-halflings-regular.svg"
    fonts_glyphicons_ttf:
        inputs:
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/fonts/glyphicons-halflings-regular.ttf"
        output: "fonts/glyphicons-halflings-regular.ttf"
    fonts_glyphicons_woff:
        inputs:
            - "%kernel.root_dir%/../vendor/twbs/bootstrap/fonts/glyphicons-halflings-regular.woff"

        output: "fonts/glyphicons-halflings-regular.woff"

1 个答案:

答案 0 :(得分:1)

这是获取轮廓的一种潜在方法

  • 将图像转换为灰度并模糊图像
  • 获取二进制图像的阈值
  • 找到轮廓
  • 遍历轮廓并使用最小轮廓区域进行过滤
  • 绘制轮廓

阈值图像

enter image description here

找到轮廓

enter image description here

请注意,在此图像中,检测到笔内的小轮廓和纸张中不需要的轮廓。您的问题是如何去除内部的小轮廓。有两种解决方案。一种是使用cv2.RETR_EXTERNAL而不是cv2.RETR_TREE,第二种是使用cv2.contourArea()按其面积过滤出小的轮廓。进行这些更改之后,结果如下

enter image description here

import cv2

image = cv2.imread('1.jpg')
blur = cv2.medianBlur(image, 9)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 110 ,255, cv2.THRESH_BINARY_INV)[1]

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

min_area = 5000
for c in cnts:
    area = cv2.contourArea(c)
    if area > min_area:
        cv2.drawContours(image,[c], 0, (36,255,12), 2)

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey(0)
相关问题