# How can I extract the edge points from an image?

I want to extract the edge points from a text image and use ListPlot[points] to visualize those points:

I tried to used ImageCorners[] and CornerFilter[], but there isn't any good result:

img=ColorConvert[img, "Grayscale"] // Binarize



HighlightImage[img, ImageCorners[img]]


img0 = Import["https://i.stack.imgur.com/pLroE.png"];

Graphics[{Blue, Values @ ComponentMeasurements[Binarize @ img0 , "Contours"]}]


You can get the coordinate data by a simple ReplaceAll:

data = Join @@ Values[ComponentMeasurements[Binarize@img0, "Contours"]] /.
Line[x_] :> x;

Row[ListPlot[data, Joined -> #, ImageSize -> 400] & /@ {False, True},
Spacer[20]]


HighlightImage[img0,
EdgeDetect @ RemoveBackground[img0, {"Background", Black}], "Lighten"]


• I need data set of points, how can I extract it from your code? – vito Oct 19 '20 at 10:29
• @vito, please see the update. – kglr Oct 19 '20 at 11:35

You could just do this:

img = Binarize@Import["https://i.stack.imgur.com/pLroE.png"];
ListPlot@PixelValuePositions[EdgeDetect@img, 1]


Or if you want to use meshing then:

img = Binarize@Import["https://i.stack.imgur.com/pLroE.png"];
ListPlot@MeshCoordinates@RegionBoundary@ImageMesh@img


If you want to go further and break them up into individual point sets for each letter/hole then:

gr = MeshConnectivityGraph@ImageMesh@img;
comps = ConnectedGraphComponents@gr;
pts = GraphEmbedding /@ comps;
ListPlot[pts]


Note if you do ListLinePlot then the order of the points may not come out as expected. You may find it more productive to work with the graphs in comps, which have richer information about connectivity, instead of using raw points.

• Note, if you want a better quality mesh you should rasterize the image at a higher resolution first for example: ImageMesh@Rasterize[img, RasterSize -> 3000] – flinty Oct 18 '20 at 16:58
• MeshConnectivityGraph doesn't work for me, I have Mathematica 12.0 – vito Oct 19 '20 at 10:26