summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/multi_ncut/src/c
diff options
context:
space:
mode:
Diffstat (limited to 'SD-VBS/benchmarks/multi_ncut/src/c')
-rw-r--r--SD-VBS/benchmarks/multi_ncut/src/c/.computeMultiW.c.swpbin0 -> 12288 bytes
-rw-r--r--SD-VBS/benchmarks/multi_ncut/src/c/script_multi_ncut.c90
-rw-r--r--SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c95
-rw-r--r--SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c142
-rw-r--r--SD-VBS/benchmarks/multi_ncut/src/c/segment.h46
5 files changed, 373 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/multi_ncut/src/c/.computeMultiW.c.swp b/SD-VBS/benchmarks/multi_ncut/src/c/.computeMultiW.c.swp
new file mode 100644
index 0000000..a3b35c6
--- /dev/null
+++ b/SD-VBS/benchmarks/multi_ncut/src/c/.computeMultiW.c.swp
Binary files differ
diff --git a/SD-VBS/benchmarks/multi_ncut/src/c/script_multi_ncut.c b/SD-VBS/benchmarks/multi_ncut/src/c/script_multi_ncut.c
new file mode 100644
index 0000000..e4f7912
--- /dev/null
+++ b/SD-VBS/benchmarks/multi_ncut/src/c/script_multi_ncut.c
@@ -0,0 +1,90 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "segment.h"
8#include "extra.h"
9#define MAX_EDGE 500000
10int main(int argc, char* argv[])
11{
12 SET_UP
13 float sigma = 0.6;
14 float k = 4;
15 int min_size = 10;
16 char im1[256];
17 int num_ccs[1] = {0};
18 I2D *out;
19 I2D* im;
20 I2D* seg;
21 I2D* kernel;
22 I2D* ind;
23 I2D* output;
24 int ret;
25 int *segments;
26 edge* edges;
27 universe* u;
28
29 F2D *imageOut, *tempOut;
30 F2D *edgeWeights, *in;
31 printf("Input image: ");
32 scanf("%s", im1);
33 im = readImage(im1);
34
35
36 int height, width, num_edges, num_vertices;
37 height = im->height;
38 width = im->width;
39 num_edges = MAX_EDGE;
40 num_vertices = width * height;
41 segments = (int *) malloc(height*width*sizeof(int));
42 edges = (edge*)malloc(sizeof(edge)*width*height*4);
43 imageOut = fSetArray(height, width, 0);
44 tempOut = fSetArray(height, width, 0);
45 kernel = iMallocHandle(1,5);
46 edgeWeights = fMallocHandle(1, num_edges);
47 in = fDeepCopy(edgeWeights);
48 ind = fMallocHandle(1, num_edges);
49 u = (universe*)malloc(sizeof(universe));
50 u->elts = (uni_elt*)malloc(sizeof(uni_elt)*num_vertices);
51 output = iMallocHandle(height, width);
52
53 printf("start\n");
54 for_each_job{
55 seg = segment_image(im, sigma, k, min_size, num_ccs, segments, edges, imageOut, tempOut, kernel, edgeWeights, in, ind, u, output);
56 out = seg;
57 }
58 printf("end..\n");
59#ifdef CHECK
60 /** Self checking - use expected.txt from data directory **/
61 {
62 int ret=0;
63 float tol = 0;
64
65#ifdef GENERATE_OUTPUT
66 writeMatrix(out, argv[1]);
67#endif
68
69 ret = selfCheck(out, "./expected_C.txt", tol);
70 if(ret < 0)
71 printf("Error in Multi N Cut\n");
72 }
73#endif
74
75 iFreeHandle(im);
76 free(edges);
77 free(segments);
78 free(tempOut);
79 free(imageOut);
80 free(edgeWeights);
81 free(in);
82 free(ind);
83 free(output);
84 free(u->elts);
85 free(u);
86 WRITE_TO_FILE
87 return 0;
88}
89
90
diff --git a/SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c b/SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c
new file mode 100644
index 0000000..13b219b
--- /dev/null
+++ b/SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c
@@ -0,0 +1,95 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "segment.h"
8
9// threshold function
10#define THRESHOLD(size, c) (c/size)
11
12int find(universe* u, int x)
13{
14 int y=x;
15 while (y != u->elts[y].p)
16 y = u->elts[y].p;
17 u->elts[x].p = y;
18 return y;
19}
20
21void join(universe* u, int x, int y)
22{
23 if (u->elts[x].rank > u->elts[y].rank)
24 {
25 u->elts[y].p = x;
26 u->elts[x].size += u->elts[y].size;
27 }
28 else
29 {
30 u->elts[x].p = y;
31 u->elts[y].size += u->elts[x].size;
32 if (u->elts[x].rank == u->elts[y].rank)
33 u->elts[y].rank++;
34 }
35 u->num--;
36 return ;
37}
38
39universe *segment_graph(int num_vertices, int num_edges, edge *edges, float c, F2D* edgeWeights, F2D* in, I2D* ind,
40 universe* u)
41{
42 float threshold[num_vertices];
43 int i, a, b, j, k;
44 //universe *u;
45 //F2D *edgeWeights;
46 I2D *indices;
47
48 edgeWeights = fResetHandle(edgeWeights,1,num_edges);
49
50 for(i=0; i<num_edges; i++)
51 asubsref(edgeWeights,i) = edges[i].w;
52
53 // sort edges by weight
54 indices = fResortIndices(edgeWeights,1, in, ind);
55
56 // make a disjoint-set forest
57 //u = (universe*)malloc(sizeof(universe));
58 //u->elts = (uni_elt*)malloc(sizeof(uni_elt)*num_vertices);
59 u->num = num_vertices;
60 for(i=0; i<num_vertices; i++)
61 {
62 u->elts[i].rank = 0;
63 u->elts[i].size = 1;
64 u->elts[i].p = i;
65 }
66
67 // init thresholds
68 for (i = 0; i < num_vertices; i++)
69 arrayref(threshold,i) = THRESHOLD(1,c);
70
71 // for each edge, in non-decreasing weight order...
72 for (i = 0; i < num_edges; i++)
73 {
74 edge *pedge = &edges[ asubsref(indices,i) ];
75
76 // components conected by this edge
77 a = find(u, pedge->a);
78 b = find(u, pedge->b);
79 if (a != b)
80 {
81 if ((pedge->w <= arrayref(threshold,a)) && (pedge->w <= arrayref(threshold,b)))
82 {
83 join(u, a, b);
84 a = find(u, a);
85 arrayref(threshold,a) = pedge->w + THRESHOLD(u->elts[a].size, c);
86 }
87 }
88 }
89
90 //fFreeHandle(edgeWeights);
91 //iFreeHandle(indices);
92
93 return u;
94}
95
diff --git a/SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c b/SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c
new file mode 100644
index 0000000..72bc16f
--- /dev/null
+++ b/SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c
@@ -0,0 +1,142 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <time.h>
8#include "segment.h"
9
10#ifndef M_PI
11#define M_PI 3.141592653589793
12#endif
13
14// dissimilarity measure between pixels
15float diff(F2D *r, int x1, int y1, int x2, int y2)
16{
17 return sqrt(abs(( subsref(r,y1,x1) * subsref(r,y1,x1)) - ( subsref(r,y2,x2) * subsref(r,y2,x2))));
18}
19
20I2D *segment_image(I2D* im, float sigma, float c, int min_size, int *num_ccs,
21 int* segments, edge* edges,
22 F2D* imageOut, F2D* tempOut, I2D* kernel,
23 F2D* edgeWeights, F2D* in, I2D* ind,
24 universe* u,
25 I2D* output)
26{
27 int width = im->width;
28 int height = im->height;
29 int num = 0, x, y, i;
30 F2D* smooth_im;
31 //I2D *output;
32 //edge* edges;
33 int components = 0;
34/* int *colors = (int *) malloc(width*height*sizeof(int)); */
35 //segments = (int *) malloc(height*width*sizeof(int));
36
37 // smooth each color channel
38 smooth_im = imageReblur(im, imageOut, tempOut, kernel);
39
40 //build graph
41 //edges = (edge*)malloc(sizeof(edge)*width*height*4);
42
43 for (y = 0; y < height; y++)
44 {
45 for (x = 0; x < width; x++)
46 {
47 segments[y*width+x] = -1;
48 if (x < width-1)
49 {
50 edges[num].a = y * width + x;
51 edges[num].b = y * width + (x+1);
52 edges[num].w = diff(smooth_im, x, y, x+1, y);
53 num++;
54 }
55
56 if (y < height-1)
57 {
58 edges[num].a = y * width + x;
59 edges[num].b = (y+1) * width + x;
60 edges[num].w = diff(smooth_im, x, y, x, y+1);
61 num++;
62 }
63
64 if ((x < width-1) && (y < height-1))
65 {
66 edges[num].a = y * width + x;
67 edges[num].b = (y+1) * width + (x+1);
68 edges[num].w = diff(smooth_im, x, y, x+1, y+1);
69 num++;
70 }
71
72 if ((x < width-1) && (y > 0))
73 {
74 edges[num].a = y * width + x;
75 edges[num].b = (y-1) * width + (x+1);
76 edges[num].w = diff(smooth_im, x, y, x+1, y-1);
77 num++;
78 }
79 }
80 }
81
82 //free(smooth_im);
83
84 // segment
85 u = segment_graph(width*height, num, edges, c, edgeWeights, in, ind, u);
86
87 // post process small components
88 for (i = 0; i < num; i++)
89 {
90 int a, b;
91 a = find(u,edges[i].a);
92 b = find(u,edges[i].b);
93 if ((a != b) && ((u->elts[a].size < min_size) || (u->elts[b].size < min_size)))
94 join(u, a, b);
95 }
96
97 //free(edges);
98 arrayref(num_ccs,0) = u->num;
99
100 // pick random colors for each component
101 //output = iMallocHandle(height, width);
102
103/* srand(time(0));
104 for (i = 0; i < width*height; i++)
105 {
106 float temp;
107 temp = rand()/((float)RAND_MAX);
108 colors[i] = (int)(temp*255);
109 }
110*/
111
112 for (y = 0; y < height; y++)
113 {
114 for (x = 0; x < width; x++)
115 {
116 int comp;
117 comp = find(u, y * width + x);
118 if(segments[comp] == -1)
119 segments[comp] = components++;
120 subsref(output, y, x) = segments[comp];
121 }
122 }
123
124/*
125 for (y = 0; y < height; y++)
126 {
127 for (x = 0; x < width; x++)
128 {
129 int comp;
130 comp = find(u, y * width + x);
131 subsref(output, y, x) = colors[comp];
132 }
133 }
134*/
135 //free(u->elts);
136 //free(u);
137 //free(segments);
138
139 return output;
140}
141
142
diff --git a/SD-VBS/benchmarks/multi_ncut/src/c/segment.h b/SD-VBS/benchmarks/multi_ncut/src/c/segment.h
new file mode 100644
index 0000000..73ed8de
--- /dev/null
+++ b/SD-VBS/benchmarks/multi_ncut/src/c/segment.h
@@ -0,0 +1,46 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#ifndef SEGMENT
6#define SEGMENT
7
8#include "sdvbs_common.h"
9
10typedef struct
11{
12 float w;
13 int a, b;
14} edge;
15
16typedef struct
17{
18 int rank;
19 int p;
20 int size;
21} uni_elt;
22
23typedef struct
24{
25 uni_elt *elts;
26 int num;
27}universe;
28
29/* use imRef to access image data. */
30#define imRef(im, x, y) (im->data[y*im->width+x])
31
32I2D *segment_image(I2D *im, float sigma, float c, int min_size, int *num_ccs,
33 int* segments, edge* edges,
34 F2D* imageOut, F2D* tempOut, I2D* kernel, F2D* edgeWeights, F2D* in, I2D* ind,
35 universe* u,
36 I2D* output);
37universe *segment_graph(int num_vertices, int num_edges, edge *edges, float c, F2D* edgeWeights, F2D* in, I2D* ind,
38 universe* u);
39void join(universe* u, int x, int y);
40int find(universe* u, int x);
41float diff(F2D *r, int x1, int y1, int x2, int y2);
42
43
44#endif
45
46