你已经找到了边缘,现在你需要找到这些边缘的位置。
(我没有使用你提供的图片,而是使用了桌面上的示例图片:D)
以下行将给出这些坐标:
import cv2
import numpy as np
img = cv2.imread('Messi.jpg', 0)
edges = cv2.Canny(img, 100, 255) #--- image containing edges ---
现在你需要找到数值大于0的坐标。
indices = np.where(edges != [0])
coordinates = zip(indices[0], indices[1])
numpy.where()
方法,以检索由两个数组组成的元组indices
,第一个数组包含白点的x坐标,第二个数组包含白色像素的y坐标。indices
返回:
(array([ 1, 1, 2, ..., 637, 638, 638], dtype=int64),
array([292, 298, 292, ..., 52, 49, 52], dtype=int64))
zip()
方法获取包含点的元组列表。打印coordinates
会给我一个带有边缘的坐标列表:
[(1, 292), (1, 298), (2, 292), .....(8, 289), (8, 295), (9, 289), (9, 295), (10, 288), (10, 289), (10, 294)]
cv2.drawContours
,该怎么办? - TechnoRazor