Joseph Redmon
2016-08-11 aebe937710ced03d03f73ab23f410f29685655c1
src/image.c
@@ -109,14 +109,17 @@
        int class = max_index(probs[i], classes);
        float prob = probs[i][class];
        if(prob > thresh){
            int width = pow(prob, 1./2.)*10+1;
            width = 8;
            //int width = pow(prob, 1./2.)*30+1;
            int width = 8;
            printf("%s: %.0f%%\n", names[class], prob*100);
            int offset = class*1 % classes;
            float red = get_color(2,offset,classes);
            float green = get_color(1,offset,classes);
            float blue = get_color(0,offset,classes);
            float rgb[3];
            //width = prob*20+2;
            rgb[0] = red;
            rgb[1] = green;
            rgb[2] = blue;
@@ -344,23 +347,6 @@
#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)
    {
@@ -373,7 +359,7 @@
#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);
@@ -397,6 +383,28 @@
    }
#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;
@@ -451,6 +459,25 @@
        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;
@@ -536,7 +563,7 @@
    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);
@@ -548,7 +575,7 @@
    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;
@@ -559,7 +586,7 @@
        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];
@@ -587,15 +614,27 @@
    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;
}
@@ -778,23 +817,6 @@
    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);
@@ -877,6 +899,7 @@
    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);
@@ -895,7 +918,16 @@
    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");
@@ -903,6 +935,7 @@
    show_image(sat5, "Saturation-.5");
    show_image(exp2, "Exposure-2");
    show_image(exp5, "Exposure-.5");
    */
#ifdef OPENCV
    cvWaitKey(0);
#endif
@@ -1020,7 +1053,11 @@
}
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)