1

I'm fairly new to programming and need to write a program that will find the center of 4 discs (circles) that are located in each corner of a square image.

I don't know the exact coordinates of the discs but have a very good approximation of them. How can I go about finding the location of the centers of each of the 4 discs?

wp78de
  • 18,207
  • 7
  • 43
  • 71
Katherine
  • 143
  • 2
  • 5
  • Check out OpenCV! It was built for this sort of stuff. – asylumax Jun 02 '20 at 22:26
  • 2
    Welcome to Stack Overflow. This is not a tutorial, code-writing, or homework service. This is a Q&A site where *specific* programming questions (usually, but not always, including some code) get *specific* answers. Please take the [tour] and carefully read through the [help] to learn more about the site, including [what is on-topic](https://stackoverflow.com/help/on-topic) and [what is not](https://stackoverflow.com/help/dont-ask), and how to [ask a good question](https://stackoverflow.com/help/how-to-ask). Please also follow the [question checklist](https://meta.stackoverflow.com/q/260648). – MattDMo Jun 02 '20 at 22:26

2 Answers2

1

You can look for the centroid of shapes using moments in OpenCV:

import cv2

# read image through command line
img = cv2.imread(args["ipimage"])
# or load it from a path
#img = cv2.imread(R"/usr/home/dinges/4c.png")

# convert the image to grayscale
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# convert the grayscale image to binary image
ret,thresh = cv2.threshold(gray_image,127,255,0)

# find contours in the binary image
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
   # calculate moments for each contour
   M = cv2.moments(c)

   # calculate x,y coordinate of center
   cX = int(M["m10"] / M["m00"])
   cY = int(M["m01"] / M["m00"])
   print('centroid: X:{}, Y:{}'.format(cX, cY)) 
   cv2.circle(img, (cX, cY), 5, (255, 255, 255), -1)
   cv2.putText(img, "centroid", (cX -25, cY -25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

   # display the image
   cv2.imshow("Image", img)
   cv2.waitKey(0)

Reference: Find the Center of a Blob (Centroid) using OpenCV

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Here is a related solution for a single shape https://stackoverflow.com/questions/19768508/python-opencv-finding-circle-sun-coordinates-of-center-the-circle-from-pictu – wp78de Jun 02 '20 at 23:07
0

The window will most likely draw images using the top left corner as X= 0, Y = 0. Circles tend to be drawn from the centre outwards, so the centre of the circle is most likely the (X,Y) coordinates used to draw the given circle. You can probably find a way to request the values for the circles coordinates depending how they're being drawn and what you're using to draw them. There are a number of ways of drawing to screen with python, so It depends how you are doing it. Can you provide a little more details?

JMcK
  • 100
  • 1
  • 9
  • The circles are already on the corners of the image, and I need to find the location of the center for each one of them. I was thinking I'd first have to detect the circle, and from there be able to calculate the center coordinates. However, I'm unsure of how to detect the circle location first – Katherine Jun 02 '20 at 22:38
  • If you share your code it will be much easier to answer. Edit your original answer to include it. :) – JMcK Jun 03 '20 at 08:55