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

Enter image description here

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

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

HighlightImage[img, CornerFilter[img] // ImageAdjust]

Enter image description here

HighlightImage[img, ImageCorners[img]]

Enter image description here


2 Answers 2

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

1. ComponentMeasurements

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

enter image description here

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}, 

enter image description here

2. RemoveBackground + EdgeDetect

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

enter image description here

  • $\begingroup$ I need data set of points, how can I extract it from your code? $\endgroup$
    – vito
    Oct 19, 2020 at 10:29
  • $\begingroup$ @vito, please see the update. $\endgroup$
    – kglr
    Oct 19, 2020 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"];

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;

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.

  • $\begingroup$ 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] $\endgroup$
    – flinty
    Oct 18, 2020 at 16:58
  • $\begingroup$ MeshConnectivityGraph doesn't work for me, I have Mathematica 12.0 $\endgroup$
    – vito
    Oct 19, 2020 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.