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

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.

主站蜘蛛池模板: 伊吾县| 河北区| 正安县| 房山区| 丰城市| 新化县| 陆丰市| 南平市| 开远市| 深州市| 自贡市| 沾益县| 益阳市| 龙泉市| 惠州市| 上栗县| 湘西| 南宫市| 土默特右旗| 宝鸡市| 乌兰县| 林芝县| 汶上县| 历史| 晋江市| 洪雅县| 辽宁省| 鄂尔多斯市| 沐川县| 盘山县| 永川市| 平陆县| 平安县| 于田县| 那曲县| 镇坪县| 独山县| 仁怀市| 建水县| 梓潼县| 乾安县|