void test_resize(char *filename) { image im = load_image(filename, 0,0, 3); image gray = grayscale_image(im); image sat2 = copy_image(im); saturate_image(sat2, 2); image sat5 = copy_image(im); saturate_image(sat5, .5); image exp2 = copy_image(im); exposure_image(exp2, 2); image exp5 = copy_image(im); exposure_image(exp5, .5); show_image(im, "Original"); show_image(gray, "Gray"); show_image(sat2, "Saturation-2"); show_image(sat5, "Saturation-.5"); show_image(exp2, "Exposure-2"); show_image(exp5, "Exposure-.5"); #ifdef OPENCV cvWaitKey(0); #endif }
cv::Mat applyThreshold(cv::Mat &img, int debug) { int bin_threshold = 180; cv::Mat grayscale_image(img.rows, img.cols, CV_8UC1, cvScalarAll(0)); cv::Mat threshold_image(img.rows, img.cols, CV_8UC1, cvScalarAll(0)); if (debug == 4) { cv::namedWindow("threshold_control_box", 1); cv::createTrackbar("bin_threshold", "threshold_control_box", &bin_threshold, 255); } cv::inRange(img, cv::Scalar(bin_threshold, bin_threshold, bin_threshold), cv::Scalar(256, 256, 256), threshold_image); return threshold_image; } // converting a given image into binary using a threshold_image
void test_resize(char *filename) { image im = load_image(filename, 0,0, 3); float mag = mag_array(im.data, im.w*im.h*im.c); printf("L2 Norm: %f\n", mag); image gray = grayscale_image(im); image sat2 = copy_image(im); saturate_image(sat2, 2); image sat5 = copy_image(im); saturate_image(sat5, .5); image exp2 = copy_image(im); exposure_image(exp2, 2); image exp5 = copy_image(im); exposure_image(exp5, .5); #ifdef GPU image r = resize_image(im, im.w, im.h); image black = make_image(im.w*2 + 3, im.h*2 + 3, 9); image black2 = make_image(im.w, im.h, 3); float *r_gpu = cuda_make_array(r.data, r.w*r.h*r.c); float *black_gpu = cuda_make_array(black.data, black.w*black.h*black.c); float *black2_gpu = cuda_make_array(black2.data, black2.w*black2.h*black2.c); shortcut_gpu(3, r.w, r.h, 1, r_gpu, black.w, black.h, 3, black_gpu); //flip_image(r); //shortcut_gpu(3, r.w, r.h, 1, r.data, black.w, black.h, 3, black.data); shortcut_gpu(3, black.w, black.h, 3, black_gpu, black2.w, black2.h, 1, black2_gpu); cuda_pull_array(black_gpu, black.data, black.w*black.h*black.c); cuda_pull_array(black2_gpu, black2.data, black2.w*black2.h*black2.c); show_image_layers(black, "Black"); show_image(black2, "Recreate"); #endif show_image(im, "Original"); show_image(gray, "Gray"); show_image(sat2, "Saturation-2"); show_image(sat5, "Saturation-.5"); show_image(exp2, "Exposure-2"); show_image(exp5, "Exposure-.5"); #ifdef OPENCV cvWaitKey(0); #endif }
cv::Mat applyCanny(cv::Mat &img, int debug) { cv::Mat grayscale_image(img.rows, img.cols, CV_8UC1, cvScalarAll(0)); cv::Mat canny_image(img.rows, img.cols, CV_8UC1, cvScalarAll(0)); cv::cvtColor(img, grayscale_image, CV_BGR2GRAY); int minthreshold = 180, maxthreshold = 255; if (debug == 3) { cv::namedWindow("canny_control_box", 1); cv::createTrackbar("minthreshold", "canny_control_box", &minthreshold, 255); cv::createTrackbar("maxthreshold", "canny_control_box", &maxthreshold, 255); } do { cv::Canny(grayscale_image, canny_image, minthreshold, maxthreshold, 3); if (debug == 3) { cv::imshow("canny_control_box", canny_image); } if (cv::waitKey(33) == 27) break; } while (debug == 3); return canny_image; } // converts the given image into a binary one using canny
void run_nightmare(int argc, char **argv) { srand(0); if(argc < 4){ fprintf(stderr, "usage: %s %s [cfg] [weights] [image] [layer] [options! (optional)]\n", argv[0], argv[1]); return; } char *cfg = argv[2]; char *weights = argv[3]; char *input = argv[4]; int max_layer = atoi(argv[5]); int range = find_int_arg(argc, argv, "-range", 1); int norm = find_int_arg(argc, argv, "-norm", 1); int rounds = find_int_arg(argc, argv, "-rounds", 1); int iters = find_int_arg(argc, argv, "-iters", 10); int octaves = find_int_arg(argc, argv, "-octaves", 4); float zoom = find_float_arg(argc, argv, "-zoom", 1.); float rate = find_float_arg(argc, argv, "-rate", .04); float thresh = find_float_arg(argc, argv, "-thresh", 1.); float rotate = find_float_arg(argc, argv, "-rotate", 0); float momentum = find_float_arg(argc, argv, "-momentum", .9); float lambda = find_float_arg(argc, argv, "-lambda", .01); char *prefix = find_char_arg(argc, argv, "-prefix", 0); int reconstruct = find_arg(argc, argv, "-reconstruct"); int smooth_size = find_int_arg(argc, argv, "-smooth", 1); network net = parse_network_cfg(cfg); load_weights(&net, weights); char *cfgbase = basecfg(cfg); char *imbase = basecfg(input); set_batch_network(&net, 1); image im = load_image_color(input, 0, 0); if(0){ float scale = 1; if(im.w > 512 || im.h > 512){ if(im.w > im.h) scale = 512.0/im.w; else scale = 512.0/im.h; } image resized = resize_image(im, scale*im.w, scale*im.h); free_image(im); im = resized; } float *features = 0; image update; if (reconstruct){ resize_network(&net, im.w, im.h); int zz = 0; network_predict(net, im.data); image out_im = get_network_image(net); image crop = crop_image(out_im, zz, zz, out_im.w-2*zz, out_im.h-2*zz); //flip_image(crop); image f_im = resize_image(crop, out_im.w, out_im.h); free_image(crop); printf("%d features\n", out_im.w*out_im.h*out_im.c); im = resize_image(im, im.w, im.h); f_im = resize_image(f_im, f_im.w, f_im.h); features = f_im.data; int i; for(i = 0; i < 14*14*512; ++i){ features[i] += rand_uniform(-.19, .19); } free_image(im); im = make_random_image(im.w, im.h, im.c); update = make_image(im.w, im.h, im.c); } int e; int n; for(e = 0; e < rounds; ++e){ fprintf(stderr, "Iteration: "); fflush(stderr); for(n = 0; n < iters; ++n){ fprintf(stderr, "%d, ", n); fflush(stderr); if(reconstruct){ reconstruct_picture(net, features, im, update, rate, momentum, lambda, smooth_size, 1); //if ((n+1)%30 == 0) rate *= .5; show_image(im, "reconstruction"); #ifdef OPENCV cvWaitKey(10); #endif }else{ int layer = max_layer + rand()%range - range/2; int octave = rand()%octaves; optimize_picture(&net, im, layer, 1/pow(1.33333333, octave), rate, thresh, norm); } } fprintf(stderr, "done\n"); if(0){ image g = grayscale_image(im); free_image(im); im = g; } char buff[256]; if (prefix){ sprintf(buff, "%s/%s_%s_%d_%06d",prefix, imbase, cfgbase, max_layer, e); }else{ sprintf(buff, "%s_%s_%d_%06d",imbase, cfgbase, max_layer, e); } printf("%d %s\n", e, buff); save_image(im, buff); //show_image(im, buff); //cvWaitKey(0); if(rotate){ image rot = rotate_image(im, rotate); free_image(im); im = rot; } image crop = crop_image(im, im.w * (1. - zoom)/2., im.h * (1.-zoom)/2., im.w*zoom, im.h*zoom); image resized = resize_image(crop, im.w, im.h); free_image(im); free_image(crop); im = resized; } }
void run_nightmare(int argc, char **argv) { srand(0); if(argc < 4){ fprintf(stderr, "usage: %s %s [cfg] [weights] [image] [layer] [options! (optional)]\n", argv[0], argv[1]); return; } char *cfg = argv[2]; char *weights = argv[3]; char *input = argv[4]; int max_layer = atoi(argv[5]); int range = find_int_arg(argc, argv, "-range", 1); int rounds = find_int_arg(argc, argv, "-rounds", 1); int iters = find_int_arg(argc, argv, "-iters", 10); int octaves = find_int_arg(argc, argv, "-octaves", 4); float zoom = find_float_arg(argc, argv, "-zoom", 1.); float rate = find_float_arg(argc, argv, "-rate", .04); float thresh = find_float_arg(argc, argv, "-thresh", 1.); float rotate = find_float_arg(argc, argv, "-rotate", 0); char *prefix = find_char_arg(argc, argv, "-prefix", 0); network net = parse_network_cfg(cfg); load_weights(&net, weights); char *cfgbase = basecfg(cfg); char *imbase = basecfg(input); set_batch_network(&net, 1); image im = load_image_color(input, 0, 0); if(0){ float scale = 1; if(im.w > 512 || im.h > 512){ if(im.w > im.h) scale = 512.0/im.w; else scale = 512.0/im.h; } image resized = resize_image(im, scale*im.w, scale*im.h); free_image(im); im = resized; } int e; int n; for(e = 0; e < rounds; ++e){ fprintf(stderr, "Iteration: "); fflush(stderr); for(n = 0; n < iters; ++n){ fprintf(stderr, "%d, ", n); fflush(stderr); int layer = max_layer + rand()%range - range/2; int octave = rand()%octaves; optimize_picture(&net, im, layer, 1/pow(1.33333333, octave), rate, thresh); } fprintf(stderr, "done\n"); if(0){ image g = grayscale_image(im); free_image(im); im = g; } char buff[256]; if (prefix){ sprintf(buff, "%s/%s_%s_%d_%06d",prefix, imbase, cfgbase, max_layer, e); }else{ sprintf(buff, "%s_%s_%d_%06d",imbase, cfgbase, max_layer, e); } printf("%d %s\n", e, buff); save_image(im, buff); //show_image(im, buff); //cvWaitKey(0); if(rotate){ image rot = rotate_image(im, rotate); free_image(im); im = rot; } image crop = crop_image(im, im.w * (1. - zoom)/2., im.h * (1.-zoom)/2., im.w*zoom, im.h*zoom); image resized = resize_image(crop, im.w, im.h); free_image(im); free_image(crop); im = resized; } }
/*----------------------------------------------------------------------------*/ int main(int argc, char* argv[]) { int loop, test = 0, error = 0, command = COMMAND_NONE; int gray = 0, view = 1, help = 0; char *psave = 0x0, *pname = 0x0, *pdata = 0x0; my1Image currimage, tempimage, *pimage; my1IFrame currframe, tempframe; /* print tool info */ printf("\n%s - %s (%s)\n",MY1APP_PROGNAME,MY1APP_PROGINFO,MY1APP_PROGVERS); printf(" => by [email protected]\n\n"); /* check program arguments */ if(argc>1) { for(loop=1;loop<argc;loop++) { if(argv[loop][0]=='-') /* options! */ { if(!strcmp(argv[loop],"--save")) { loop++; if(loop<argc) psave = argv[loop]; else printf("Cannot get save file name - NOT saving!\n"); } else if(!strcmp(argv[loop],"--cdata")) { loop++; if(loop<argc) pdata = argv[loop]; else printf("Cannot get C data file name - NOT writing!\n"); } else if(!strcmp(argv[loop],"--gray")) { gray = 1; } else if(!strcmp(argv[loop],"--hide")) { view = 0; } else if(!strcmp(argv[loop],"--help")) { help = 1; } else { printf("Unknown option '%s'!\n",argv[loop]); } } else /* not an option? */ { /* first non-option must be file name! */ if(!pname) { pname = argv[loop]; continue; } /* then check for command! */ if(!strcmp(argv[loop],"laplace1")) { command = COMMAND_LAPLACE1; gray = 1; } else if(!strcmp(argv[loop],"sobelx")) { command = COMMAND_SOBELX; gray = 1; } else if(!strcmp(argv[loop],"sobely")) { command = COMMAND_SOBELY; gray = 1; } else if(!strcmp(argv[loop],"sobelall")) { command = COMMAND_SOBELALL; gray = 1; } else if(!strcmp(argv[loop],"laplace2")) { command = COMMAND_LAPLACE2; gray = 1; } else if(!strcmp(argv[loop],"gauss")) { command = COMMAND_GAUSS; gray = 1; } else { printf("Unknown parameter %s!\n",argv[loop]); continue; } /* warn if overriding previous command! */ if(command) { printf("Warning! Command '%s' overrides '%s'!\n", argv[loop],argv[test]); } test = loop; } } } /* check if user requested help */ if(help) { about(); return 0; } /** check input filename */ if(!pname) { printf("No filename given! Aborting!\n"); return ERROR_GENERAL; } /* initialize image & frame*/ initimage(&currimage); initimage(&tempimage); initframe(&currframe); initframe(&tempframe); /* try to open file */ if((error=load_image(&currimage,pname))<0) { return error; } /* display basic info */ printf("Input image: %s\n",pname); print_image_info(&currimage); /* convert grayscale if requested/required */ if(gray) grayscale_image(&currimage); /* process command */ switch(command) { case COMMAND_LAPLACE1: { createimage(&tempimage,currimage.height,currimage.width); laplace_image(&currimage,&tempimage); break; } case COMMAND_SOBELX: { createimage(&tempimage,currimage.height,currimage.width); sobel_x_image(&currimage,&tempimage); break; } case COMMAND_SOBELY: { createimage(&tempimage,currimage.height,currimage.width); sobel_y_image(&currimage,&tempimage); break; } case COMMAND_SOBELALL: { createimage(&tempimage,currimage.height,currimage.width); sobel_image(&currimage,&tempimage,0x0); break; } case COMMAND_LAPLACE2: { createimage(&tempimage,currimage.height,currimage.width); createframe(&currframe,currimage.height,currimage.width); createframe(&tempframe,currimage.height,currimage.width); image2frame(&currimage,&currframe,0); laplace_frame(&currframe,&tempframe); frame2image(&tempframe,&tempimage,1); break; } case COMMAND_GAUSS: { createimage(&tempimage,currimage.height,currimage.width); createframe(&currframe,currimage.height,currimage.width); createframe(&tempframe,currimage.height,currimage.width); image2frame(&currimage,&currframe,0); gauss_frame(&currframe,&tempframe,1.0,0x0); frame2image(&tempframe,&tempimage,1); break; } } if(!tempimage.length) pimage = &currimage; else pimage = &tempimage; printf("Check image:\n"); print_image_info(pimage); /** save results if requested */ if(psave) { printf("Saving image data to %s...\n",psave); error=save_image(pimage,psave); view = 0; } if(pdata) { printf("Saving C data to %s...\n",pdata); error=cdata_image(pimage,pdata); } /* view image if no request to hide! .. and NOT saving! */ if(view) view_image(pimage); /* cleanup */ freeframe(&currframe); freeframe(&tempframe); freeimage(&currimage); freeimage(&tempimage); return error; }