summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c
blob: 72bc16fc743ecbd60a86823670c4f09c21b42e2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/********************************
Author: Sravanthi Kota Venkata
********************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "segment.h"

#ifndef M_PI
#define M_PI 3.141592653589793
#endif

// dissimilarity measure between pixels
float diff(F2D *r, int x1, int y1, int x2, int y2) 
{
  return sqrt(abs(( subsref(r,y1,x1) * subsref(r,y1,x1)) - ( subsref(r,y2,x2) * subsref(r,y2,x2))));
}

I2D *segment_image(I2D* im, float sigma, float c, int min_size, int *num_ccs, 
		   int* segments, edge* edges,
                   F2D* imageOut, F2D* tempOut, I2D* kernel, 
		   F2D* edgeWeights, F2D* in, I2D* ind,
		   universe* u,
		   I2D* output)
{
    int width = im->width;
    int height = im->height;
    int num = 0, x, y, i;
    F2D* smooth_im;
    //I2D *output;
    //edge* edges;
    int components = 0;
/*  int *colors = (int *) malloc(width*height*sizeof(int)); */
    //segments = (int *) malloc(height*width*sizeof(int));

    // smooth each color channel  
    smooth_im = imageReblur(im, imageOut, tempOut, kernel);

    //build graph
    //edges = (edge*)malloc(sizeof(edge)*width*height*4);

    for (y = 0; y < height; y++) 
    {
        for (x = 0; x < width; x++) 
        {
            segments[y*width+x] = -1;
            if (x < width-1) 
            {
        	    edges[num].a = y * width + x;
	            edges[num].b = y * width + (x+1);
	            edges[num].w = diff(smooth_im, x, y, x+1, y);
	            num++;
            }

            if (y < height-1) 
            {
	            edges[num].a = y * width + x;
	            edges[num].b = (y+1) * width + x;
	            edges[num].w = diff(smooth_im, x, y, x, y+1);
	            num++;
            }

            if ((x < width-1) && (y < height-1)) 
            {
	            edges[num].a = y * width + x;
	            edges[num].b = (y+1) * width + (x+1);
	            edges[num].w = diff(smooth_im, x, y, x+1, y+1);
	            num++;
            }

            if ((x < width-1) && (y > 0)) 
            {
	            edges[num].a = y * width + x;
	            edges[num].b = (y-1) * width + (x+1);
	            edges[num].w = diff(smooth_im, x, y, x+1, y-1);
	            num++;
            }
        }
    }

    //free(smooth_im);

    // segment
    u = segment_graph(width*height, num, edges, c, edgeWeights, in, ind, u);
  
    // post process small components
    for (i = 0; i < num; i++) 
    {
        int a, b;
        a = find(u,edges[i].a);
        b = find(u,edges[i].b);
        if ((a != b) && ((u->elts[a].size < min_size) || (u->elts[b].size < min_size)))
            join(u, a, b);
    }

    //free(edges);
    arrayref(num_ccs,0) = u->num;

    // pick random colors for each component
    //output = iMallocHandle(height, width);

/*    srand(time(0));
    for (i = 0; i < width*height; i++)
    {
        float temp;
        temp = rand()/((float)RAND_MAX);
        colors[i] = (int)(temp*255);
    }
*/
   
    for (y = 0; y < height; y++) 
    {
        for (x = 0; x < width; x++) 
        {
            int comp;
            comp = find(u, y * width + x);
            if(segments[comp] == -1)
                segments[comp] = components++;
            subsref(output, y, x) = segments[comp];
        }
    }  
  
/*
    for (y = 0; y < height; y++) 
    {
        for (x = 0; x < width; x++) 
        {
            int comp;
            comp = find(u, y * width + x);
            subsref(output, y, x) = colors[comp];
        }
    }  
*/
    //free(u->elts);
    //free(u);
    //free(segments);
    
    return output;
}