Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6077485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:43:34+00:00 2026-05-23T10:43:34+00:00

Current situation : I’m trying to extract segments from an image. Thanks to openCV’s

  • 0

Current situation: I’m trying to extract segments from an image. Thanks to openCV’s findContours() method, I now have a list of 8-connected point for every contours. However, these lists are not directly usable, because they contain a lot of duplicates.

The problem: Given a list of 8-connected points, which can contain duplicates, extract segments from it.

Possible solutions:

  • At first, I used openCV’s approxPolyDP() method. However, the results are pretty bad… Here is the zoomed contours:

enter image description here

Here is the result of approxPolyDP(): (9 segments! Some overlap)

enter image description here

but what I want is more like:

enter image description here

It’s bad because approxPolyDP() can convert something that “looks like several segments” in “several segments”. However, what I have is a list of points that tend to iterate several times over themselves.

For example, if my points are:

0 1 2 3 4 5 6 7 8 
  9   

Then, the list of point will be 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 9… And if the number of points become large (>100) then the segments extracted by approxPolyDP() are unfortunately not duplicates (i.e : they overlap each other, but are not strictly equal, so I can’t just say “remove duplicates”, as opposed to pixels for example)

  • Perhaps, I’ve got a solution, but it’s pretty long (though interesting). First of all, for all 8-connected list, I create a sparse matrix (for efficiency) and set the matrix values to 1 if the pixel belongs to the list. Then, I create a graph, with nodes corresponding to pixels, and edges between neighbouring pixels. This also means that I add all the missing edges between pixels (complexity small, possible because of the sparse matrix). Then I remove all possible “squares” (4 neighbouring nodes), and this is possible because I am already working on pretty thin contours. Then I can launch a minimal spanning tree algorithm. And finally, I can approximate every branch of the tree with openCV’s approxPolyDP()

To sum up: I’ve got a tedious method, that I’ve not yet implemented as it seems error-prone. However, I ask you, people at Stack Overflow: are there other existing methods, possibly with good implementations?


Edit: To clarify, once I have a tree, I can extract “branches” (branches start at leaves or nodes linked to 3 or more other nodes) Then, the algorithm in openCV’s approxPolyDP() is the Ramer–Douglas–Peucker algorithm, and here is the Wikipedia picture of what it does:

enter image description here

With this picture, it is easy to understand why it fails when points may be duplicates of each other


Another edit: In my method, there is something that may be interesting to note. When you consider points located in a grid (like pixels), then generally, the minimal spanning tree algorithm is not useful because there are many possible minimal trees

X-X-X-X
|
X-X-X-X

is fundamentally very different from

X-X-X-X
| | | |
X X X X

but both are minimal spanning trees

However, in my case, my nodes rarely form clusters because they are supposed to be contours, and there is already a thinning algorithm that runs beforehand in the findContours().


Answer to Tomalak’s comment:

enter image description here

If DP algorithm returns 4 segments (the segment from the point 2 to the center being there twice) I would be happy! Of course, with good parameters, I can get to a state where “by chance” I have identical segments, and I can remove duplicates. However, clearly, the algorithm is not designed for it.

Here is a real example with far too many segments:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:43:34+00:00Added an answer on May 23, 2026 at 10:43 am

    Using Mathematica 8, I created a morphological graph from the list of white pixels in the image. It is working fine on your first image:

    enter image description here

    enter image description here

    Create the morphological graph:

    graph = MorphologicalGraph[binaryimage];
    

    Then you can query the graph properties that are of interest to you.

    This gives the names of the vertex in the graph:

    vertex = VertexList[graph]
    

    The list of the edges:

    EdgeList[graph]
    

    And that gives the positions of the vertex:

    pos = PropertyValue[{graph, #}, VertexCoordinates] & /@ vertex
    

    This is what the results look like for the first image:

    In[21]:= vertex = VertexList[graph]
    
    Out[21]= {1, 3, 2, 4, 5, 6, 7, 9, 8, 10}
    
    In[22]:= EdgeList[graph]
    
    Out[22]= {1 \[UndirectedEdge] 3, 2 \[UndirectedEdge] 4,  3 \[UndirectedEdge] 4, 
              3 \[UndirectedEdge] 5, 4 \[UndirectedEdge] 6,  6 \[UndirectedEdge] 7, 
              6 \[UndirectedEdge] 9, 8 \[UndirectedEdge] 9,  9 \[UndirectedEdge] 10}
    
    In[26]:= pos = PropertyValue[{graph, #}, VertexCoordinates] & /@ vertex
    
    Out[26]= {{54.5, 191.5}, {98.5, 149.5},  {42.5, 185.5}, 
              {91.5, 138.5}, {132.5, 119.5}, {157.5, 72.5},
              {168.5, 65.5}, {125.5, 52.5},  {114.5, 53.5}, 
              {120.5, 29.5}}
    

    Given the documentation, http://reference.wolfram.com/mathematica/ref/MorphologicalGraph.html, the command MorphologicalGraph first computes the skeleton by morphological thinning:

    skeleton = Thinning[binaryimage, Method -> "Morphological"]
    

    Then the vertex are detected; they are the branch points and the end points:

    verteximage = ImageAdd[
                      MorphologicalTransform[skeleton, "SkeletonEndPoints"],   
                      MorphologicalTransform[skeleton, "SkeletonBranchPoints"]]
    

    enter image description here

    And then the vertex are linked after analysis of their connectivity.

    For example, one could start by breaking the structure around the vertex and then look for the connected components, revealing the edges of the graph:

    comp = MorphologicalComponents[
               ImageSubtract[
                   skeleton, 
                   Dilation[vertices, CrossMatrix[1]]]];
    Colorize[comp] 
    

    enter image description here

    The devil is in the details, but that sounds like a solid starting point if you wish to develop your own implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of approx. 10000 items. The current situation is that every
Current situation: Ubuntu 8.04 server edition (live server) Postgresql 8.3.7 (from standard repositories) Postgis
My current situation is: I have to read a file and put the contents
In my current situation, it is not unusual for me to have several UNIX
let me explain my current situation i have a SharePoint site lets say it
The situation: I need to convert our current development environment from Windows XP 32-bit to
I have a fairly standard inheritance situation in my current LINQ-to-SQL project. I have
My current situation: I have a user control A.ascx with code behind A.ascx.cs and
The current situation is as follows: We have an production .net 3.5 WCF service,
Here is my current situation: I have a web page containing a couple scrollable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.