使用OpenCV从图像中提取轮廓

时间:2017-01-28 19:03:40

标签: python opencv

我有一个图像,很少有交叉线产生四个多边形。我想测量每个多边形的面积。在我的脚本中,我正在尝试创建轮廓,并且可能使用了错误的方法。我很感激任何建议...... source image

import cv2
import numpy as np
import time


# define range of  color in HSV
lower_red = np.array([150,135,160])
upper_red = np.array([180,250,200])
white_line  = np.array([255,255,255])
red_line   = np.array([0,0,255])



im = cv2.imread('laser.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cv2.imshow('imgray', im)
ret,thresh = cv2.threshold(imgray,127,255,0)
cv2.imshow('thresh', thresh)

# Remove some small noise if any.
dilate = cv2.dilate(thresh,None)
erode = cv2.erode(dilate,None)
cv2.imshow('erode', erode)


_, contours, hierarchy = cv2.findContours(erode,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
##for contour in contours:
##    if (cv2.contourArea(contour) > 50):  
##        print contours
im2 =  np.zeros((500,500,3), np.uint8)
cv2.drawContours(im2,contours,0,(125,125,0),1)
cv2.imshow('contours',im2)


k = cv2.waitKey(0)
if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
    cv2.imwrite('messigray.png',im2)
    cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

要使用当前代码提取轮廓,您可以执行以下操作:

_,contours,heirarchy=cv2.findContours(erode,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
im2 =  np.zeros((480,640,3), np.uint8)
cv2.drawContours(im2,contours,-1,(125,125,0),1)

(使用RETR_EXTERNAL只会为你提供外部部分的边界,如果在那些内部制作的任何单个部件将被排除,那么只需使用RETR_TREE,它将提供正确的层次结构,因此你知道你正在查看内部边界) 。 你已经创建了大小为500,500的空数组im2,因为这样做会排除图像的某些部分(在此外绘制轮廓时会出错)。这些都是小错误。

现在对于错误的主要部分是cv2.drawContours(im2,contours,0,(125,125,0),1),你在这里给出了图像中轮廓的数组,并告诉它只绘制第一个轮廓(第0个元素);所以你应该做的是使用轮廓编号逐一绘制/处理轮廓(1或2或3或4或......而不是0)或首先选择轮廓然后像这样绘制:

cnt=contour[4]
cv2.drawContours(im2,[cnt],0,(125,125,0),1)

或者只是通过提供(-1)代替(0)绘制所有轮廓,就像我之前所示。

如果你想选择一些轮廓并绘制它们,你可以这样做:

cnt=[contour[2],contour[5],contour[9]]
cv2.drawContours(im2,cnt,-1,(125,125,0),1)