/* compute local smoothness weight as a sigmoid on image gradient*/ image_t* compute_dpsis_weight(color_image_t *im, float coef, const convolution_t *deriv) { image_t* lum = image_new(im->width, im->height), *lum_x = image_new(im->width, im->height), *lum_y = image_new(im->width, im->height); int i; // ocompute luminance v4sf *im1p = (v4sf*) im->c1, *im2p = (v4sf*) im->c2, *im3p = (v4sf*) im->c3, *lump = (v4sf*) lum->data; for( i=0 ; i<im->height*im->stride/4 ; i++){ *lump = (0.299f*(*im1p) + 0.587f*(*im2p) + 0.114f*(*im3p))/255.0f; lump+=1; im1p+=1; im2p+=1; im3p+=1; } // compute derivatives with five-point tencil convolve_horiz(lum_x, lum, deriv); convolve_vert(lum_y, lum, deriv); // compute lum norm lump = (v4sf*) lum->data; v4sf *lumxp = (v4sf*) lum_x->data, *lumyp = (v4sf*) lum_y->data; for( i=0 ; i<lum->height*lum->stride/4 ; i++){ *lump = -coef*__builtin_ia32_sqrtps( (*lumxp)*(*lumxp) + (*lumyp)*(*lumyp)); lump[0][0] = 0.5f*expf(lump[0][0]); lump[0][1] = 0.5f*expf(lump[0][1]); lump[0][2] = 0.5f*expf(lump[0][2]); lump[0][3] = 0.5f*expf(lump[0][3]); lump+=1; lumxp+=1; lumyp+=1; } image_delete(lum_x); image_delete(lum_y); return lum; }
/* resize an image with bilinear interpolation to fit the new weidht, height ; reallocation is done if necessary */ void image_resize_bilinear_newsize(image_t *dst, const image_t *src, const int new_width, const int new_height){ resize_if_needed_newsize(dst,new_width,new_height); if(new_width < new_height){ image_t *tmp = image_new(new_width,src->height); image_resize_horiz(tmp,src); image_resize_vert(dst,tmp); image_delete(tmp); }else{ image_t *tmp = image_new(src->width,new_height); image_resize_vert(tmp,src); image_resize_horiz(dst,tmp); image_delete(tmp); } }
void image_fits_get_info(char *filename, double *ra_ptr, double *dec_ptr, double *exp_ptr, u_int64_t *number_ptr, time_str *time_ptr) { fitsfile *fits; image_str *image = image_create(0, 0); int status = 0; /* Error code for CFITSIO library */ fits_open_file(&fits, filename, READONLY, &status); /* Read all keywords we may need */ image_keywords_from_fits(image, fits); if(exp_ptr) *exp_ptr = image_keyword_get_double(image, "EXPOSURE"); if(ra_ptr) *ra_ptr = 15.0*image_keyword_get_sexagesimal(image, "RA"); if(dec_ptr) *dec_ptr = image_keyword_get_sexagesimal(image, "DEC"); if(time_ptr) *time_ptr = time_str_from_date_time(image_keyword_get_string(image, "TIME")); if(number_ptr) *number_ptr = image_keyword_get_int64(image, "FRAMENUMBER"); fits_close_file(fits, &status); image_delete(image); }
void texture_write_to_file(texture* t, char* filename){ image* i = texture_get_image(t); image_write_to_file(i, filename); image_delete(i); }
void imagetest_write(void) { struct image *image; uint32_t i; struct rect rect; image = image_new(IMAGE_TYPE_8, 640, 480); image_putline(image, 20, 460, 620, 20, 60); image_putellipse(image, 320, 240, 300, 200, 64, 180); image_putcircle(image, 320, 240, 80, 64, 195); rect.x = 60; rect.y = 300; rect.w = 580; rect.h = 180; image_putrect(image, &rect, 80); for(i = 0; i < 256; i++) image_putpixel(image, 20 + i, 50, i); /* image_putstr(image, &image_font_6x10, 100, 40, 12, IMAGE_ALIGN_LEFT, "libchaos rocks!"); image_putstr(image, &image_font_6x10, 160, 80, 11, IMAGE_ALIGN_LEFT, "dschoint");*/ image_save_gif(image, "lala.gif"); image_delete(image); }
void image_list_delete(image_list_t *list) { int i; for(i=0;i<list->size;i++) image_delete(list->data[i]); free(list->data); free(list); }
/* return a resize version of the image with bilinear interpolation */ image_t *image_resize_bilinear(const image_t *src, const float scale){ const int width = src->width, height = src->height; const int newwidth = (int) (1.5f + (width-1) / scale); // 0.5f for rounding instead of flooring, and the remaining comes from scale = (dst-1)/(src-1) const int newheight = (int) (1.5f + (height-1) / scale); image_t *dst = image_new(newwidth,newheight); if(height*newwidth < width*newheight){ image_t *tmp = image_new(newwidth,height); image_resize_horiz(tmp,src); image_resize_vert(dst,tmp); image_delete(tmp); }else{ image_t *tmp = image_new(width,newheight); image_resize_vert(tmp,src); image_resize_horiz(dst,tmp); image_delete(tmp); } return dst; }
static int save_noise_to_file_thread(void* unused) { image* noise = perlin_noise_generate(512, 512, 8); image_tga_save_file(noise, "./perlin_noise.tga"); debug("Noise saved as perlin_noise.tga"); image_delete(noise); ui_spinner* save_spinner = ui_elem_get("save_spinner"); save_spinner->color.w = 0; currently_saving = false; return 0; }
texture* bmp_load_file( char* filename ) { image* i = image_bmp_load_file(filename); texture* t = texture_new(); glBindTexture(GL_TEXTURE_2D, texture_handle(t)); glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0 ); texture_set_image(t, i); texture_set_filtering_anisotropic(t); image_delete(i); return t; }
static int save_noise_to_file_thread(void* unused) { image* noise = perlin_noise_generate(512, 512, 8); image_tga_save_file(noise, "./perlin_noise.tga"); debug("Noise saved as perlin_noise.tga"); image_delete(noise); ui_spinner* save_spinner = ui_elem_get("save_spinner"); ui_rectangle* spinner_box = ui_elem_get("spinner_box"); save_spinner->color = vec4_new(1,1,1,0); spinner_box->color = vec4_new(0,0,0,0); spinner_box->border_color = vec4_new(1,1,1,0); currently_saving = false; return 0; }
void graphics_viewport_screenshot() { unsigned char* image_data = malloc( sizeof(unsigned char) * graphics_viewport_width() * graphics_viewport_height() * 4 ); glReadPixels( 0, 0, graphics_viewport_width(), graphics_viewport_height(), GL_BGRA, GL_UNSIGNED_BYTE, image_data ); image* i = image_new(graphics_viewport_width(), graphics_viewport_height(), image_data); free(image_data); timestamp(timestamp_string); screenshot_string[0] = '\0'; strcat(screenshot_string, "./corange_"); strcat(screenshot_string, timestamp_string); strcat(screenshot_string, ".tga"); image_write_to_file(i, screenshot_string); image_delete(i); }
float *bw_gist_scaletab(image_t *src, int w, int n_scale, const int *n_orientation) { int i; if(src->width < 8 || src->height < 8) { fprintf(stderr, "Error: bw_gist_scaletab() - Image not big enough !\n"); return NULL; } int numberBlocks = w; int tot_oris=0; for(i=0;i<n_scale;i++) tot_oris+=n_orientation[i]; image_t *img = image_cpy(src); image_list_t *G = create_gabor(n_scale, n_orientation, img->width, img->height); prefilt(img, 4); float *g = gist_gabor(img, numberBlocks, G); for(i = 0; i < tot_oris*w*w; i++) { if(!finite(g[i])) { fprintf(stderr, "Error: bw_gist_scaletab() - descriptor not valid (nan or inf)\n"); free(g); g=NULL; break; } } image_list_delete(G); image_delete(img); return g; }
int main(int argc, char **argv){ if( argc<6){ if(argc>1) fprintf(stderr,"Error, not enough arguments\n"); usage(); exit(1); } // read arguments color_image_t *im1 = color_image_load(argv[1]); color_image_t *im2 = color_image_load(argv[2]); float_image edges = read_edges(argv[3], im1->width, im1->height); float_image matches = read_matches(argv[4]); const char *outputfile = argv[5]; // prepare variables epic_params_t epic_params; epic_params_default(&epic_params); variational_params_t flow_params; variational_params_default(&flow_params); image_t *wx = image_new(im1->width, im1->height), *wy = image_new(im1->width, im1->height); // read optional arguments #define isarg(key) !strcmp(a,key) int current_arg = 6; while(current_arg < argc ){ const char* a = argv[current_arg++]; if( isarg("-h") || isarg("-help") ) usage(); else if( isarg("-nw") ) strcpy(epic_params.method, "NW"); else if( isarg("-p") || isarg("-prefnn") ) epic_params.pref_nn = atoi(argv[current_arg++]); else if( isarg("-n") || isarg("-nn") ) epic_params.nn = atoi(argv[current_arg++]); else if( isarg("-k") ) epic_params.coef_kernel = atof(argv[current_arg++]); else if( isarg("-i") || isarg("-iter") ) flow_params.niter_outer = atoi(argv[current_arg++]); else if( isarg("-a") || isarg("-alpha") ) flow_params.alpha= atof(argv[current_arg++]); else if( isarg("-g") || isarg("-gamma") ) flow_params.gamma= atof(argv[current_arg++]); else if( isarg("-d") || isarg("-delta") ) flow_params.delta= atof(argv[current_arg++]); else if( isarg("-s") || isarg("-sigma") ) flow_params.sigma= atof(argv[current_arg++]); else if( isarg("-sintel") ){ epic_params.pref_nn= 25; epic_params.nn= 160; epic_params.coef_kernel = 1.1f; flow_params.niter_outer = 5; flow_params.alpha = 1.0f; flow_params.gamma = 0.72f; flow_params.delta = 0.0f; flow_params.sigma = 1.1f; } else if( isarg("-kitti") ){ epic_params.pref_nn= 25; epic_params.nn= 160; epic_params.coef_kernel = 1.1f; flow_params.niter_outer = 2; flow_params.alpha = 1.0f; flow_params.gamma = 0.77f; flow_params.delta = 0.0f; flow_params.sigma = 1.7f; } else if( isarg("-middlebury") ){ epic_params.pref_nn= 15; epic_params.nn= 65; epic_params.coef_kernel = 0.2f; flow_params.niter_outer = 25; flow_params.alpha = 1.0f; flow_params.gamma = 0.72f; flow_params.delta = 0.0f; flow_params.sigma = 1.1f; } else{ fprintf(stderr, "unknown argument %s", a); usage(); exit(1); } } // compute interpolation and energy minimization color_image_t *imlab = rgb_to_lab(im1); epic(wx, wy, imlab, &matches, &edges, &epic_params, 1); // energy minimization variational(wx, wy, im1, im2, &flow_params); // write output file and free memory writeFlowFile(outputfile, wx, wy); color_image_delete(im1); color_image_delete(imlab); color_image_delete(im2); free(matches.pixels); free(edges.pixels); image_delete(wx); image_delete(wy); return 0; }
static bool test_simple_errors(GLenum src_target, GLenum dst_target) { bool pass = true; GLuint i, src, src2, dst; src = image_create(src_target); dst = image_create(dst_target); /* Test all three combinations of incomplete src or dst */ glCopyImageSubData(src, src_target, 0, 0, 0, 0, dst, dst_target, 0, 0, 0, 0, 0, 0, 0); pass &= piglit_check_gl_error(GL_INVALID_OPERATION); image_storage(src_target, src, GL_RGBA8, 32, 32); assert(piglit_check_gl_error(GL_NO_ERROR)); glCopyImageSubData(src, src_target, 0, 0, 0, 0, dst, dst_target, 0, 0, 0, 0, 0, 0, 0); pass &= piglit_check_gl_error(GL_INVALID_OPERATION); image_storage(dst_target, dst, GL_RGBA8, 32, 32); assert(piglit_check_gl_error(GL_NO_ERROR)); /* We want to test with empty src but valid dst */ src2 = image_create(src_target); glCopyImageSubData(src2, src_target, 0, 0, 0, 0, dst, dst_target, 0, 0, 0, 0, 0, 0, 0); pass &= piglit_check_gl_error(GL_INVALID_OPERATION); /* This is no longer needed */ image_delete(src_target, src2); /* Section 18.3.2 (Copying Between Images) of the OpenGL 4.5 Core * Profile spec says: * * "An INVALID_VALUE error is generated if either name does not * correspond to a valid renderbuffer or texture object according * to the corresponding target parameter." */ if (src_target != GL_RENDERBUFFER_EXT) { for (i = 0; i < ARRAY_LENGTH(targets); ++i) { if (targets[i] == src_target) continue; /* here, targets[i] doesn't match src object's target */ glCopyImageSubData(src, targets[i], 0, 0, 0, 0, dst, dst_target, 0, 0, 0, 0, 0, 0, 0); pass &= piglit_check_gl_error(GL_INVALID_VALUE); if (!pass) return false; } } /* Section 18.3.2 (Copying Between Images) of the OpenGL 4.5 Core * Profile spec says: * * "An INVALID_VALUE error is generated if either name does not * correspond to a valid renderbuffer or texture object according * to the corresponding target parameter." */ if (dst_target != GL_RENDERBUFFER_EXT) { for (i = 0; i < ARRAY_LENGTH(targets); ++i) { if (targets[i] == dst_target) continue; /* here, targets[i] doesn't match dst object's target */ glCopyImageSubData(src, src_target, 0, 0, 0, 0, dst, targets[i], 0, 0, 0, 0, 0, 0, 0); pass &= piglit_check_gl_error(GL_INVALID_VALUE); if (!pass) return false; } } /* 4523 should be a bogus renderbuffer/texture */ glCopyImageSubData(4523, src_target, 0, 0, 0, 0, dst, dst_target, 0, 0, 0, 0, 0, 0, 0); pass &= piglit_check_gl_error(GL_INVALID_VALUE); glCopyImageSubData(src, src_target, 0, 0, 0, 0, 4523, dst_target, 0, 0, 0, 0, 0, 0, 0); pass &= piglit_check_gl_error(GL_INVALID_VALUE); /* Invalid level */ glCopyImageSubData(src, src_target, 5, 0, 0, 0, dst, dst_target, 0, 0, 0, 0, 0, 0, 0); pass &= piglit_check_gl_error(GL_INVALID_VALUE); glCopyImageSubData(src, src_target, 0, 0, 0, 0, dst, dst_target, 5, 0, 0, 0, 0, 0, 0); pass &= piglit_check_gl_error(GL_INVALID_VALUE); /* Region out of bounds */ glCopyImageSubData(src, src_target, 0, 7, 5, 2, dst, dst_target, 0, 0, 0, 0, 26, 25, 20); pass &= piglit_check_gl_error(GL_INVALID_VALUE); glCopyImageSubData(src, src_target, 0, 7, 5, 2, dst, dst_target, 0, 0, 0, 0, 25, 30, 20); pass &= piglit_check_gl_error(GL_INVALID_VALUE); glCopyImageSubData(src, src_target, 0, 7, 5, 2, dst, dst_target, 0, 0, 0, 0, 25, 24, 31); pass &= piglit_check_gl_error(GL_INVALID_VALUE); glCopyImageSubData(src, src_target, 0, 0, 0, 0, dst, dst_target, 0, 7, 5, 2, 26, 25, 20); pass &= piglit_check_gl_error(GL_INVALID_VALUE); glCopyImageSubData(src, src_target, 0, 0, 0, 0, dst, dst_target, 0, 7, 5, 2, 25, 30, 20); pass &= piglit_check_gl_error(GL_INVALID_VALUE); glCopyImageSubData(src, src_target, 0, 0, 0, 0, dst, dst_target, 0, 7, 5, 2, 25, 24, 31); pass &= piglit_check_gl_error(GL_INVALID_VALUE); image_delete(src_target, src); image_delete(dst_target, dst); return pass; }
void ttftest_write(const char *text, const char *font, const char *file, const char *bg, const char *color) { struct image *ttftext; struct image *gradient; struct ttf *ttf; struct color fg; struct rect drect; struct rect srect; log(ttftest_log, L_status, "---------- TrueType GIF renderer ----------", text); log(ttftest_log, L_status, "text: %s", text); log(ttftest_log, L_status, "font: %s", font); log(ttftest_log, L_status, "bg: %s", bg); log(ttftest_log, L_status, "file: %s", file); log(ttftest_log, L_status, "color: %s", color); log(ttftest_log, L_status, "-------------------------------------------"); ttf = ttf_new(font); if(ttf_open(ttf, font)) { log(ttftest_log, L_fatal, "Could not load font: %s", font); return; } ttf_calc(ttf, 11); ttf->style |= TTF_STYLE_BOLD; image_color_parse(&fg, color); ttftext = ttf_text_blended(ttf, text, &fg); gradient = image_load_gif(bg); if(gradient == NULL) { log(ttftest_log, L_fatal, "Could not load background image: %s", bg); return; } image_convert(gradient, IMAGE_TYPE_32); srect = ttftext->rect; drect = gradient->rect; drect.y = 1; drect.x = 6; image_blit_32to32(ttftext, &srect, gradient, &drect); image_convert(gradient, IMAGE_TYPE_8); if(gradient == NULL) { log(ttftest_log, L_warning, "Could not render test glyph."); } else { if(image_save_gif(gradient, file)) { log(ttftest_log, L_fatal, "Could not save output: %s", file); } image_delete(gradient); } }
int main(int argc, char *argv[]) #endif { /* C90 requires all vars to be declared at top of function */ CoiHandle entity; CoiHandle node; CoiHandle light; CoiHandle rendersystem; CoiHandle renderwindow; CoiHandle viewport; CoiHandle plane; CoiHandle plane_entity; CoiHandle plane_node; CoiHandle terrain_group; CoiHandle terrain_iterator; CoiHandle import_data; CoiHandle image; float direction[3]; float colour[4]; #if defined(LLCOI_TEST_USE_OPENINPUT) // Openinput oi_event evt; char openinput_window_params[100]; unsigned int windowHnd = 0; #if defined(PLATFORM_LINUX) Display *disp; Window win; unsigned int scrn; #endif #endif //LLCOI_TEST_USE_OPENINPUT keep_going = 1; long loop_x = 0; long loop_y = 0; // setup create_root("plugins.cfg", "ogre.cfg", "ogre.log"); if (!(restore_config() || show_config_dialog())) { return 1; } setup_resources("resources.cfg"); renderwindow = root_initialise(1, "Ogre Renderwindow"); set_default_num_mipmaps(5); initialise_all_resource_groups(); create_scene_manager("OctreeSceneManager", "The SceneManager"); myCamera = create_camera(get_scene_manager(), "mycam"); camera_set_position(myCamera, 1683, 50, 2116); camera_lookat(myCamera, 1963, -50, 1660); camera_set_near_clip_distance(myCamera, 1); camera_set_far_clip_distance(myCamera, 50000); viewport = add_viewport(myCamera); viewport_set_background_colour(viewport, 0, 0, 0); camera_set_aspect_ratio(myCamera, 800, 600); // entities plane = create_plane_from_normal(0, 1, 0, 0); mesh_manager_create_plane("ground", "General", plane, 1500, 1500, 20, 20, 1, 1, 5, 5, 0, 0, 1); plane_entity = create_entity(get_scene_manager(), "plane", "ground"); entity_set_material_name(plane_entity, "Dev/Red"); plane_node = create_child_scene_node(get_scene_manager(), "planenode"); scene_node_attach_entity(plane_node, plane_entity); entity = create_entity(get_scene_manager(), "OgreHead", "ogrehead.mesh"); node = create_child_scene_node(get_scene_manager(), "headNode"); scene_node_attach_entity(node, entity); scene_manager_set_ambient_light_rgb(get_scene_manager(), 0.5f, 0.5f, 0.5f); light = create_light(get_scene_manager(), "mainLight"); light_set_position(light, 20, 80, 50); // terrain create_terrain_global_options(); terrain_group = create_terrain_group(get_scene_manager(), ALIGN_X_Z, 513, 12000.0f); terrain_group_set_filename_convention(terrain_group, "BasicTutorial3Terrain", "dat"); terrain_group_set_origin(terrain_group, 0, 0, 0); // terrain defaults terrain_global_options_set_max_pixel_error(8); terrain_global_options_set_composite_map_distance(3000); terrain_global_options_set_light_map_direction_vector3(light_get_derived_direction(light)); terrain_global_options_set_composite_map_ambient_colour(scene_manager_get_ambient_light(get_scene_manager()));// sm terrain_global_options_set_composite_map_diffuse_colour(light_get_diffuse_colour(light));// light import_data = terrain_group_get_default_import_settings(terrain_group); terrain_group_import_data_set_terrain_size(import_data, 513); terrain_group_import_data_set_world_size(import_data, 12000.0f); terrain_group_import_data_set_input_scale(import_data, 600); terrain_group_import_data_set_min_batch_size(import_data, 33); terrain_group_import_data_set_max_batch_size(import_data, 65); terrain_group_import_data_resize_layers(import_data, 3); terrain_group_import_data_set_layer(import_data, 0, 100, "nvidia/dirt_grayrocky_diffusespecular.dds", "nvidia/dirt_grayrocky_normalheight.dds"); terrain_group_import_data_set_layer(import_data, 1, 30, "nvidia/grass_green-01_diffusespecular.dds", "nvidia/grass_green-01_normalheight.dds"); terrain_group_import_data_set_layer(import_data, 2, 200, "nvidia/growth_weirdfungus-03_diffusespecular.dds", "nvidia/growth_weirdfungus-03_normalheight.dds"); // define terrains for (loop_x; loop_x <= 0; ++loop_x) { for (loop_y; loop_y <= 0; ++loop_y) { if (resource_exists(terrain_group_get_resource_group(terrain_group), terrain_group_generate_filename(terrain_group, loop_x, loop_y))) { terrain_group_define_terrain(terrain_group, loop_x, loop_y); } else { image = create_image(); image_load(image, "terrain.png"); if (loop_x % 2 != 0) { image_flip_around_y(image); } if (loop_y % 2 != 0) { image_flip_around_x(image); } terrain_group_define_terrain_image(terrain_group, loop_x, loop_y, image); image_delete(image); image = NULL; } } } terrain_group_load_all_terrains(terrain_group, 1); // blend maps /* terrain_iterator = terrain_group_get_terrain_iterator(terrain_group); while(terrain_iterator_has_more_elements(terrain_iterator)) { CoiHandle terrain = terrain_iterator_get_next(terrain_iterator); int blend_map_size = terrain_get_layer_blend_map_size(terrain); CoiHandle blend_map_0 = terrain_get_layer_blend_map(terrain, 1); CoiHandle blend_map_1 = terrain_get_layer_blend_map(terrain, 2); float min_height_0 = 70; float fade_dist_0 = 40; float min_height_1 = 70; float fade_dist_1 = 15; float* blend_1 = terrain_layer_blend_map_get_blend_pointer(blend_map_1); for(unsigned int y = 0; y < blend_map_size; ++y) { for(unsigned int x = 0; x < blend_map_size; ++x) { float tx, ty; terrain_layer_blend_map_convert_image_to_terrain_space(x, y, &tx, &ty); float height = terrain_get_height_at_terrain_position(tx, ty); float val = (height - min_height_0) / fade_dist_0; val = math_clamp_f(val, 0.0f, 1.0f); val = (height - min_height_1) / fade_dist_1; val = math_clamp_f(val, 0.0f, 1.0f); *blend_1++ = val; } } terrain_layer_blend_map_dirty(blend_map_0); terrain_layer_blend_map_dirty(blend_map_1); terrain_layer_blend_map_update(blend_map_0); terrain_layer_blend_map_update(blend_map_1); } */ terrain_group_free_temporary_resources(terrain_group); // listeners add_frame_listener(frame_listener_test,EVENT_FRAME_RENDERING_QUEUED|EVENT_FRAME_STARTED); add_window_listener(renderwindow, window_event_listener_test); input_manager = create_input_system(render_window_get_hwnd(renderwindow)); input_listener = create_input_listener(); keyboard = create_keyboard_object(input_manager, 1); mouse = create_mouse_object(input_manager, 1); attach_keyboard_listener(keyboard, input_listener); attach_mouse_listener(mouse, input_listener); add_key_pressed_listener(input_listener, key_pressed_test); while(keep_going) { keyboard_capture(keyboard); mouse_capture(mouse); // Pump window messages for nice behaviour pump_messages(); // Render a frame render_one_frame(); if (render_window_closed(renderwindow)) { keep_going = 0; } } #if defined(LLCOI_TEST_USE_OPENINPUT) windowHnd = render_window_get_hwnd(renderwindow); #if defined(PLATFORM_LINUX) disp = XOpenDisplay( NULL ); scrn = DefaultScreen(disp); sprintf(openinput_window_params, "c:%u s:%u w:%u", (unsigned int)disp, (unsigned int)scrn, windowHnd); #else sprintf(openinput_window_params, "c:%u s:%u w:%u", 0, 0, windowHnd); #endif oi_init(openinput_window_params, 0); log_message("***************************"); log_message("*** All Systems online! ***"); log_message("***************************"); //render_loop(); while (keep_going) { // ask oi to wait for events oi_events_poll(&evt); switch(evt.type) { case OI_QUIT: // Quit keep_going = 0; break; case OI_KEYDOWN: // Keyboard button down //WTF?? Better way to check this, please.. if(evt.key.keysym.sym == OIK_ESC) keep_going = 0; break; default: break; } // Pump window messages for nice behaviour pump_messages(); // Render a frame render_one_frame(); if (render_window_closed()) { keep_going = 0; } } oi_close(); #endif //LLCOI_TEST_USE_OPENINPUT remove_window_listener(renderwindow); destroy_keyboard_object(input_manager, keyboard); destroy_mouse_object(input_manager, mouse); destroy_input_system(input_manager); release_engine(); return 0; }
/* It is represented as two images, the first one for horizontal smoothness, the second for vertical in dst_horiz, the pixel i,j represents the smoothness weight between pixel i,j and i,j+1 in dst_vert, the pixel i,j represents the smoothness weight between pixel i,j and i+1,j */ void compute_smoothness(image_t *dst_horiz, image_t *dst_vert, const image_t *uu, const image_t *vv, const image_t *dpsis_weight, const convolution_t *deriv_flow, const float half_alpha) { int w = uu->width, h = uu->height, s = uu->stride, i, j, offset; image_t *ux1 = image_new(w,h), *uy1 = image_new(w,h), *vx1 = image_new(w,h), *vy1 = image_new(w,h), *ux2 = image_new(w,h), *uy2 = image_new(w,h), *vx2 = image_new(w,h), *vy2 = image_new(w,h); // compute ux1, vx1, filter [-1 1] for( j=0 ; j<h ; j++) { offset = j*s; for( i=0 ; i<w-1 ; i++, offset++) { ux1->data[offset] = uu->data[offset+1] - uu->data[offset]; vx1->data[offset] = vv->data[offset+1] - vv->data[offset]; } } // compute uy1, vy1, filter [-1;1] for( j=0 ; j<h-1 ; j++) { offset = j*s; for( i=0 ; i<w ; i++, offset++) { uy1->data[offset] = uu->data[offset+s] - uu->data[offset]; vy1->data[offset] = vv->data[offset+s] - vv->data[offset]; } } // compute ux2, uy2, vx2, vy2, filter [-0.5 0 0.5] convolve_horiz(ux2,uu,deriv_flow); convolve_horiz(vx2,vv,deriv_flow); convolve_vert(uy2,uu,deriv_flow); convolve_vert(vy2,vv,deriv_flow); // compute final value, horiz for( j=0 ; j<h ; j++) { offset = j*s; for( i=0 ; i<w-1 ; i++, offset++) { float tmp = 0.5f*(uy2->data[offset]+uy2->data[offset+1]); float uxsq = ux1->data[offset]*ux1->data[offset] + tmp*tmp; tmp = 0.5f*(vy2->data[offset]+vy2->data[offset+1]); float vxsq = vx1->data[offset]*vx1->data[offset] + tmp*tmp; tmp = uxsq + vxsq; dst_horiz->data[offset] = (dpsis_weight->data[offset]+dpsis_weight->data[offset+1])*half_alpha / sqrt( tmp + epsilon_smooth ) ; } memset( &dst_horiz->data[j*s+w-1], 0, sizeof(float)*(s-w+1)); } // compute final value, vert for( j=0 ; j<h-1 ; j++) { offset = j*s; for( i=0 ; i<w ; i++, offset++) { float tmp = 0.5f*(ux2->data[offset]+ux2->data[offset+s]); float uysq = uy1->data[offset]*uy1->data[offset] + tmp*tmp; tmp = 0.5f*(vx2->data[offset]+vx2->data[offset+s]); float vysq = vy1->data[offset]*vy1->data[offset] + tmp*tmp; tmp = uysq + vysq; dst_vert->data[offset] = (dpsis_weight->data[offset]+dpsis_weight->data[offset+s])*half_alpha / sqrt( tmp + epsilon_smooth ) ; /*if( dpsis_weight->data[offset]<dpsis_weight->data[offset+s]) dst_vert->data[offset] = dpsis_weight->data[offset]*half_alpha / sqrt( tmp + epsilon_smooth ) ; else dst_vert->data[offset] = dpsis_weight->data[offset+s]*half_alpha / sqrt( tmp + epsilon_smooth ) ;*/ } } memset( &dst_vert->data[(h-1)*s], 0, sizeof(float)*s); image_delete(ux1); image_delete(uy1); image_delete(vx1); image_delete(vy1); image_delete(ux2); image_delete(uy2); image_delete(vx2); image_delete(vy2); }
/* compute the saliency of a given image */ image_t* saliency(const color_image_t *im, float sigma_image, float sigma_matrix ){ int width = im->width, height = im->height, filter_size; // smooth image color_image_t *sim = color_image_new(width, height); float *presmooth_filter = gaussian_filter(sigma_image, &filter_size); convolution_t *presmoothing = convolution_new(filter_size, presmooth_filter, 1); color_image_convolve_hv(sim, im, presmoothing, presmoothing); convolution_delete(presmoothing); free(presmooth_filter); // compute derivatives float deriv_filter[2] = {0.0f, -0.5f}; convolution_t *deriv = convolution_new(1, deriv_filter, 0); color_image_t *imx = color_image_new(width, height), *imy = color_image_new(width, height); color_image_convolve_hv(imx, sim, deriv, NULL); color_image_convolve_hv(imy, sim, NULL, deriv); convolution_delete(deriv); // compute autocorrelation matrix image_t *imxx = image_new(width, height), *imxy = image_new(width, height), *imyy = image_new(width, height); v4sf *imx1p = (v4sf*) imx->c1, *imx2p = (v4sf*) imx->c2, *imx3p = (v4sf*) imx->c3, *imy1p = (v4sf*) imy->c1, *imy2p = (v4sf*) imy->c2, *imy3p = (v4sf*) imy->c3, *imxxp = (v4sf*) imxx->data, *imxyp = (v4sf*) imxy->data, *imyyp = (v4sf*) imyy->data; int i; for(i = 0 ; i<height*im->stride/4 ; i++){ *imxxp = (*imx1p)*(*imx1p) + (*imx2p)*(*imx2p) + (*imx3p)*(*imx3p); *imxyp = (*imx1p)*(*imy1p) + (*imx2p)*(*imy2p) + (*imx3p)*(*imy3p); *imyyp = (*imy1p)*(*imy1p) + (*imy2p)*(*imy2p) + (*imy3p)*(*imy3p); imxxp+=1; imxyp+=1; imyyp+=1; imx1p+=1; imx2p+=1; imx3p+=1; imy1p+=1; imy2p+=1; imy3p+=1; } // integrate autocorrelation matrix float *smooth_filter = gaussian_filter(sigma_matrix, &filter_size); convolution_t *smoothing = convolution_new(filter_size, smooth_filter, 1); image_t *tmp = image_new(width, height); convolve_horiz(tmp, imxx, smoothing); convolve_vert(imxx, tmp, smoothing); convolve_horiz(tmp, imxy, smoothing); convolve_vert(imxy, tmp, smoothing); convolve_horiz(tmp, imyy, smoothing); convolve_vert(imyy, tmp, smoothing); convolution_delete(smoothing); free(smooth_filter); // compute smallest eigenvalue v4sf vzeros = {0.0f,0.0f,0.0f,0.0f}; v4sf vhalf = {0.5f,0.5f,0.5f,0.5f}; v4sf *tmpp = (v4sf*) tmp->data; imxxp = (v4sf*) imxx->data; imxyp = (v4sf*) imxy->data; imyyp = (v4sf*) imyy->data; for(i = 0 ; i<height*im->stride/4 ; i++){ (*tmpp) = vhalf*( (*imxxp)+(*imyyp) ) ; (*tmpp) = __builtin_ia32_sqrtps(__builtin_ia32_maxps(vzeros, (*tmpp) - __builtin_ia32_sqrtps(__builtin_ia32_maxps(vzeros, (*tmpp)*(*tmpp) + (*imxyp)*(*imxyp) - (*imxxp)*(*imyyp) ) ))); tmpp+=1; imxyp+=1; imxxp+=1; imyyp+=1; } image_delete(imxx); image_delete(imxy); image_delete(imyy); color_image_delete(imx); color_image_delete(imy); color_image_delete(sim); return tmp; }
int main(int argc, char* argv[]) { printf("[i] Start...\n"); char file_name[] = "test.bmp"; char* filename=file_name; if(argc >= 2) { filename = argv[1]; } printf("[i] file: %s\n", filename); image* img = image_create(320, 240, 3, CV_DEPTH_8U); if(!img) { printf("[!] Error: image_create()\n"); return -1; } image_delete(&img); // test image loading image* img2 = image_load(filename); printf("[i] image size: %dx%dx%d (%d)\n", img2->width, img2->height, img2->n_channels, img2->size); image_save(img2, "test2_load_save.bmp"); printf("[i] == Tests == \n"); #if 1 // copy printf("[i] image_copy \n"); img = image_create(img2->width, img2->height, img2->n_channels, CV_DEPTH_8U); image_copy(img2, img); image_save(img, "test2_copy.bmp"); // test convert image to grayscale printf("[i] image_convert_color \n"); image* img_gray = image_create(img2->width, img2->height, 1, CV_DEPTH_8U); gettimeofday(&t0, NULL); image_convert_color(img2, img_gray, CV_RGB2GRAY); gettimeofday(&t1, NULL); image_save(img_gray, "test3_gray.bmp"); // test borders detection printf("[i] image_thin_borders \n"); image* img_borders = NULL; gettimeofday(&t2, NULL); image_thin_borders(img_gray, &img_borders); gettimeofday(&t3, NULL); image_save(img_borders, "test4_thin_borders.bmp"); // min-max-loc printf("[i] image_min_max_loc \n"); double _min, _max; gettimeofday(&t4, NULL); image_min_max_loc(img_gray, &_min, &_max, NULL, NULL); gettimeofday(&t5, NULL); printf("[i] min=%0.2f max=%0.2f\n", _min, _max); // threshold printf("[i] image_threshold \n"); image* img_thr = image_create(img2->width, img2->height, 1, CV_DEPTH_8U); gettimeofday(&t6, NULL); image_threshold(img_gray, img_thr, 60); gettimeofday(&t7, NULL); image_save(img_thr, "test5_threshold.bmp"); #endif #if 1 // rotate180 printf("[i] image_rotate180 \n"); image_rotate180(img2); image_save(img2, "test6_rotate180.bmp"); image_rotate180(img2); // reflect vertical printf("[i] image_reflect_vertical \n"); image_reflect_vertical(img2); image_save(img2, "test7_reflect_vertical.bmp"); image_reflect_vertical(img2); #endif // rotate180 int colors_count; cv_point center; #if 1 // simple resize printf("[i] image_resize \n"); image *img_small = image_create(160, 120, 3, CV_DEPTH_8U); gettimeofday(&t8, NULL); image_resize(img2, img_small); gettimeofday(&t9, NULL); image_save(img_small, "test8_resize.bmp"); // image* img_small_gray = image_create(80, 60, 1, CV_DEPTH_8U); // image_convert_color(img_small, img_small_gray, CV_RGB2GRAY); // image* img_small_borders = NULL; // image_thin_borders(img_small_gray, &img_small_borders); // image_save(img_small_borders, "test_resize_thin_borders.bmp"); #if 1 // k-meanes colorer printf("[i] image_kmeans_colorer \n"); image* img_kmeanes = image_create(160, 120, 3, CV_DEPTH_8U); image* img_kmeanes_idx = image_create(160, 120, 1, CV_DEPTH_8U); #define CLUSTER_COUNT 10 int cluster_count = CLUSTER_COUNT; cv_color_cluster clusters[CLUSTER_COUNT]; gettimeofday(&t10, NULL); colors_count = image_kmeans_colorer(img_small, img_kmeanes, img_kmeanes_idx, clusters, cluster_count); gettimeofday(&t11, NULL); printf("[i] colors count: %d\n", colors_count); image_save(img_kmeanes, "test_kmeanscolorer.bmp"); #if 0 print_color_clusters(clusters, CLUSTER_COUNT); printf("[i] === colors clusters after sort:\n"); sort_color_clusters_by_count(clusters, CLUSTER_COUNT); print_color_clusters(clusters, CLUSTER_COUNT); #endif image_delete(&img_kmeanes); image_delete(&img_kmeanes_idx); #endif // k-meanes colorer image_delete(&img_small); // image_delete(&img_small_gray); // image_delete(&img_small_borders); #endif // simple resize #if 1 // HSV printf("[i] image_hsv2rgb \n"); image* img_hsv = image_create(img2->width, img2->height, 3, CV_DEPTH_8U); image* img_bgr = image_create(img2->width, img2->height, 3, CV_DEPTH_8U); gettimeofday(&t12, NULL); image_rgb2hsv(img2, img_hsv); gettimeofday(&t13, NULL); image_hsv2rgb(img_hsv, img_bgr); image_save(img_hsv, "test9_rgb2hsv.bmp"); image_save(img_bgr, "test9_hsv2rgb.bmp"); image_delete(&img_hsv); image_delete(&img_bgr); #endif // HSV #if 1 // hsv colorer printf("[i] image_hsv_colorer \n"); image* img_hsv_col = image_create(img2->width, img2->height, 3, CV_DEPTH_8U); image* img_hsv_idx = image_create(img2->width, img2->height, 1, CV_DEPTH_8U); #define COLORS_COUNT 10 cv_color_cluster clusters2[COLORS_COUNT]; gettimeofday(&t14, NULL); colors_count = image_hsv_colorer(img2, img_hsv_col, img_hsv_idx, clusters2, COLORS_COUNT); gettimeofday(&t15, NULL); printf("[i] colors count: %d\n", colors_count); image_save(img_hsv_col, "test_hsvcolorer.bmp"); #if 1 print_color_clusters(clusters2, COLORS_COUNT); printf("[i] === colors clusters after sort:\n"); sort_color_clusters_by_count(clusters2, COLORS_COUNT); print_color_clusters(clusters2, COLORS_COUNT); center = get_color_center(clusters2[0].id, img_hsv_idx); printf("[i] first color center: %03d %03d\n", center.x, center.y); center = get_color_center(clusters2[1].id, img_hsv_idx); printf("[i] second color center: %03d %03d\n", center.x, center.y); center = get_color_center(clusters2[2].id, img_hsv_idx); printf("[i] third color center: %03d %03d\n", center.x, center.y); #endif // print_color_clusters image_delete(&img_hsv_col); image_delete(&img_hsv_idx); #endif // hsv colorer printf("[i] == Performance == \n"); print_performance("image_convert_color", t1, t0); print_performance("image_thin_borders", t3, t2); print_performance("image_min_max_loc", t5, t4); print_performance("image_threshold", t7, t6); print_performance("image_resize", t9, t8); print_performance("image_kmeans_colorer", t11, t10); print_performance("image_rgb2hsv", t13, t12); print_performance("image_hsv_colorer", t15, t14); image_delete(&img); image_delete(&img2); #if 1 image_delete(&img_gray); image_delete(&img_borders); image_delete(&img_thr); #endif printf("[i] End.\n"); return 0; }
void visual_tracer_close(void) { if (host.tw != NULL) { image_delete(host.tw); image_close(host.tw); } }
int main(int argc, char ** argv){ image_t *match_x = NULL, *match_y = NULL, *match_z = NULL; // load images if(argc < 4){ fprintf(stderr,"Wrong command, require at least 3 arguments.\n\n"); usage(); exit(1); } color_image_t *im1 = color_image_load(argv[1]), *im2 = color_image_load(argv[2]); if(im1->width != im2->width || im1->height != im2->height){ fprintf(stderr,"Image dimensions does not match\n"); exit(1); } // set params to default optical_flow_params_t* params = (optical_flow_params_t*) malloc(sizeof(optical_flow_params_t)); if(!params){ fprintf(stderr,"error deepflow(): not enough memory\n"); exit(1); } optical_flow_params_default(params); // parse options int current_arg = 4; while(1){ if( current_arg >= argc) break; if(!strcmp(argv[current_arg],"-h") || !strcmp(argv[current_arg],"--help") ){ usage(); exit(1); }else if(!strcmp(argv[current_arg],"-a") || !strcmp(argv[current_arg],"-alpha") ){ current_arg++; if(current_arg >= argc) require_argument("alpha"); float alpha = atof(argv[current_arg++]); if(alpha<0){ fprintf(stderr,"Alpha argument cannot be negative\n"); exit(1); } params->alpha = alpha; }else if(!strcmp(argv[current_arg],"-b") || !strcmp(argv[current_arg],"-beta") ){ current_arg++; if(current_arg >= argc) require_argument("beta"); float beta = atof(argv[current_arg++]); if(beta<0){ fprintf(stderr,"Beta argument cannot be negative\n"); exit(1); } params->beta = beta; }else if(!strcmp(argv[current_arg],"-g") || !strcmp(argv[current_arg],"-gamma") ){ current_arg++; if(current_arg >= argc) require_argument("gamma"); float gamma = atof(argv[current_arg++]); if(gamma<0){ fprintf(stderr,"Gamma argument cannot be negative\n"); exit(1); } params->gamma = gamma; }else if(!strcmp(argv[current_arg],"-d") || !strcmp(argv[current_arg],"-delta") ){ current_arg++; if(current_arg >= argc) require_argument("delta"); float delta = atof(argv[current_arg++]); if(delta<0) { fprintf(stderr,"Delta argument cannot be negative\n"); exit(1); } params->delta = delta; }else if(!strcmp(argv[current_arg],"-s") || !strcmp(argv[current_arg],"-sigma") ){ current_arg++; if(current_arg >= argc) require_argument("sigma"); float sigma = atof(argv[current_arg++]); if(sigma<0){ fprintf(stderr,"Sigma argument is negative\n"); exit(1); } params->sigma = sigma; }else if(!strcmp(argv[current_arg],"-bk")) { current_arg++; if(current_arg >= argc) require_argument("bk"); float betak = atof(argv[current_arg++]); if(betak<0.0f){ fprintf(stderr,"Bk argument must be positive\n"); exit(1); } params->bk = betak; }else if(!strcmp(argv[current_arg],"-e") || !strcmp(argv[current_arg],"-eta") ){ current_arg++; if(current_arg >= argc) require_argument("eta"); float eta = atof(argv[current_arg++]); if(eta<0.25 || eta>0.98){ fprintf(stderr,"Eta argument has to be between 0.25 and 0.98\n"); exit(1); } params->eta = eta; }else if( !strcmp(argv[current_arg],"-minsize") ){ current_arg++; if(current_arg >= argc) require_argument("minsize"); int minsize = atoi(argv[current_arg++]); if(minsize < 10){ fprintf(stderr,"Minsize argument has to be higher than 10\n"); exit(1); } params->min_size = minsize; }else if(!strcmp(argv[current_arg],"-inner") ){ current_arg++; if(current_arg >= argc) require_argument("inner"); int inner = atoi(argv[current_arg++]); if(inner<=0){ fprintf(stderr,"Inner argument must be strictly positive\n"); exit(1); } params->n_inner_iteration = inner; }else if(!strcmp(argv[current_arg],"-iter") ){ current_arg++; if(current_arg >= argc) require_argument("iter"); int iter = atoi(argv[current_arg++]); if(iter<=0){ fprintf(stderr,"Iter argument must be strictly positive\n"); exit(1); } params->n_solver_iteration = iter; }else if( !strcmp(argv[current_arg],"-match") || !strcmp(argv[current_arg],"-matchf")){ int wm = im1->width, hm = im1->height; if( !strcmp(argv[current_arg++],"-match") ){ wm = 512; hm = 256; } image_delete(match_x); image_delete(match_y); image_delete(match_z); match_x = image_new(wm, hm); match_y = image_new(wm, hm); match_z = image_new(wm, hm); image_erase(match_x); image_erase(match_y); image_erase(match_z); FILE *fid = stdin; if( current_arg<argc && argv[current_arg][0] != '-'){ fid = fopen(argv[current_arg++], "r"); if(fid==NULL){ fprintf(stderr, "Cannot read matches from file %s", argv[current_arg-1]); exit(1); } } int x1, x2, y1, y2; float score; while(!feof(fid) && fscanf(fid, "%d %d %d %d %f\n", &x1, &y1, &x2, &y2, &score)==5){ if( x1<0 || y1<0 || x2<0 || y2<0 || x1>=wm || y1>=hm || x2>=wm || y2>=hm){ fprintf(stderr, "Error while reading matches %d %d -> %d %d, out of bounds\n", x1, y1, x2, y2); exit(1); } match_x->data[ y1*match_x->stride+x1 ] = (float) (x2-x1); match_y->data[ y1*match_x->stride+x1 ] = (float) (y2-y1); match_z->data[ y1*match_x->stride+x1 ] = score; } }else if ( !strcmp(argv[current_arg],"-sintel") ){ current_arg++; optical_flow_params_sintel(params); }else if ( !strcmp(argv[current_arg],"-middlebury") ){ current_arg++; optical_flow_params_middlebury(params); }else if ( !strcmp(argv[current_arg],"-kitti") ){ current_arg++; optical_flow_params_kitti(params); }else{ if(argv[current_arg][0] == '-'){ fprintf(stderr,"Unknow options %s\n",argv[current_arg]); }else{ fprintf(stderr,"Error while reading options, %s\n",argv[current_arg]); } exit(1); } } image_t *wx = image_new(im1->width,im1->height), *wy = image_new(im1->width,im1->height); optical_flow(wx, wy, im1, im2, params, match_x, match_y, match_z); writeFlowFile(argv[3], wx, wy); image_delete(wx); image_delete(wy); image_delete(match_x); image_delete(match_y); image_delete(match_z); color_image_delete(im1); color_image_delete(im2); free(params); return 0; }
void mexFunction( int nl, mxArray *pl[], int nr, const mxArray *pr[] ) { if( nr==0 ){ usage(MATLAB_OPTIONS); return; } if ( nl != 1){ usage(MATLAB_OPTIONS); mexErrMsgTxt("error: returns one output"); return; } if( nr < 2 || nr > 4){ usage(MATLAB_OPTIONS); mexErrMsgTxt("error: takes two to four inputs"); return; } // The code is originally written for C-order arrays. // We thus transpose all arrays in this mex-function which is not efficient... const int *pDims; if( mxGetNumberOfDimensions(pr[0]) != 3 ) mexErrMsgTxt("input images must have 3 dimensions"); if( !mxIsClass(pr[0], "single") ) mexErrMsgTxt("input images must be single"); pDims = mxGetDimensions(pr[0]); if( pDims[2]!=3 ) mexErrMsgTxt("input images must have 3 channels"); const int h = pDims[0], w = pDims[1]; color_image_t *im1 = input3darray_to_color_image( pr[0] ); if( mxGetNumberOfDimensions(pr[1]) != 3 ) mexErrMsgTxt("input images must have 3 dimensions"); if( !mxIsClass(pr[1], "single") ) mexErrMsgTxt("input images must be single"); pDims = mxGetDimensions(pr[1]); if( pDims[0]!=h || pDims[1]!=w || pDims[2]!=3) mexErrMsgTxt( "input images must have the same size" ); color_image_t *im2 = input3darray_to_color_image( pr[1] ); image_t *match_x = NULL, *match_y = NULL, *match_z = NULL; if( nr>2 && !mxIsEmpty(pr[2]) ){ if( mxGetNumberOfDimensions(pr[2]) != 2 ) mexErrMsgTxt("input matches must be a 2d-matrix"); if( !mxIsClass(pr[2], "single")) mexErrMsgTxt("input matches must be single"); pDims = mxGetDimensions(pr[1]); if( pDims[1]<4) mexErrMsgTxt( "input matches must have at least 4 columns: x1 y1 x2 y2" ); match_x = image_new(w, h); match_y = image_new(w, h); match_z = image_new(w, h); input2darray_to_matches( match_x, match_y, match_z, pr[2]); } // set params to default optical_flow_params_t* params = (optical_flow_params_t*) malloc(sizeof(optical_flow_params_t)); if(!params){ fprintf(stderr,"error deepflow2(): not enough memory\n"); exit(1); } optical_flow_params_default(params); // read options if( nr > 3 ){ char *options = mxArrayToString(pr[3]); if( !options ) mexErrMsgTxt("Fourth parameter must be a string"); int argc=0; char* argv[256]; argv[argc]=strtok(options," "); while(argv[argc]!=NULL) { argv[++argc]=strtok(NULL," "); } parse_options(params, argc, argv, MATLAB_OPTIONS, w, h); } image_t *wx = image_new(im1->width,im1->height), *wy = image_new(im1->width,im1->height); optical_flow(wx, wy, im1, im2, params, match_x, match_y, match_z); int dims[3] = {h,w,2}; pl[0] = mxCreateNumericArray(3, dims, mxSINGLE_CLASS, mxREAL); flow_to_output3darray(wx, wy, pl[0]); image_delete(wx); image_delete(wy); image_delete(match_x); image_delete(match_y); image_delete(match_z); color_image_delete(im1); color_image_delete(im2); free(params); }
/* perform flow computation at one level of the pyramid */ void compute_one_level(image_t *wx, image_t *wy, color_image_t *im1, color_image_t *im2, const variational_params_t *params){ const int width = wx->width, height = wx->height, stride=wx->stride; image_t *du = image_new(width,height), *dv = image_new(width,height), // the flow increment *mask = image_new(width,height), // mask containing 0 if a point goes outside image boundary, 1 otherwise *smooth_horiz = image_new(width,height), *smooth_vert = image_new(width,height), // horiz: (i,j) contains the diffusivity coeff from (i,j) to (i+1,j) *uu = image_new(width,height), *vv = image_new(width,height), // flow plus flow increment *a11 = image_new(width,height), *a12 = image_new(width,height), *a22 = image_new(width,height), // system matrix A of Ax=b for each pixel *b1 = image_new(width,height), *b2 = image_new(width,height); // system matrix b of Ax=b for each pixel color_image_t *w_im2 = color_image_new(width,height), // warped second image *Ix = color_image_new(width,height), *Iy = color_image_new(width,height), *Iz = color_image_new(width,height), // first order derivatives *Ixx = color_image_new(width,height), *Ixy = color_image_new(width,height), *Iyy = color_image_new(width,height), *Ixz = color_image_new(width,height), *Iyz = color_image_new(width,height); // second order derivatives image_t *dpsis_weight = compute_dpsis_weight(im1, 5.0f, deriv); int i_outer_iteration; for(i_outer_iteration = 0 ; i_outer_iteration < params->niter_outer ; i_outer_iteration++){ int i_inner_iteration; // warp second image image_warp(w_im2, mask, im2, wx, wy); // compute derivatives get_derivatives(im1, w_im2, deriv, Ix, Iy, Iz, Ixx, Ixy, Iyy, Ixz, Iyz); // erase du and dv image_erase(du); image_erase(dv); // initialize uu and vv memcpy(uu->data,wx->data,wx->stride*wx->height*sizeof(float)); memcpy(vv->data,wy->data,wy->stride*wy->height*sizeof(float)); // inner fixed point iterations for(i_inner_iteration = 0 ; i_inner_iteration < params->niter_inner ; i_inner_iteration++){ // compute robust function and system compute_smoothness(smooth_horiz, smooth_vert, uu, vv, dpsis_weight, deriv_flow, half_alpha ); compute_data_and_match(a11, a12, a22, b1, b2, mask, du, dv, Ix, Iy, Iz, Ixx, Ixy, Iyy, Ixz, Iyz, half_delta_over3, half_gamma_over3); sub_laplacian(b1, wx, smooth_horiz, smooth_vert); sub_laplacian(b2, wy, smooth_horiz, smooth_vert); // solve system sor_coupled(du, dv, a11, a12, a22, b1, b2, smooth_horiz, smooth_vert, params->niter_solver, params->sor_omega); // update flow plus flow increment int i; v4sf *uup = (v4sf*) uu->data, *vvp = (v4sf*) vv->data, *wxp = (v4sf*) wx->data, *wyp = (v4sf*) wy->data, *dup = (v4sf*) du->data, *dvp = (v4sf*) dv->data; for( i=0 ; i<height*stride/4 ; i++){ (*uup) = (*wxp) + (*dup); (*vvp) = (*wyp) + (*dvp); uup+=1; vvp+=1; wxp+=1; wyp+=1;dup+=1;dvp+=1; } } // add flow increment to current flow memcpy(wx->data,uu->data,uu->stride*uu->height*sizeof(float)); memcpy(wy->data,vv->data,vv->stride*vv->height*sizeof(float)); } // free memory image_delete(du); image_delete(dv); image_delete(mask); image_delete(smooth_horiz); image_delete(smooth_vert); image_delete(uu); image_delete(vv); image_delete(a11); image_delete(a12); image_delete(a22); image_delete(b1); image_delete(b2); image_delete(dpsis_weight); color_image_delete(w_im2); color_image_delete(Ix); color_image_delete(Iy); color_image_delete(Iz); color_image_delete(Ixx); color_image_delete(Ixy); color_image_delete(Iyy); color_image_delete(Ixz); color_image_delete(Iyz); }
/*static*/ void prefilt(image_t *src, int fc) { fftw_lock(); int i, j; /* Log */ for(j = 0; j < src->height; j++) { for(i = 0; i < src->width; i++) { src->data[j*src->stride+i] = log(src->data[j*src->stride+i]+1.0f); } } image_t *img_pad = image_add_padding(src, 5); /* Get sizes */ int width = img_pad->width; int height = img_pad->height; int stride = img_pad->stride; /* Alloc memory */ float *fx = (float *) fftwf_malloc(width*height*sizeof(float)); float *fy = (float *) fftwf_malloc(width*height*sizeof(float)); float *gfc = (float *) fftwf_malloc(width*height*sizeof(float)); fftwf_complex *in1 = (fftwf_complex *) fftwf_malloc(width*height*sizeof(fftwf_complex)); fftwf_complex *in2 = (fftwf_complex *) fftwf_malloc(width*height*sizeof(fftwf_complex)); fftwf_complex *out = (fftwf_complex *) fftwf_malloc(width*height*sizeof(fftwf_complex)); /* Build whitening filter */ float s1 = fc/sqrt(log(2)); for(j = 0; j < height; j++) { for(i = 0; i < width; i++) { in1[j*width + i][0] = img_pad->data[j*stride+i]; in1[j*width + i][1] = 0.0f; fx[j*width + i] = (float) i - width/2.0f; fy[j*width + i] = (float) j - height/2.0f; gfc[j*width + i] = exp(-(fx[j*width + i]*fx[j*width + i] + fy[j*width + i]*fy[j*width + i]) / (s1*s1)); } } fftshift(gfc, width, height); /* FFT */ fftwf_plan fft1 = fftwf_plan_dft_2d(width, height, in1, out, FFTW_FORWARD, FFTW_ESTIMATE); fftw_unlock(); fftwf_execute(fft1); fftw_lock(); /* Apply whitening filter */ for(j = 0; j < height; j++) { for(i = 0; i < width; i++) { out[j*width+i][0] *= gfc[j*width + i]; out[j*width+i][1] *= gfc[j*width + i]; } } /* IFFT */ fftwf_plan ifft1 = fftwf_plan_dft_2d(width, height, out, in2, FFTW_BACKWARD, FFTW_ESTIMATE); fftw_unlock(); fftwf_execute(ifft1); fftw_lock(); /* Local contrast normalisation */ for(j = 0; j < height; j++) { for(i = 0; i < width; i++) { img_pad->data[j*stride + i] -= in2[j*width+i][0] / (width*height); in1[j*width + i][0] = img_pad->data[j*stride + i] * img_pad->data[j*stride + i]; in1[j*width + i][1] = 0.0f; } } /* FFT */ fftwf_plan fft2 = fftwf_plan_dft_2d(width, height, in1, out, FFTW_FORWARD, FFTW_ESTIMATE); fftw_unlock(); fftwf_execute(fft2); fftw_lock(); /* Apply contrast normalisation filter */ for(j = 0; j < height; j++) { for(i = 0; i < width; i++) { out[j*width+i][0] *= gfc[j*width + i]; out[j*width+i][1] *= gfc[j*width + i]; } } /* IFFT */ fftwf_plan ifft2 = fftwf_plan_dft_2d(width, height, out, in2, FFTW_BACKWARD, FFTW_ESTIMATE); fftw_unlock(); fftwf_execute(ifft2); fftw_lock(); /* Get result from contrast normalisation filter */ for(j = 0; j < height; j++) { for(i = 0; i < width; i++) { img_pad->data[j*stride+i] = img_pad->data[j*stride + i] / (0.2f+sqrt(sqrt(in2[j*width+i][0]*in2[j*width+i][0]+in2[j*width+i][1]*in2[j*width+i][1]) / (width*height))); } } image_rem_padding(src, img_pad, 5); /* Free */ fftwf_destroy_plan(fft1); fftwf_destroy_plan(fft2); fftwf_destroy_plan(ifft1); fftwf_destroy_plan(ifft2); image_delete(img_pad); fftwf_free(in1); fftwf_free(in2); fftwf_free(out); fftwf_free(fx); fftwf_free(fy); fftwf_free(gfc); fftw_unlock(); }