在计算轮廓时如何避免图像帧计数?

时间:2020-07-25 10:36:01

标签: python opencv

在计算轮廓时如何避免图像帧计数? (opencv python)

there have 6 contours with image frame.i need avoid image frame

1 个答案:

答案 0 :(得分:1)

您需要使用cv2.THRESH_BINARY_INV作为阈值函数的参数。

import numpy as np
import cv2

img = cv2.imread('./tmp.png')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContours(img, contours, -1, (0, 255, 0), 3)

print("num contours = {}".format(len(contours)))
cv2.imwrite("./contours.png", img)

结果图: enter image description here