From 06cdf7fce6018213936cae86ecf03d8baeb05b0b Mon Sep 17 00:00:00 2001 From: mutantturkey Date: Tue, 3 Jul 2012 13:34:42 -0400 Subject: formatting and added a flag to the background deriver --- fly-tools/background/main.c | 71 ++++++++++++++++++++++++++++------- fly-tools/mask/main.c | 91 ++++++++++++++++++++++----------------------- 2 files changed, 102 insertions(+), 60 deletions(-) diff --git a/fly-tools/background/main.c b/fly-tools/background/main.c index 638e89f..96a2692 100644 --- a/fly-tools/background/main.c +++ b/fly-tools/background/main.c @@ -5,7 +5,7 @@ #include #include #include - +#include #define round(x)((int)((x)+0.5)) #define ThrowWandException(wand) { char *description; ExceptionType severity; description=MagickGetException(wand,&severity); (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); description=(char *) MagickRelinquishMemory(description); exit(-1); } @@ -27,10 +27,11 @@ int main(int argc, char **argv ) { char *sample_file = NULL; char *output_file = NULL; char *image_list = NULL; + bool histogram = false; int c; int opterr = 0; - while ((c = getopt (argc, argv, "s:i:o:h")) != -1) + while ((c = getopt (argc, argv, "s:i:o:mh")) != -1) switch (c) { case 's': sample_file = optarg; @@ -41,6 +42,9 @@ int main(int argc, char **argv ) { case 'o': output_file = optarg; break; + case 'm': + histogram = true; + break; case 'h': puts(usage); exit(1); @@ -167,24 +171,65 @@ int main(int argc, char **argv ) { // calculate histograms - for (j = 0; j < height; j++) { - for (k = 0; k < width; k++) { + if (!histogram) { + for (j = 0; j < height; j++) { + for (k = 0; k < width; k++) { + + uint8_t histr[256] = { 0 }, histg[256] = { 0 }, histb[256] = { 0 }; + for (i = 0; i < nImages; i++) { + histr[array[i][j][k][0]] += 1; + histg[array[i][j][k][1]] += 1; + histb[array[i][j][k][2]] += 1; + } + + output[j][k][0] = findmax(histr, 256); + output[j][k][1] = findmax(histg, 256); + output[j][k][2] = findmax(histb, 256); + + printf("out (%d,%d,%d,%d,%d) \n ", j, k, output[j][k][0], output[j][k][1], output[j][k][2]); - uint8_t histr[256] = { 0 }, histg[256] = { 0 }, histb[256] = { 0 }; - for (i = 0; i < nImages; i++) { - histr[array[i][j][k][0]] += 1; - histg[array[i][j][k][1]] += 1; - histb[array[i][j][k][2]] += 1; } + } + } else { + for (j = 0; j < height; j++) { + for (k = 0; k < width; k++) { + + gsl_histogram * r = gsl_histogram_alloc (nImages); + gsl_histogram * g = gsl_histogram_alloc (nImages); + gsl_histogram * b = gsl_histogram_alloc (nImages); + + gsl_histogram_set_ranges_uniform (r, 0, 255); + gsl_histogram_set_ranges_uniform (g, 0, 255); + gsl_histogram_set_ranges_uniform (b, 0, 255); + + for (i = 0; i < nImages; i++) { + gsl_histogram_increment (r, array[i][j][k][0]); + gsl_histogram_increment (g, array[i][j][k][1]); + gsl_histogram_increment (b, array[i][j][k][2]); + } - output[j][k][0] = findmax(histr, 256); - output[j][k][1] = findmax(histg, 256); - output[j][k][2] = findmax(histb, 256); - printf("out (%d,%d,%d,%d,%d) \n ", j, k, output[j][k][0], output[j][k][1], output[j][k][2]); + size_t red = gsl_histogram_max_val(r); + size_t green = gsl_histogram_max_val(r); + size_t blue = gsl_histogram_max_val(r); + output[j][k][0] = array[red][j][k][0]; + output[j][k][1] = array[blue][j][k][1]; + output[j][k][2] = array[green][j][k][2]; + gsl_histogram_free (r); + gsl_histogram_free (g); + gsl_histogram_free (b); + printf("out (%d,%d,%d,%d,%d) \n ", j, k, output[j][k][0], output[j][k][1], output[j][k][2]); + + } } } + pixels = NewPixelWand(); + p_out = NewPixelWand(); + PixelSetColor(p_out,"white"); + + output_wand = NewMagickWand(); + MagickNewImage(output_wand,width,height,p_out); pixels = NewPixelWand(); p_out = NewPixelWand(); diff --git a/fly-tools/mask/main.c b/fly-tools/mask/main.c index ae8064c..5f7744f 100644 --- a/fly-tools/mask/main.c +++ b/fly-tools/mask/main.c @@ -18,30 +18,27 @@ char *output_folder = NULL; void convert_image(char *file) { - MagickWand *mask = NewMagickWand(); - char *output_name = malloc(256); - if(MagickReadImage(mask, file) == MagickFalse) { - ThrowWandException(mask); - return; + MagickWand *mask = NewMagickWand(); + char *output_name = malloc(256); + if(MagickReadImage(mask, file) == MagickFalse) { + ThrowWandException(mask); + return; } - // convert \\\( -composite -compose Difference $output_dir/Masks/$setname/Background.png {} \\\) \\\( -contrast-stretch 90%x0% \\\) \\\( -threshold 30% \\\) $output_dir/Masks/$setname/Masks/{/} - MagickCompositeImage(mask, background, DifferenceCompositeOp, 0, 0); -// MagickContrastStretchImage(mask, 90000, 0); MagickAutoLevelImage(mask); - MagickThresholdImage(mask, 30000); + MagickThresholdImage(mask, 30000); - sprintf(output_name, "%s%s", output_folder, basename(file)); + sprintf(output_name, "%s%s", output_folder, basename(file)); if(MagickWriteImages(mask, output_name, MagickTrue) == MagickFalse) { - ThrowWandException(mask); - } + ThrowWandException(mask); + } - mask = DestroyMagickWand(mask); + mask = DestroyMagickWand(mask); - printf("output written to: %s \n", output_name); - free(output_name); - free(file); + printf("output written to: %s \n", output_name); + free(output_name); + free(file); } int main( int argc, char **argv){ @@ -70,48 +67,48 @@ int main( int argc, char **argv){ exit(1); break; default: - break; -} + break; + } if( background_file == NULL || image_list == NULL || output_folder == NULL ) { puts(usage); exit(1); - } - MagickBooleanType status; + } + MagickBooleanType status; MagickWandGenesis(); background = NewMagickWand(); status=MagickReadImage(background, background_file); if (status == MagickFalse) { - puts("background could not load error"); - exit(0); - } - - thpool_t* threadpool; /* make a new thread pool structure */ - threadpool=thpool_init(4); /* initialise it to 4 number of threads */ - - char filename[256]; - char *temp; - FILE *f = fopen ( image_list, "r" ); - if ( f != NULL ) { - while ( fgets ( filename, sizeof(filename), f ) != NULL ) { - temp = strchr(filename, '\n'); + puts("background could not load error"); + exit(0); + } + + thpool_t* threadpool; /* make a new thread pool structure */ + threadpool=thpool_init(4); /* initialise it to 4 number of threads */ + + char filename[256]; + char *temp; + FILE *f = fopen ( image_list, "r" ); + if ( f != NULL ) { + while ( fgets ( filename, sizeof(filename), f ) != NULL ) { + temp = strchr(filename, '\n'); if (temp != NULL) *temp = '\0'; - char *filename_r = malloc(256); - strncpy(filename_r, filename, sizeof(filename)); - printf("add work: %s \n", filename); - - thpool_add_work(threadpool, (void*)convert_image, (void*)filename_r); - } - fclose ( f ); - } - else { - exit(0); - } - + char *filename_r = malloc(256); + strncpy(filename_r, filename, sizeof(filename)); + printf("add work: %s \n", filename); + + thpool_add_work(threadpool, (void*)convert_image, (void*)filename_r); + } + fclose ( f ); + } + else { + exit(0); + } + puts("Will kill threadpool"); - thpool_destroy(threadpool); + thpool_destroy(threadpool); MagickWandTerminus(); - return 0; + return 0; } -- cgit v1.2.3