| 7 | |
| 8 | |
| 9 | def process(image, opt=1): |
| 10 | mask = fgbg.apply(frame) |
| 11 | line = cv.getStructuringElement(cv.MORPH_RECT, (1, 5), (-1, -1)) |
| 12 | mask = cv.morphologyEx(mask, cv.MORPH_OPEN, line) |
| 13 | cv.imshow("mask", mask) |
| 14 | # 轮廓提取, 发现最大轮廓 |
| 15 | out, contours, hierarchy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) |
| 16 | for c in range(len(contours)): |
| 17 | area = cv.contourArea(contours[c]) |
| 18 | if area < 150: |
| 19 | continue |
| 20 | rect = cv.minAreaRect(contours[c]) |
| 21 | cv.ellipse(image, rect, (0, 255, 0), 2, 8) |
| 22 | cv.circle(image, (np.int32(rect[0][0]), np.int32(rect[0][1])), 2, (255, 0, 0), 2, 8, 0) |
| 23 | return image, mask |
| 24 | |
| 25 | |
| 26 | while True: |