如何在Python OpenCV中获取轮廓的x,y位置

时间:2016-09-12 00:26:09

标签: python opencv

我正试图从下图中获取轮廓的x和y位置,但我搞砸了。 the image

我只需要找到轮廓的x和y位置或轮廓的中心。

当我从GIMP手动查找他们的位置时,结果将如下所示。

290,210 982,190 570,478

我相信可以用cv2.findContours方法完成,但我现在真的没有想法。

-Offtopic -

我将在使用win32api.SetCursorPos((xposition,yposition))设置光标位置时使用这些值

由于

3 个答案:

答案 0 :(得分:1)

你可以参考这里

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

答案 1 :(得分:0)

事实上,你可以用findContours做到这一点。由于你有轮廓,有几种选择:

  1. 计算封闭的矩形并采取例如中心点。
  2. 计算力矩并取质心
  3. 安装最小围圈并取中心
  4. 等......
  5. Here是您可以对轮廓做些什么的一些示例,包括上面的选项。

答案 2 :(得分:0)

首先,您需要找到轮廓,绘制一个边界框,然后从那里获取x和y。希望对您有帮助

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

结果是这样的 (93,746) (1174,738) (147,736) (395,729) (506、404) (240,168) (918,130)