简体   繁体   中英

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

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

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

  1. Calculate enclosing rectangle and take eg 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.

First you need to find contours, draw a bounding box and then take the x and y from there. I hope this helps

import numpy as np
import cv2

im = cv2.imread('ctBI9.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
        if cv2.contourArea(c) <= 50 :
            continue    
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255,0), 2)
        center = (x,y)
        print (center)

while True: 
    cv2.imshow('test',im)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

result is something like this (93, 746) (1174, 738) (147, 736) (395, 729) (506, 404) (240, 168) (918, 130)

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()

输入图像

输出图像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM