【问题标题】:Extract well ordered xy coordinates of OpenCV's Canny Edge Detection in python在 python 中提取 OpenCV 的 Canny 边缘检测的有序 xy 坐标
【发布时间】:2017-01-02 19:56:12
【问题描述】:

我有一个像this 这样的多边形,我想获取这个多边形边界的 xy 坐标。

因此我尝试了 OpenCV 的 Canny Edge Detection 并提取了如下坐标:

>>> edge = cv2.Canny(region, 100, 200)
>>> ans = []
>>> for y in range(0, edge.shape[0]):
>>>     for x in range(0, edge.shape[1]):
            if edge[y, x] != 0:
                ans = ans + [[x, y]]
>>> print ans

问题是,我想按照this picture 可能解释的边界排列顺序提取坐标。

由于我的多边形很复杂,凸包不起作用,并且由于我从 cv2.edge() 获得的边缘点的数量,我需要使用快速算法。

【问题讨论】:

  • 你应该使用 findContours 而不是 Canny
  • @Sunreef 谢谢,成功了!

标签: python opencv coordinates


【解决方案1】:

我不确定变量分配是否是有意的(y 分配给 x 轴,反之亦然),但这里我用来获取某个坐标:

import cv2
import numpy as np

img = cv2.imread('1.JPG')
edges = cv2.Canny(img,100,200)
ans = []
highestY = 0
highestX = 0
highestCoordinate = [0, 0]

for x in range(0, edges.shape[0]):
    for y in range(0, edges.shape[1]):
        if edges[x, y] != 0:            
            ans = ans + [[x, y]]
            if highestX < x:
                highestX = x
            if highestY < y:
                highestY = y
                highestCoordinate = [x, y]       

print(f"Highest y is {highestY}")
print(f"Highest x is {highestX}")
print(f"Highest coordinate is {highestCoordinate}")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    相关资源
    最近更新 更多