From f618466c25d43f3bae9e40920273bf77de1e1149 Mon Sep 17 00:00:00 2001 From: leochanj105 Date: Mon, 19 Oct 2020 23:09:30 -0400 Subject: initial sd-vbs initial sd-vbs add sd-vbs sd-vbs --- .../multi_ncut/src/c/.computeMultiW.c.swp | Bin 0 -> 12288 bytes .../multi_ncut/src/c/script_multi_ncut.c | 90 +++++++++++++ SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c | 95 ++++++++++++++ SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c | 142 +++++++++++++++++++++ SD-VBS/benchmarks/multi_ncut/src/c/segment.h | 46 +++++++ .../multi_ncut/src/matlab/script_run_profile.m | 78 +++++++++++ 6 files changed, 451 insertions(+) create mode 100644 SD-VBS/benchmarks/multi_ncut/src/c/.computeMultiW.c.swp create mode 100644 SD-VBS/benchmarks/multi_ncut/src/c/script_multi_ncut.c create mode 100644 SD-VBS/benchmarks/multi_ncut/src/c/segment-graph.c create mode 100644 SD-VBS/benchmarks/multi_ncut/src/c/segment-image.c create mode 100644 SD-VBS/benchmarks/multi_ncut/src/c/segment.h create mode 100755 SD-VBS/benchmarks/multi_ncut/src/matlab/script_run_profile.m (limited to 'SD-VBS/benchmarks/multi_ncut/src') 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 Binary files /dev/null and b/SD-VBS/benchmarks/multi_ncut/src/c/.computeMultiW.c.swp 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 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include "segment.h" +#include "extra.h" +#define MAX_EDGE 500000 +int main(int argc, char* argv[]) +{ + SET_UP + float sigma = 0.6; + float k = 4; + int min_size = 10; + char im1[256]; + int num_ccs[1] = {0}; + I2D *out; + I2D* im; + I2D* seg; + I2D* kernel; + I2D* ind; + I2D* output; + int ret; + int *segments; + edge* edges; + universe* u; + + F2D *imageOut, *tempOut; + F2D *edgeWeights, *in; + printf("Input image: "); + scanf("%s", im1); + im = readImage(im1); + + + int height, width, num_edges, num_vertices; + height = im->height; + width = im->width; + num_edges = MAX_EDGE; + num_vertices = width * height; + segments = (int *) malloc(height*width*sizeof(int)); + edges = (edge*)malloc(sizeof(edge)*width*height*4); + imageOut = fSetArray(height, width, 0); + tempOut = fSetArray(height, width, 0); + kernel = iMallocHandle(1,5); + edgeWeights = fMallocHandle(1, num_edges); + in = fDeepCopy(edgeWeights); + ind = fMallocHandle(1, num_edges); + u = (universe*)malloc(sizeof(universe)); + u->elts = (uni_elt*)malloc(sizeof(uni_elt)*num_vertices); + output = iMallocHandle(height, width); + + printf("start\n"); + for_each_job{ + seg = segment_image(im, sigma, k, min_size, num_ccs, segments, edges, imageOut, tempOut, kernel, edgeWeights, in, ind, u, output); + out = seg; + } + printf("end..\n"); +#ifdef CHECK + /** Self checking - use expected.txt from data directory **/ + { + int ret=0; + float tol = 0; + +#ifdef GENERATE_OUTPUT + writeMatrix(out, argv[1]); +#endif + + ret = selfCheck(out, "./expected_C.txt", tol); + if(ret < 0) + printf("Error in Multi N Cut\n"); + } +#endif + + iFreeHandle(im); + free(edges); + free(segments); + free(tempOut); + free(imageOut); + free(edgeWeights); + free(in); + free(ind); + free(output); + free(u->elts); + free(u); + WRITE_TO_FILE + return 0; +} + + 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 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include "segment.h" + +// threshold function +#define THRESHOLD(size, c) (c/size) + +int find(universe* u, int x) +{ + int y=x; + while (y != u->elts[y].p) + y = u->elts[y].p; + u->elts[x].p = y; + return y; +} + +void join(universe* u, int x, int y) +{ + if (u->elts[x].rank > u->elts[y].rank) + { + u->elts[y].p = x; + u->elts[x].size += u->elts[y].size; + } + else + { + u->elts[x].p = y; + u->elts[y].size += u->elts[x].size; + if (u->elts[x].rank == u->elts[y].rank) + u->elts[y].rank++; + } + u->num--; + return ; +} + +universe *segment_graph(int num_vertices, int num_edges, edge *edges, float c, F2D* edgeWeights, F2D* in, I2D* ind, + universe* u) +{ + float threshold[num_vertices]; + int i, a, b, j, k; + //universe *u; + //F2D *edgeWeights; + I2D *indices; + + edgeWeights = fResetHandle(edgeWeights,1,num_edges); + + for(i=0; ielts = (uni_elt*)malloc(sizeof(uni_elt)*num_vertices); + u->num = num_vertices; + for(i=0; ielts[i].rank = 0; + u->elts[i].size = 1; + u->elts[i].p = i; + } + + // init thresholds + for (i = 0; i < num_vertices; i++) + arrayref(threshold,i) = THRESHOLD(1,c); + + // for each edge, in non-decreasing weight order... + for (i = 0; i < num_edges; i++) + { + edge *pedge = &edges[ asubsref(indices,i) ]; + + // components conected by this edge + a = find(u, pedge->a); + b = find(u, pedge->b); + if (a != b) + { + if ((pedge->w <= arrayref(threshold,a)) && (pedge->w <= arrayref(threshold,b))) + { + join(u, a, b); + a = find(u, a); + arrayref(threshold,a) = pedge->w + THRESHOLD(u->elts[a].size, c); + } + } + } + + //fFreeHandle(edgeWeights); + //iFreeHandle(indices); + + return u; +} + 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 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include +#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; +} + + 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 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#ifndef SEGMENT +#define SEGMENT + +#include "sdvbs_common.h" + +typedef struct +{ + float w; + int a, b; +} edge; + +typedef struct +{ + int rank; + int p; + int size; +} uni_elt; + +typedef struct +{ + uni_elt *elts; + int num; +}universe; + +/* use imRef to access image data. */ +#define imRef(im, x, y) (im->data[y*im->width+x]) + +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); +universe *segment_graph(int num_vertices, int num_edges, edge *edges, float c, F2D* edgeWeights, F2D* in, I2D* ind, + universe* u); +void join(universe* u, int x, int y); +int find(universe* u, int x); +float diff(F2D *r, int x1, int y1, int x2, int y2); + + +#endif + + diff --git a/SD-VBS/benchmarks/multi_ncut/src/matlab/script_run_profile.m b/SD-VBS/benchmarks/multi_ncut/src/matlab/script_run_profile.m new file mode 100755 index 0000000..7624137 --- /dev/null +++ b/SD-VBS/benchmarks/multi_ncut/src/matlab/script_run_profile.m @@ -0,0 +1,78 @@ +% MNcutDemo.m +% created by song, 06/13/2005 +% an exmaple of how to use and display MNcut + + +function script_run_profile(dataDir, resultDir, type, common, toolDir) + +%% Input parameters + +if(nargin == 0) + data_set='test'; +end +if(nargin <=2) + imageSize = 100; +end + +num_segs = 20; +if(strcmp(type, 'test')) + num_segs = 1; +elseif(strcmp(type, 'sim_fast')) + num_segs = 1; +elseif(strcmp(type, 'sim')) + num_segs = 2; +elseif(strcmp(type, 'sqcif')) + num_segs = 3; +elseif(strcmp(type, 'qcif')) + num_segs = 4; +elseif(strcmp(type, 'cif')) + num_segs = 8; +elseif(strcmp(type, 'vga')) + num_segs = 13; +elseif(strcmp(type, 'wuxga')) + num_segs = 32; +end + +path(path, [toolDir,'/MultiNcut']); +path(path, common); + +img_filename = [dataDir, '/1.bmp']; +%I=imread(img_filename); +%I = rgb2gray(I); + +I = readImage(img_filename); + +%% Self check params +tol = 0.1; +elapsed = zeros(1,2); +rows = size(I,1); +cols = size(I,2); +fprintf(1,'Input size\t\t- (%dx%d)\n', rows, cols); + +%% Timing +start = photonStartTiming; + +[SegLabel,eigenVectors,eigenValues]= MNcut(I,num_segs); + +%% Timing +stop = photonEndTiming; + +temp = photonReportTiming(start, stop); +elapsed(1) = elapsed(1) + temp(1); +elapsed(2) = elapsed(2) + temp(2); + +%% Self checking +writeMatrix(SegLabel, dataDir); + +%% Timing +photonPrintTiming(elapsed); + +for j=1:size(SegLabel,3), + [gx,gy] = gradient(SegLabel(:,:,j)); + bw = (abs(gx)>0.1) + (abs(gy) > 0.1); + + figure(1);clf; J1=showmask(double(I),bw); imagesc(J1);axis image; axis off; + set(gca, 'Position', [0 0 1 1]); +end + + -- cgit v1.2.2