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.
You can use cv2. HoughLines() to detect lines in your edge image. The lines are returned in parametric form: (rho,theta) pairs for which rho = x * cos(theta) + y * sin(theta) . The code below (taken from OpenCV's tutorial on using cv2.
How are Edges Detected? Edges are characterized by sudden changes in pixel intensity. To detect edges, we need to go looking for such changes in the neighboring pixels. Come, let's explore the use of two important edge-detection algorithms available in OpenCV: Sobel Edge Detection and Canny Edge Detection.
Canny() Function in OpenCV is used to detect the edges in an image.
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])
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))
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With