A Method for Extracting polygon from a texture image.

Source: Internet
Author: User

A Method for Extracting polygon from a texture image.

I have been writing a blog here for a long time. In many cases, the texture image and the polygon match are often complicated to manually set. Therefore, a code is written to extract the edge polygon from the texture image. However, this code can only extract "solid" polygon and only support one polygon. Of course, you can extend it to extract multiple polygon. The basic idea is as follows:

1. quickly fill in the transparent part of the texture. And obtain an edge seed.

2. Use edge seeds to quickly search for edges.

3. Simplify the edge.

It seems that the problem has been solved, but the test found that the node around the seed has no direction due to the fast edge retrieval, as a result, some points are not added to the List clockwise or counterclockwise when they are closed. To solve this problem, edge sorting is also required. After edge sorting, it needs to be simplified again:

4. Sort the simplified edge.

5. Simplify the edge.

Of course, if the edge sorting algorithm is more efficient, you can sort and simplify the search results, so that the edge can only run once.

Only edge simplification and sorting algorithms are listed here. Other codes have been published in previous articles.

1' simplified edge 2 Private Function EdgeSimple (edge As List (Of Point) 3 Dim tmp As New List (Of Point) 4 Dim result As New List (Of Point) 5' copy the first two points to the end 6 If edge. count <3 Then Return result 7 tmp. addRange (edge) 8 tmp. add (edge (0) 9 tmp. add (edge (1) 10' traverses the entire array. Each three vertices determine if they are collocated. If they are not collocated, Add the intermediate vertex to the return value 11 Dim v1, v2 As Vector212 For I As Integer = 0 To edge. count-113 v1 = New Vector2 (tmp (I ). x-tmp (I + 1 ). x, tmp (I ). y-tmp (I + 1 ). y) 14 v2 = New Vector2 (tmp (I + 2 ). x-tmp (I + 1 ). x, tmp (I + 2 ). y-tmp (I + 1 ). y) 15 v1.Normalize () 16 v2.Normalize () 17 If Vector2.Dot (v1, v2) + 1> 0.20.1f Then18 result. add (tmp (I + 1) 19 End If20 Next21 Return result22 End Function23 24 'edge sorting 25 Private Sub EdgeSort (edge As List (Of Point )) 26 Dim op As Point = GetOrigin (edge) 27 Dim tmp As Point28 For I As Integer = 0 To edge. count-229 For j As Integer = 0 To edge. count-I-230 If PointCmp (edge (j), edge (j + 1), op) Then31 tmp = edge (j) 32 edge (j) = edge (j + 1) 33 edge (j + 1) = tmp34 End If35 Next36 Next37 End Sub38 39 40 41 Private Function PointCmp (a As Point, B As Point, op As Point) As Boolean42 If. x> = 0 AndAlso B. X <0 Then Return True43 If. X = 0 AndAlso B. X = 0 Then Return. y> B. y44 Dim det As Integer = (. x-op. x) * (B. y-op. y)-(B. x-op. x) * (. y-op. y) 45 If det = 0 Then46 Dim d1 As Double = (. x-op. x) * (. x-op. x) + (. y-op. y) * (. y-op. y) 47 Dim d2 As Double = (B. x-op. x) * (B. x-op. y) + (B. y-op. y) * (B. y-op. y) 48 Return d1> d249 Else50 Return det <051 End If52 End Function

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.