- 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.
- JavaScript高效圖形編程
- Python編程完全入門教程
- Data Analysis with IBM SPSS Statistics
- Building Machine Learning Systems with Python(Second Edition)
- RealSenseTM互動開發(fā)實(shí)戰(zhàn)
- GameMaker Essentials
- Visual Studio Code 權(quán)威指南
- 代碼閱讀
- 寫給青少年的人工智能(Python版·微課視頻版)
- Drupal Search Engine Optimization
- Python Social Media Analytics
- Professional JavaScript
- 高質(zhì)量程序設(shè)計指南:C++/C語言
- Python全棧開發(fā):數(shù)據(jù)分析
- Scratch少兒編程高手的7個好習(xí)慣