Skip to content Skip to sidebar Skip to footer

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. Th

Solution 1:

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


Solution 2:

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.


Solution 3:

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)


Post a Comment for "How To Get X,y Position Of Contours In Python OpenCV"