May-14-2022, 05:34 PM
Hello. I'm writing the code to find contours and extract the bounding rectangle coordinates, then find center coordinate and draw center point. But I'm not satisfied with result.
Here's a code in Python:
![[Image: Screenshot-2022-05-14-at-20-32-39.png]](https://i.ibb.co/Zd8ZjB3/Screenshot-2022-05-14-at-20-32-39.png)
This is what I expect to get:
![[Image: res-copy.png]](https://i.ibb.co/bdh8Tky/res-copy.png)
Why I get such result? Please help to improve this code.
Thanks!
Here's a code in Python:
import cv2
import numpy as np
import imutils
image = cv2.imread('res.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray_image, (7,7) ,10)
thresh = cv2.threshold(blurred, 160, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
for c in cnts:
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
cv2.circle(image, (cX, cY), 4, (255, 255, 255), -1)
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()This is what I get now: ![[Image: Screenshot-2022-05-14-at-20-32-39.png]](https://i.ibb.co/Zd8ZjB3/Screenshot-2022-05-14-at-20-32-39.png)
This is what I expect to get:
![[Image: res-copy.png]](https://i.ibb.co/bdh8Tky/res-copy.png)
Why I get such result? Please help to improve this code.
Thanks!
