Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find coordinates of a Canny Edge Image - OpenCV & python

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

like image 764
rdo123 Avatar asked May 10 '18 13:05

rdo123


People also ask

How do I get coordinates from edge detection?

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 do we detect edges of items in a drawing in OpenCV?

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.

What does Canny return OpenCV?

Canny() Function in OpenCV is used to detect the edges in an image.


1 Answers

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)]
like image 107
Jeru Luke Avatar answered Nov 07 '22 18:11

Jeru Luke