官术网_书友最值得收藏!

  • OpenCV 4 with Python Blueprints
  • Dr. Menua Gevorgyan Arsen Mamikonyan Michael Beyeler
  • 253字
  • 2021-06-24 16:50:04

Determining the contour of the segmented hand region

The first step involves determining the contour of the segmented hand region. Luckily, OpenCV comes with a pre-canned version of such an algorithm—cv2.findContours. This function acts on a binary image and returns a set of points that are believed to be part of the contour. As there might be multiple contours present in the image, it is possible to retrieve an entire hierarchy of contours, as follows:

def find_hull_defects(segment: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
contours, hierarchy = cv2.findContours(segment, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)

Furthermore, because we do not know which contour we are looking for, we have to make an assumption to clean up the contour result, since it is possible that some small cavities are left over even after the morphological closing. However, we are fairly certain that our mask contains only the segmented area of interest. We will assume that the largest contour found is the one that we are looking for.

Thus, we simply traverse the list of contours, calculate the contour area (cv2.contourArea), and store only the largest one (max_contour), like this:

max_contour = max(contours, key=cv2.contourArea) 

The contour that we found might still have too many corners. We approximate the contour with a similar contour that does not have sides that are less than 1 percent of the perimeter of the contour, like this:

epsilon = 0.01 * cv2.arcLength(max_contour, True)
max_contour = cv2.approxPolyDP(max_contour, epsilon, True)

Let's learn how to find the convex hull of a contour area, in the next section.

主站蜘蛛池模板: 沈阳市| 丽水市| 金秀| 镶黄旗| 万源市| 施秉县| 泰兴市| 宁德市| 信丰县| 分宜县| 四会市| 通辽市| 西林县| 石阡县| 从江县| 玉林市| 衡山县| 乌拉特后旗| 江达县| 江阴市| 昌乐县| 侯马市| 和政县| 色达县| 华容县| 静安区| 芦山县| 西宁市| 黑龙江省| 怀柔区| 巴塘县| 松原市| 九龙坡区| 嘉荫县| 金秀| 涞水县| 荥阳市| 大关县| 达尔| 大连市| 锡林浩特市|