生成单个轮廓的二进制图像

时间:2019-07-31 09:37:58

标签: python opencv cv2

我正在尝试细分足球场。我找到了代表野外区域的最大轮廓。我需要使用该区域生成一个二进制图像。

我正在关注一份研究论文,并遵循了所有步骤,包括

  1. 转换为HSV
  2. 捕获H频道
  3. 生成直方图
  4. 一些处理(与问题无关)
  5. 在二进制图像中找到最大的斑点

我已经使用Contours完成了此操作,并且具有代表场区域的最大轮廓。

enter image description here

我需要使用此特定轮廓来生成新的二进制图像,该图像仅包含该轮廓的区域。

# Find Largest Blob
# mask is the processed binary image
# using that mask I find the contours and draw them on original 
#image
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, 
cv2.CHAIN_APPROX_NONE)
largest_blob = max(contours, key=cv2.contourArea)
cv2.drawContours(image, largest_blob, -1, (0, 0, 255), 2)

2 个答案:

答案 0 :(得分:0)

您可以这样做,首先找到这些轮廓的边界矩形,

enter image description here

边框由绿色框指示

x,y,w,h = cv2.boundingRect(cnt)

然后使用它们裁剪图像

field = img[y:y+h, x:x+w, :]

然后您可以将二值化应用于field对象,更多信息可以在这里找到 https://docs.opencv.org/3.1.0/dd/d49/tutorial_py_contour_features.html

答案 1 :(得分:0)

我已经使用cv2.fillPoly(noiseless_mask, [largest_blob], 255)函数完成了它。 noiseless_mask = np.zeros_like(mask)

enter image description here