Image Segmentation

Image segmentation is usually formulated as a graph partition problem, where each segment corresponds to a connected component. Moreover, each pixel is the vertex of the graph. Each edge has a weight, which is a non-negative dissimilarity between neighboring pixels. So, the goal of image segmentation is to decompose the image graph into several disconnected components, where the elements in a component are similar and the elements in the different components are dissimilar.

The components are defined as follows:

  • A component is made of a set of connected vertices;
  • Any two components have no shared vertices;
  • The dissimilarity ( of any two components 1 and 2 is larger than the confidence H of any of 1 and 2.
  • The dissimilarity ( is defined to be the minimum edge weight of all the edges connecting 1 and 2, or infinity if no such edge exists;
  • The confidence of a component C, (, is defined to be the maximum edge weight of the minimum spanning tree of C, plus a function ( where c is a positive constant and ∣ is the size of the component C;
  • A set of vertices must not be treated as a component if they can be partitioned into two or more components.

Your job is to write a program to list all the components.

Input Specification:

Each input file contains one test case. For each case, the first line contains three integers: Nv​​ (0), the total number of vertices (and hence the vertices are numbered from 0 to Nv​​1); Ne​​, the total number of edges; and c, the constant in the function (. Then Ne​​ lines follow, each gives an adge in the format:

V1 V2 Weight
 

Note: it is guaranteed that each pixel has no more than 8 neighboring pixels. The constant and all the weights are positive and are no more than 1000.

Output Specification:

For each case, list each component in a line. The vertices in a component must be printed in increasing order, separated by one space with no extra space at the beginning or the end of the line. The components must be listed in increasing order of their first vertex.

Sample Input 1:

10 21 100
0 1 10
0 3 60
0 4 90
1 2 90
1 3 50
1 4 200
1 5 86
2 4 95
2 5 5
3 4 95
3 6 15
3 7 101
4 5 500
4 6 100
4 7 101
4 8 101
5 7 300
5 8 50
6 7 90
7 8 84
7 9 34
 

Sample Output 1:

0 1 3 6
2 5 8
4
7 9
 

Sample Input 2:

7 7 100
0 1 10
1 2 61
2 3 50
3 4 200
4 5 82
5 0 200
3 6 90
 

Sample Output 2:

0 1
2 3 6
4 5
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node
 4 {
 5     int id;
 6     int pid;
 7     int w;
 8     bool operator< (const node& en) const
 9     {
10         return w>en.w;
11     }
12 } no;
13 struct vec
14 {
15     int root;
16     int size;
17     int mmst;
18     bool visit;
19 } ve;
20 vector<vec> v;
21 inline int fr(int i)
22 {
23     int ii=i;
24     for(;v[i].root!=-1;i=v[i].root);
25     if(ii!=i)
26     v[ii].root=i;
27     return i;
28 }
29 int main()
30 {
31 //    freopen("data.txt","r",stdin);
32 //    ios::sync_with_stdio(false);
33     int n,m,c,c1,c2,w;
34     scanf("%d%d%d",&n,&m,&c);
35 
36     ve.size=1;
37     ve.root=-1;
38     ve.mmst=0;
39     ve.visit=true;
40     
41     v.resize(n,ve);
42     
43     priority_queue<node> q;
44     for(;m--;)
45     {
46         scanf("%d%d%d",&no.id,&no.pid,&no.w);
47         q.push(no);
48     }
49     for(;!q.empty();)
50     {
51         no=q.top();
52         q.pop();
53         int r1=fr(no.id);
54         int r2=fr(no.pid);
55         if(r1!=r2)
56         {
57             int h1=c/v[r1].size+v[r1].mmst;
58             int h2=c/v[r2].size+v[r2].mmst;
59             h1=min(h1,h2);
60             
61             int rr1=min(r1,r2);
62             int rr2=max(r1,r2);
63             
64             if(no.w<=h1)
65             {
66                 v[rr1].size+=v[rr2].size;
67                 v[rr1].mmst=max(no.w,max(v[r1].mmst,v[r2].mmst));
68                 v[rr2].root=rr1;
69             }
70         }
71     }
72     vector<pair<int,int> > vr;
73       for(int i=0;i<v.size();i++)
74       vr.emplace_back(make_pair(fr(i),i));
75       
76       sort(vr.begin(),vr.end());
77       
78       int r=vr[0].first;
79       printf("%d",vr[0].second);
80       for(int i=1;i<vr.size();i++)
81       {
82           if(vr[i].first==r)
83           printf(" %d",vr[i].second);
84         else
85         {
86             r=vr[i].first;
87             printf("\n%d",vr[i].second);
88         }
89       }
90       return 0;
91 }

猜你喜欢

转载自www.cnblogs.com/SkystarX/p/12285796.html