- 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.
- 大話PLC(輕松動漫版)
- Java Web應用開發(fā)技術(shù)與案例教程(第2版)
- 青少年P(guān)ython編程入門
- Jupyter數(shù)據(jù)科學實戰(zhàn)
- 可解釋機器學習:模型、方法與實踐
- Mastering Apache Maven 3
- UML 基礎(chǔ)與 Rose 建模案例(第3版)
- Visual Basic程序設(shè)計實驗指導(第二版)
- Yii Project Blueprints
- Python網(wǎng)絡(luò)爬蟲技術(shù)與應用
- 軟件體系結(jié)構(gòu)
- Java編程從入門到精通
- Pandas 1.x Cookbook
- Flutter for Beginners
- 像程序員一樣使用MySQL