Python Forum
(OpenCV) Help to improve code for object detection and finding center of it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(OpenCV) Help to improve code for object detection and finding center of it
#1
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:
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]

This is what I expect to get:
[Image: res-copy.png]

Why I get such result? Please help to improve this code.
Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using .pb saved model for object detection hobbyist 2 1,193 Aug-03-2022, 05:55 AM
Last Post: hobbyist
  [S0LVED] [Tkinter] Center dialog? Winfried 8 4,231 Mar-01-2022, 07:17 PM
Last Post: Winfried
  How to zoom on/follow MOUTH in FACE? (OPENCV Face Detection) buzzdarkyear 2 1,804 Jan-12-2022, 12:31 AM
Last Post: buzzdarkyear
  Program to move a dot towards a circle center plumberpy 10 4,229 Dec-03-2021, 12:20 PM
Last Post: BashBedlam
  Improves performance on socket multi-threaded servers for object detection pennant 1 1,909 Aug-31-2021, 08:43 AM
Last Post: Larz60+
  This is an Object - Code interpretation JoeDainton123 2 1,818 Jun-16-2021, 08:11 PM
Last Post: snippsat
  Object detection on ground via the use of geo/sat UAV images hobbyist 2 2,113 Jan-23-2021, 11:57 PM
Last Post: hobbyist
  finding and deleting json object GrahamL 1 4,854 Dec-10-2020, 04:11 PM
Last Post: bowlofred
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,137 Dec-06-2020, 10:10 PM
Last Post: Aizou
  Center align Kristenl2784 1 1,970 Aug-03-2020, 04:25 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020