| | |
| | | #endif |
| | | } |
| | | |
| | | void save_image(image im, const char *name) |
| | | { |
| | | char buff[256]; |
| | | //sprintf(buff, "%s (%d)", name, windows); |
| | | sprintf(buff, "%s.png", name); |
| | | unsigned char *data = calloc(im.w*im.h*im.c, sizeof(char)); |
| | | int i,k; |
| | | for(k = 0; k < im.c; ++k){ |
| | | for(i = 0; i < im.w*im.h; ++i){ |
| | | data[i*im.c+k] = (unsigned char) (255*im.data[i + k*im.w*im.h]); |
| | | } |
| | | } |
| | | int success = stbi_write_png(buff, im.w, im.h, im.c, data, im.w*im.c); |
| | | free(data); |
| | | if(!success) fprintf(stderr, "Failed to write image %s\n", buff); |
| | | } |
| | | |
| | | #ifdef OPENCV |
| | | image get_image_from_stream(CvCapture *cap) |
| | | { |
| | |
| | | #endif |
| | | |
| | | #ifdef OPENCV |
| | | void save_image_jpg(image p, char *name) |
| | | void save_image_jpg(image p, const char *name) |
| | | { |
| | | image copy = copy_image(p); |
| | | rgbgr_image(copy); |
| | |
| | | } |
| | | #endif |
| | | |
| | | void save_image(image im, const char *name) |
| | | { |
| | | #ifdef OPENCV |
| | | save_image_jpg(im, name); |
| | | #else |
| | | char buff[256]; |
| | | //sprintf(buff, "%s (%d)", name, windows); |
| | | sprintf(buff, "%s.png", name); |
| | | unsigned char *data = calloc(im.w*im.h*im.c, sizeof(char)); |
| | | int i,k; |
| | | for(k = 0; k < im.c; ++k){ |
| | | for(i = 0; i < im.w*im.h; ++i){ |
| | | data[i*im.c+k] = (unsigned char) (255*im.data[i + k*im.w*im.h]); |
| | | } |
| | | } |
| | | int success = stbi_write_png(buff, im.w, im.h, im.c, data, im.w*im.c); |
| | | free(data); |
| | | if(!success) fprintf(stderr, "Failed to write image %s\n", buff); |
| | | #endif |
| | | } |
| | | |
| | | |
| | | void show_image_layers(image p, char *name) |
| | | { |
| | | int i; |
| | |
| | | return out; |
| | | } |
| | | |
| | | image rotate_crop_image(image im, float rad, float s, int w, int h, int dx, int dy) |
| | | { |
| | | int x, y, c; |
| | | float cx = im.w/2.; |
| | | float cy = im.h/2.; |
| | | image rot = make_image(w, h, im.c); |
| | | for(c = 0; c < im.c; ++c){ |
| | | for(y = 0; y < h; ++y){ |
| | | for(x = 0; x < w; ++x){ |
| | | float rx = cos(rad)*(x/s + dx/s -cx) - sin(rad)*(y/s + dy/s -cy) + cx; |
| | | float ry = sin(rad)*(x/s + dx/s -cx) + cos(rad)*(y/s + dy/s -cy) + cy; |
| | | float val = bilinear_interpolate(im, rx, ry, c); |
| | | set_pixel(rot, x, y, c, val); |
| | | } |
| | | } |
| | | } |
| | | return rot; |
| | | } |
| | | |
| | | image rotate_image(image im, float rad) |
| | | { |
| | | int x, y, c; |
| | |
| | | return best; |
| | | } |
| | | |
| | | void composite_3d(char *f1, char *f2, char *out) |
| | | void composite_3d(char *f1, char *f2, char *out, int delta) |
| | | { |
| | | if(!out) out = "out"; |
| | | image a = load_image(f1, 0,0,0); |
| | |
| | | image c2 = crop_image(b, -10, shift, b.w, b.h); |
| | | float d2 = dist_array(c2.data, a.data, a.w*a.h*a.c, 100); |
| | | |
| | | if(d2 < d1){ |
| | | if(d2 < d1 && 0){ |
| | | image swap = a; |
| | | a = b; |
| | | b = swap; |
| | |
| | | printf("%d\n", shift); |
| | | } |
| | | |
| | | image c = crop_image(b, 0, shift, a.w, a.h); |
| | | image c = crop_image(b, delta, shift, a.w, a.h); |
| | | int i; |
| | | for(i = 0; i < c.w*c.h; ++i){ |
| | | c.data[i] = a.data[i]; |
| | |
| | | return resized; |
| | | } |
| | | |
| | | image random_crop_image(image im, int low, int high, int size) |
| | | image random_crop_image(image im, int w, int h) |
| | | { |
| | | int dx = rand_int(0, im.w - w); |
| | | int dy = rand_int(0, im.h - h); |
| | | image crop = crop_image(im, dx, dy, w, h); |
| | | return crop; |
| | | } |
| | | |
| | | image random_augment_image(image im, float angle, int low, int high, int size) |
| | | { |
| | | int r = rand_int(low, high); |
| | | image resized = resize_min(im, r); |
| | | int dx = rand_int(0, resized.w - size); |
| | | int dy = rand_int(0, resized.h - size); |
| | | image crop = crop_image(resized, dx, dy, size, size); |
| | | int min = (im.h < im.w) ? im.h : im.w; |
| | | float scale = (float)r / min; |
| | | |
| | | if(resized.data != im.data) free_image(resized); |
| | | float rad = rand_uniform(-angle, angle) * TWO_PI / 360.; |
| | | int dx = rand_int(0, scale * im.w - size); |
| | | int dy = rand_int(0, scale * im.h - size); |
| | | //printf("%d %d\n", dx, dy); |
| | | |
| | | image crop = rotate_crop_image(im, rad, scale, size, size, dx, dy); |
| | | |
| | | return crop; |
| | | } |
| | | |
| | |
| | | constrain_image(im); |
| | | } |
| | | |
| | | /* |
| | | image saturate_image(image im, float sat) |
| | | { |
| | | image gray = grayscale_image(im); |
| | | image blend = blend_image(im, gray, sat); |
| | | free_image(gray); |
| | | constrain_image(blend); |
| | | return blend; |
| | | } |
| | | |
| | | image brightness_image(image im, float b) |
| | | { |
| | | image bright = make_image(im.w, im.h, im.c); |
| | | return bright; |
| | | } |
| | | */ |
| | | |
| | | float bilinear_interpolate(image im, float x, float y, int c) |
| | | { |
| | | int ix = (int) floorf(x); |
| | |
| | | |
| | | image bin = binarize_image(im); |
| | | |
| | | /* |
| | | #ifdef GPU |
| | | image r = resize_image(im, im.w, im.h); |
| | | image black = make_image(im.w*2 + 3, im.h*2 + 3, 9); |
| | |
| | | show_image_layers(black, "Black"); |
| | | show_image(black2, "Recreate"); |
| | | #endif |
| | | */ |
| | | image rot = rotate_crop_image(im, -.2618, 1, im.w/2, im.h/2, 0, 0); |
| | | image rot3 = rotate_crop_image(im, -.2618, 2, im.w, im.h, im.w/2, 0); |
| | | image rot2 = rotate_crop_image(im, -.2618, 1, im.w, im.h, 0, 0); |
| | | show_image(rot, "Rotated"); |
| | | show_image(rot2, "base"); |
| | | |
| | | show_image(rot3, "Rotated2"); |
| | | |
| | | /* |
| | | show_image(im, "Original"); |
| | | show_image(bin, "Binary"); |
| | | show_image(gray, "Gray"); |
| | |
| | | show_image(sat5, "Saturation-.5"); |
| | | show_image(exp2, "Exposure-2"); |
| | | show_image(exp5, "Exposure-.5"); |
| | | */ |
| | | #ifdef OPENCV |
| | | cvWaitKey(0); |
| | | #endif |
| | |
| | | } |
| | | float get_pixel_extend(image m, int x, int y, int c) |
| | | { |
| | | if(x < 0 || x >= m.w || y < 0 || y >= m.h || c < 0 || c >= m.c) return 0; |
| | | if(x < 0) x = 0; |
| | | if(x >= m.w) x = m.w-1; |
| | | if(y < 0) y = 0; |
| | | if(y >= m.h) y = m.h-1; |
| | | if(c < 0 || c >= m.c) return 0; |
| | | return get_pixel(m, x, y, c); |
| | | } |
| | | void set_pixel(image m, int x, int y, int c, float val) |