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

3
我正在尝试从以下图像中获取轮廓的x和y位置,但我搞砸了。 the image 我只需要找到轮廓的x和y位置或轮廓的中心。
结果将类似于手动从GIMP查找其位置的以下内容:
290, 210 982, 190 570, 478
我相信可以使用cv2.findContours方法完成,但我现在真的没有主意。
-离题-
我将在使用win32api.SetCursorPos((xposition,yposition))设置光标位置时使用这些值。
谢谢
3个回答

6

您可以在此参考:

使用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


最好在您的帖子中包含该链接的相关部分。如果将来外部链接被修改或删除,您的答案仍然会有用。 - stateMachine
感谢您的推荐,我已更新回答。@eldesgraciado - Sushant Agarwal

2

确实,您可以使用findContours完成此操作。 由于已经有了轮廓,因此有几个选项:

  1. 计算包围矩形并取中心点。
  2. 计算矩和取质心。
  3. 拟合最小外接圆并取中心点。
  4. 等等......

这里有一些您可以使用轮廓完成的示例,包括上述选项。


2

首先,您需要找到轮廓,绘制边界框,然后从那里获取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)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接