summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c
diff options
context:
space:
mode:
authorleochanj105 <leochanj@live.unc.edu>2020-10-19 23:09:30 -0400
committerleochanj105 <leochanj@live.unc.edu>2020-10-20 02:40:39 -0400
commitf618466c25d43f3bae9e40920273bf77de1e1149 (patch)
tree460e739e2165b8a9c37a9c7ab1b60f5874903543 /SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c
parent47ced4e96bbb782b9e780e8f2cfc637b2c21ff44 (diff)
initial sd-vbs
initial sd-vbs add sd-vbs sd-vbs
Diffstat (limited to 'SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c')
-rw-r--r--SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c142
1 files changed, 142 insertions, 0 deletions
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