13

I want to save in a list the coordinates of all edges that the openCV detect, I succeed in showing the edges on screen (on the subscribed photo), and I don't know how to save the coordinates (all the white lines). Thanks in advance.

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
rdo123
  • 149
  • 1
  • 1
  • 9

1 Answers1

19

You have found the edges, now you need to find where those edges are located.

(I did not use the image provided by you, I rather used a sample image on my desktop :D )

The following lines gives you those coordinates:

import cv2
import numpy as np

img = cv2.imread('Messi.jpg', 0)
edges = cv2.Canny(img, 100, 255)     #--- image containing edges ---

Now you need to find coordinates having value more than 0

indices = np.where(edges != [0])
coordinates = zip(indices[0], indices[1])
  • I used the numpy.where() method to retrieve a tuple indices of two arrays where the first array contains the x-coordinates of the white points and the second array contains the y-coordinates of the white pixels.

indices returns:

(array([  1,   1,   2, ..., 637, 638, 638], dtype=int64),
 array([292, 298, 292, ...,  52,  49,  52], dtype=int64))
  • I then used the zip() method to get a list of tuples containing the points.

Printing coordinates gives me a list of coordinates with edges:

[(1, 292), (1, 298), (2, 292), .....(8, 289), (8, 295), (9, 289), (9, 295), (10, 288), (10, 289), (10, 294)]
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • 6
    Only issue with this solution is that it returns one list of points, in order from top to bottom: what if you wanted multiple lists of contiguous points, such that you could use `cv2.drawContours` with them? – TechnoRazor Mar 15 '20 at 15:39