【问题标题】:opencv - plot contours in an imageopencv - 在图像中绘制轮廓
【发布时间】:2019-02-12 04:47:07
【问题描述】:

我正在尝试在图像周围绘制轮廓。我可以看到正在找到轮廓,但我无法绘制轮廓。轮廓的颜色似乎是两种(黑色和白色)颜色中的任何一种。

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
%matplotlib inline
im = io.imread('http://matlabtricks.com/images/post-35/man.png')
plt.imshow(im)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
plt.figure()
plt.imshow(imgray)

#Contoured image
ret,thresh = cv2.threshold(imgray, 120,255,cv2.THRESH_BINARY)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
c_img = cv2.drawContours(image, contours, -1, (0, 255, 0), 1)
plt.figure()
plt.imshow(c_img)

【问题讨论】:

    标签: python opencv image-processing computer-vision opencv-contour


    【解决方案1】:

    您需要在原始图像上绘制,而不是在从findContuors() 返回的图像上。以下工作。

    # Contoured image
    ret,thresh = cv2.threshold(imgray, 120,255,cv2.THRESH_BINARY)
    contours = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2]
    for contour in contours:
       cv2.drawContours(im, contour, -1, (0, 255, 0), 3)
    plt.figure()
    plt.imshow(im)
    

    【讨论】:

      【解决方案2】:

      这是我的结果:


      ## Read and convert 
      img = io.imread('http://matlabtricks.com/images/post-35/man.png')
      gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
      
      ## Find outer contours 
      _, cnts, _= cv2.findContours(gray,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
      
      ## Draw 
      canvas = np.zeros_like(img)
      cv2.drawContours(canvas , contours, -1, (0, 255, 0), 1)
      
      plt.imshow(canvas)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-20
        • 2021-09-07
        • 2017-11-06
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-17
        相关资源
        最近更新 更多