Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get x,y position of contours in Python OpenCV

I'm trying to get x and y positions of contours from the following image, but I messed up.

The image:

1

I just need to find x and y positions of contours or center of the contours.

The results will be something like the following as I manually look up their positions from GIMP.

290, 210 982, 190 570, 478

I believe it can be done with cv2.findContours method, but I'm really out of ideas right now.

-Offtopic-

I will use these values in setting cursor position usingwin32api.SetCursorPos((xposition,yposition))

Thanks

like image 750
a644cr Avatar asked Dec 29 '25 00:12

a644cr


2 Answers

You can refer here

Find Co-ordinates of Contours using OpenCV | Python

# Python code to find the co-ordinates of 
# the contours detected in an image. 
import numpy as np 
import cv2 

# Reading image 
font = cv2.FONT_HERSHEY_COMPLEX 
img2 = cv2.imread('test.jpg', cv2.IMREAD_COLOR) 

# Reading same image in another 
# variable and converting to gray scale. 
img = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE) 

# Converting image to a binary image 
# ( black and white only image). 
_, threshold = cv2.threshold(img, 110, 255, cv2.THRESH_BINARY) 

# Detecting contours in image. 
contours, _= cv2.findContours(threshold, cv2.RETR_TREE, 
                            cv2.CHAIN_APPROX_SIMPLE) 

# Going through every contours found in the image. 
for cnt in contours : 

    approx = cv2.approxPolyDP(cnt, 0.009 * cv2.arcLength(cnt, True), True) 

    # draws boundary of contours. 
    cv2.drawContours(img2, [approx], 0, (0, 0, 255), 5) 

    # Used to flatted the array containing 
    # the co-ordinates of the vertices. 
    n = approx.ravel() 
    i = 0

    for j in n : 
        if(i % 2 == 0): 
            x = n[i] 
            y = n[i + 1] 

            # String containing the co-ordinates. 
            string = str(x) + " " + str(y) 

            if(i == 0): 
                # text on topmost co-ordinate. 
                cv2.putText(img2, "Arrow tip", (x, y), 
                                font, 0.5, (255, 0, 0)) 
            else: 
                # text on remaining co-ordinates. 
                cv2.putText(img2, string, (x, y), 
                        font, 0.5, (0, 255, 0)) 
        i = i + 1

# Showing the final image. 
cv2.imshow('image2', img2) 

# Exiting the window if 'q' is pressed on the keyboard. 
if cv2.waitKey(0) & 0xFF == ord('q'): 
    cv2.destroyAllWindows()

Input image

Output image

like image 59
Sushant Agarwal Avatar answered Dec 30 '25 12:12

Sushant Agarwal


Indeed, you can do that with findContours. Since you have your contours there are several options:

  1. Calculate enclosing rectangle and take e.g. the center point.
  2. Calculate moments and take the centroid
  3. Fit minimum enclosing circle and take the center
  4. and so on...

Here are some examples of what you can do with your contours, including the options above.

like image 37
PSchn Avatar answered Dec 30 '25 13:12

PSchn