From 5a49c1d962895b4b2835ae47201d6b07e669153b Mon Sep 17 00:00:00 2001 From: Joseph Redmon Date: Thu, 9 Apr 2015 23:00:33 -0700 Subject: [PATCH] rotation --- src/data.c | 8 ++++++-- src/image.c | 29 ++++++++++++++++++++++------- src/image.h | 3 ++- src/imagenet.c | 7 +++++++ 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/data.c b/src/data.c index c454d84c..34108f6d 100644 --- a/src/data.c +++ b/src/data.c @@ -57,6 +57,12 @@ matrix load_image_paths(char **paths, int n, int w, int h) for(i = 0; i < n; ++i){ image im = load_image_color(paths[i], w, h); + translate_image(im, -128); + scale_image(im, 1./128); + float rad = rand_uniform() - .5; + image rot = rotate_image(im, rad); + free_image(im); + im = rot; X.vals[i] = im.data; X.cols = im.h*im.w*im.c; } @@ -373,8 +379,6 @@ void *load_in_thread(void *ptr) { struct load_args a = *(struct load_args*)ptr; *a.d = load_data(a.paths, a.n, a.m, a.labels, a.k, a.w, a.h); - translate_data_rows(*a.d, -128); - scale_data_rows(*a.d, 1./128); free(ptr); return 0; } diff --git a/src/image.c b/src/image.c index 32a51cc5..d85a054d 100644 --- a/src/image.c +++ b/src/image.c @@ -1,6 +1,7 @@ #include "image.h" #include "utils.h" #include +#include int windows = 0; @@ -256,16 +257,23 @@ image float_to_image(int w, int h, int c, float *data) return out; } -void rotate_image(image m) +image rotate_image(image im, float rad) { - int i,j; - for(j = 0; j < m.c; ++j){ - for(i = 0; i < m.h*m.w/2; ++i){ - float swap = m.data[j*m.h*m.w + i]; - m.data[j*m.h*m.w + i] = m.data[j*m.h*m.w + (m.h*m.w-1 - i)]; - m.data[j*m.h*m.w + (m.h*m.w-1 - i)] = swap; + int x, y, c; + float cx = im.w/2.; + float cy = im.h/2.; + image rot = make_image(im.w, im.h, im.c); + for(c = 0; c < im.c; ++c){ + for(y = 0; y < im.h; ++y){ + for(x = 0; x < im.w; ++x){ + float rx = cos(rad)*(x-cx) - sin(rad)*(y-cy) + cx; + float ry = sin(rad)*(x-cx) + cos(rad)*(y-cy) + cy; + float val = billinear_interpolate(im, rx, ry, c); + set_pixel(rot, x, y, c, val); + } } } + return rot; } void translate_image(image m, float s) @@ -358,15 +366,22 @@ image resize_image(image im, int w, int h) void test_resize(char *filename) { image im = load_image(filename, 0,0); + translate_image(im, -128); image small = resize_image(im, 65, 63); image big = resize_image(im, 513, 512); image crop = crop_image(im, 50, 10, 100, 100); image crop2 = crop_image(im, -30, -50, 291, 400); + image rot = rotate_image(big, .02); + image rot2 = rotate_image(big, 3.14159265/2.); + image test = rotate_image(im, .6); show_image(im, "original"); show_image(small, "smaller"); show_image(big, "bigger"); show_image(crop, "crop"); show_image(crop2, "crop2"); + show_image(rot, "rot"); + show_image(rot2, "rot2"); + show_image(test, "test"); cvWaitKey(0); } diff --git a/src/image.h b/src/image.h index a0d1875e..66262d81 100644 --- a/src/image.h +++ b/src/image.h @@ -20,7 +20,7 @@ image crop_image(image im, int dx, int dy, int w, int h); image resize_image(image im, int w, int h); void translate_image(image m, float s); void normalize_image(image p); -void rotate_image(image m); +image rotate_image(image m, float rad); void embed_image(image source, image dest, int dx, int dy); image collapse_image_layers(image source, int border); @@ -47,6 +47,7 @@ image ipl_to_image(IplImage* src); float get_pixel(image m, int x, int y, int c); float get_pixel_extend(image m, int x, int y, int c); void set_pixel(image m, int x, int y, int c, float val); +float billinear_interpolate(image im, float x, float y, int c); image get_image_layer(image m, int l); diff --git a/src/imagenet.c b/src/imagenet.c index 8a4ebcb3..9e993302 100644 --- a/src/imagenet.c +++ b/src/imagenet.c @@ -29,6 +29,13 @@ void train_imagenet(char *cfgfile, char *weightfile) time=clock(); pthread_join(load_thread, 0); train = buffer; + +/* + image im = float_to_image(256, 256, 3, train.X.vals[114]); + show_image(im, "training"); + cvWaitKey(0); + */ + load_thread = load_data_thread(paths, imgs, plist->size, labels, 1000, 256, 256, &buffer); printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock();