| 10 | |
| 11 | |
| 12 | def process(image, opt=1): |
| 13 | hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV) |
| 14 | line = cv.getStructuringElement(cv.MORPH_RECT, (15, 15), (-1, -1)) |
| 15 | #mask = cv.inRange(hsv, (0, 43, 46), (10, 255, 255)) |
| 16 | cv.imshow("mask", mask) |
| 17 | mask = cv.morphologyEx(mask, cv.MORPH_OPEN, line) |
| 18 | #cv.imshow("masks", mask) |
| 19 | |
| 20 | # 轮廓提取, 发现最大轮廓 |
| 21 | out, contours, hierarchy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) |
| 22 | index = -1 |
| 23 | max = 0 |
| 24 | for c in range(len(contours)): |
| 25 | area = cv.contourArea(contours[c]) |
| 26 | if area > max: |
| 27 | max = area |
| 28 | index = c |
| 29 | # 绘制 |
| 30 | if index >= 0: |
| 31 | rect = cv.minAreaRect(contours[index]) |
| 32 | cv.ellipse(image, rect, (0, 255, 0), 2, 8) |
| 33 | cv.circle(image, (np.int32(rect[0][0]), np.int32(rect[0][1])), 2, (255, 0, 0), 2, 8, 0) |
| 34 | return image |
| 35 | |
| 36 | |
| 37 | while(True): |