Slide 196
Slide 196 text
image = cv2.imread("rectangle1rgb.png")
●
●
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
●
(_, binary) = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
●
(contours, _) = cv2.findContours(binary, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
●
●
cnt = contours[0]
●
M = cv2.moments(cnt)
●
●
((x, y), radius) = cv2.minEnclosingCircle(cnt)
●
M = cv2.moments(cnt)
●
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
●
cv2.circle(image, (int(x), int(y)), int(radius), (0, 255, 255), 2)
●
cv2.circle(image, center, 5, (0, 0, 255), -1)
找中心點與外框