yolov3输出检测图片位置信息
阅读原文时间:2023年07月10日阅读:2

前言

我们在进行图片识别后需要进行进一步的处理,该文章会介绍:1.怎样取消lables;2.输出并保存(.txt)标记框的位置信息

在darknet/src/image.c 收索draw_detections_v3 .在该函数对应目录下进行修改。

以图片左上角为(0,0),以右下角为(1,1),这些数字均为位置或长度所在图片的比例大小。

下面的代码是实现提取上述数据并且保存到本地文件夹下:

#include "image.h"
#include "utils.h"
#include "blas.h"
#include "cuda.h"
#include
#include

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#ifdef OPENCV
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/types_c.h"
#include "opencv2/core/version.hpp"
#ifndef CV_VERSION_EPOCH
#include "opencv2/videoio/videoio_c.h"
#include "opencv2/imgcodecs/imgcodecs_c.h"
#include "http_stream.h"
#endif
#include "http_stream.h"
#endif

int windows = ;

float colors[][] = { {,,}, {,,},{,,},{,,},{,,},{,,} };

float get_color(int c, int x, int max)
{
float ratio = ((float)x/max)*;
int i = floor(ratio);
int j = ceil(ratio);
ratio -= i;
float r = (-ratio) * colors[i][c] + ratio*colors[j][c];
//printf("%f\n", r);
return r;
}

static float get_pixel(image m, int x, int y, int c)
{
assert(x < m.w && y < m.h && c < m.c); return m.data[c*m.h*m.w + y*m.w + x]; } static float get_pixel_extend(image m, int x, int y, int c) { if (x < || x >= m.w || y < || y >= m.h) return ;
/*
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 < || c >= m.c) return ;
return get_pixel(m, x, y, c);
}
static void set_pixel(image m, int x, int y, int c, float val)
{
if (x < || y < || c < || x >= m.w || y >= m.h || c >= m.c) return;
assert(x < m.w && y < m.h && c < m.c);
m.data[c*m.h*m.w + y*m.w + x] = val;
}
static void add_pixel(image m, int x, int y, int c, float val)
{
assert(x < m.w && y < m.h && c < m.c);
m.data[c*m.h*m.w + y*m.w + x] += val;
}

void composite_image(image source, image dest, int dx, int dy)
{
int x,y,k;
for(k = ; k < source.c; ++k){
for(y = ; y < source.h; ++y){
for(x = ; x < source.w; ++x){
float val = get_pixel(source, x, y, k);
float val2 = get_pixel_extend(dest, dx+x, dy+y, k);
set_pixel(dest, dx+x, dy+y, k, val * val2);
}
}
}
}

image border_image(image a, int border)
{
image b = make_image(a.w + *border, a.h + *border, a.c);
int x,y,k;
for(k = ; k < b.c; ++k){ for(y = ; y < b.h; ++y){ for(x = ; x < b.w; ++x){ float val = get_pixel_extend(a, x - border, y - border, k); if(x - border < || x - border >= a.w || y - border < || y - border >= a.h) val = ;
set_pixel(b, x, y, k, val);
}
}
}
return b;
}

image tile_images(image a, image b, int dx)
{
if(a.w == ) return copy_image(b);
image c = make_image(a.w + b.w + dx, (a.h > b.h) ? a.h : b.h, (a.c > b.c) ? a.c : b.c);
fill_cpu(c.w*c.h*c.c, , c.data, );
embed_image(a, c, , );
composite_image(b, c, a.w + dx, );
return c;
}

image get_label(image **characters, char *string, int size)
{
if(size > ) size = ;
image label = make_empty_image(,,);
while(*string){
image l = characters[size][(int)*string];
image n = tile_images(label, l, -size - + (size+)/);
free_image(label);
label = n;
++string;
}
image b = border_image(label, label.h*.);
free_image(label);
return b;
}

image get_label_v3(image **characters, char *string, int size)
{
size = size / ;
if (size > ) size = ;
image label = make_empty_image(, , );
while (*string) {
image l = characters[size][(int)*string];
image n = tile_images(label, l, -size - + (size + ) / );
free_image(label);
label = n;
++string;
}
image b = border_image(label, label.h*.);
free_image(label);
return b;
}

void draw_label(image a, int r, int c, image label, const float *rgb)
{
int w = label.w;
int h = label.h;
if (r - h >= ) r = r - h;

int i, j, k;  
for(j = ; j < h && j + r < a.h; ++j){  
    for(i = ; i < w && i + c < a.w; ++i){  
        for(k = ; k < label.c; ++k){  
            float val = get\_pixel(label, i, j, k);  
            set\_pixel(a, i+c, j+r, k, rgb\[k\] \* val);  
        }  
    }  
}  

}

void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b)
{
//normalize_image(a);
int i;
if(x1 < ) x1 = ; if(x1 >= a.w) x1 = a.w-;
if(x2 < ) x2 = ; if(x2 >= a.w) x2 = a.w-;

if(y1 < ) y1 = ;  
if(y1 >= a.h) y1 = a.h-;  
if(y2 < ) y2 = ;  
if(y2 >= a.h) y2 = a.h-;

for(i = x1; i <= x2; ++i){  
    a.data\[i + y1\*a.w + \*a.w\*a.h\] = r;  
    a.data\[i + y2\*a.w + \*a.w\*a.h\] = r;

    a.data\[i + y1\*a.w + \*a.w\*a.h\] = g;  
    a.data\[i + y2\*a.w + \*a.w\*a.h\] = g;

    a.data\[i + y1\*a.w + \*a.w\*a.h\] = b;  
    a.data\[i + y2\*a.w + \*a.w\*a.h\] = b;  
}  
for(i = y1; i <= y2; ++i){  
    a.data\[x1 + i\*a.w + \*a.w\*a.h\] = r;  
    a.data\[x2 + i\*a.w + \*a.w\*a.h\] = r;

    a.data\[x1 + i\*a.w + \*a.w\*a.h\] = g;  
    a.data\[x2 + i\*a.w + \*a.w\*a.h\] = g;

    a.data\[x1 + i\*a.w + \*a.w\*a.h\] = b;  
    a.data\[x2 + i\*a.w + \*a.w\*a.h\] = b;  
}  

}

void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b)
{
int i;
for(i = ; i < w; ++i){
draw_box(a, x1+i, y1+i, x2-i, y2-i, r, g, b);
}
}

void draw_bbox(image a, box bbox, int w, float r, float g, float b)
{
int left = (bbox.x-bbox.w/)*a.w;
int right = (bbox.x+bbox.w/)*a.w;
int top = (bbox.y-bbox.h/)*a.h;
int bot = (bbox.y+bbox.h/)*a.h;

int i;  
for(i = ; i < w; ++i){  
    draw\_box(a, left+i, top+i, right-i, bot-i, r, g, b);  
}  

}

image **load_alphabet()
{
int i, j;
const int nsize = ;
image **alphabets = calloc(nsize, sizeof(image));
for(j = ; j < nsize; ++j){
alphabets[j] = calloc(, sizeof(image));
for(i = ; i < ; ++i){
char buff[];
sprintf(buff, "data/labels/%d_%d.png", i, j);
alphabets[j][i] = load_image_color(buff, , );
}
}
return alphabets;
}

void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
{
int i, j;
FILE * fp;
fp = fopen("BoxValue.txt", "w");
if(NULL == fp)
{
printf("error");
return;
}

for (i = ; i < num; ++i) {  
    char labelstr\[\] = {  };  
    int class\_id = -;  
    for (j = ; j < classes; ++j) {  
        if (dets\[i\].prob\[j\] > thresh) {  
            if (class\_id < ) {  
                strcat(labelstr, names\[j\]);  
                class\_id = j;  
            }  
            else {  
                strcat(labelstr, ", ");  
                strcat(labelstr, names\[j\]);  
            }  
            printf("%s: %.0f%%\\n", names\[j\], dets\[i\].prob\[j\] \* );  
        }  
    }  
    if (class\_id >= ) {  
        int width = im.h \* .;  
        if (width < )  
            width = ;

        /\*  
        if(0){  
        width = pow(prob, 1./2.)\*10+1;  
        alphabet = 0;  
        }  
        \*/

        //printf("%d %s: %.0f%%\\n", i, names\[class\_id\], prob\*100);  
        int offset = class\_id \*  % classes;  
        float red = get\_color(, offset, classes);  
        float green = get\_color(, offset, classes);  
        float blue = get\_color(, offset, classes);  
        float rgb\[\];

        //width = prob\*20+2;

        rgb\[\] = red;  
        rgb\[\] = green;  
        rgb\[\] = blue;  
        box b = dets\[i\].bbox;  
        //printf("%f %f %f %f\\n", b.x, b.y, b.w, b.h);

        int left = (b.x - b.w / .)\*im.w;  
        int right = (b.x + b.w / .)\*im.w;  
        int top = (b.y - b.h / .)\*im.h;  
        int bot = (b.y + b.h / .)\*im.h;

        if (left < ) left = ;  
        if (right > im.w - ) right = im.w - ;  
        if (top < ) top = ;  
        if (bot > im.h - ) bot = im.h - ;

        float b\_x\_center = (left + right) / ;  
        float b\_y\_center = (top + bot) / ;  
        float b\_width = right - left;  
        float b\_height = bot - top;  
        //sprintf(labelstr, "%d x %d - w: %d, h: %d", b\_x\_center, b\_y\_center, b\_width, b\_height);

        draw\_box\_width(im, left, top, right, bot, width, red, green, blue);  
        //fprintf(fp,"%.1f %.1f %.1f %.1f %d\\n",left, top, right, bot, width, height, class\_id);  
        fprintf(fp,"%.2f %.2f %.2f %.2f %d\\n",b\_x\_center, b\_y\_center, b\_width, b\_height, class\_id);  
        if (alphabet) {  
            image label = get\_label\_v3(alphabet, labelstr, (im.h\*.));  
            //draw\_label(im, top + width, left, label, rgb);  
            free\_image(label);  
        }  
        if (dets\[i\].mask) {  
            image mask = float\_to\_image(, , , dets\[i\].mask);  
            image resized\_mask = resize\_image(mask, b.w\*im.w, b.h\*im.h);  
            image tmask = threshold\_image(resized\_mask, .);  
            embed\_image(tmask, im, left, top);  
            free\_image(mask);  
            free\_image(resized\_mask);  
            free\_image(tmask);  
        }  
    }  
}  
fclose(fp);  

}

void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
{
int i;

for(i = ; i < num; ++i){  
    int class\_id = max\_index(probs\[i\], classes);  
    float prob = probs\[i\]\[class\_id\];  
    if(prob > thresh){

        //// for comparison with OpenCV version of DNN Darknet Yolo v2  
        //printf("\\n %f, %f, %f, %f, ", boxes\[i\].x, boxes\[i\].y, boxes\[i\].w, boxes\[i\].h);  
        // int k;  
        //for (k = 0; k < classes; ++k) {  
        //    printf("%f, ", probs\[i\]\[k\]);  
        //}  
        //printf("\\n");

        int width = im.h \* .;

        if(){  
            width = pow(prob, ./.)\*+;  
            alphabet = ;  
        }

        int offset = class\_id\* % classes;  
        float red = get\_color(,offset,classes);  
        float green = get\_color(,offset,classes);  
        float blue = get\_color(,offset,classes);  
        float rgb\[\];

        //width = prob\*20+2;

        rgb\[\] = red;  
        rgb\[\] = green;  
        rgb\[\] = blue;  
        box b = boxes\[i\];

        int left  = (b.x-b.w/.)\*im.w;  
        int right = (b.x+b.w/.)\*im.w;  
        int top   = (b.y-b.h/.)\*im.h;  
        int bot   = (b.y+b.h/.)\*im.h;

        if(left < ) left = ;  
        if(right > im.w-) right = im.w-;  
        if(top < ) top = ;  
        if(bot > im.h-) bot = im.h-;  
        printf("%s: %.0f%%", names\[class\_id\], prob \* );

        //printf(" - id: %d, x\_center: %d, y\_center: %d, width: %d, height: %d",  
        //    class\_id, (right + left) / 2, (bot - top) / 2, right - left, bot - top);

        printf("\\n");  
        draw\_box\_width(im, left, top, right, bot, width, red, green, blue);  
        if (alphabet) {  
            image label = get\_label(alphabet, names\[class\_id\], (im.h\*.)/);  
            draw\_label(im, top + width, left, label, rgb);  
        }  
    }  
}  

}

#ifdef OPENCV

void draw_detections_cv_v3(IplImage* show_img, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
{
int i, j;
if (!show_img) return;

for (i = ; i < num; ++i) {  
    char labelstr\[\] = {  };  
    int class\_id = -;  
    for (j = ; j < classes; ++j) {  
        if (dets\[i\].prob\[j\] > thresh) {  
            if (class\_id < ) {  
                strcat(labelstr, names\[j\]);  
                class\_id = j;  
            }  
            else {  
                strcat(labelstr, ", ");  
                strcat(labelstr, names\[j\]);  
            }  
            printf("%s: %.0f%%\\n", names\[j\], dets\[i\].prob\[j\] \* );  
        }  
    }  
    if (class\_id >= ) {  
        int width = show\_img->height \* .;

        /\*  
        if(0){  
        width = pow(prob, 1./2.)\*10+1;  
        alphabet = 0;  
        }  
        \*/

        //printf("%d %s: %.0f%%\\n", i, names\[class\_id\], prob\*100);  
        int offset = class\_id \*  % classes;  
        float red = get\_color(, offset, classes);  
        float green = get\_color(, offset, classes);  
        float blue = get\_color(, offset, classes);  
        float rgb\[\];

        //width = prob\*20+2;

        rgb\[\] = red;  
        rgb\[\] = green;  
        rgb\[\] = blue;  
        box b = dets\[i\].bbox;  
        //printf("%f %f %f %f\\n", b.x, b.y, b.w, b.h);

        int left = (b.x - b.w / .)\*show\_img->width;  
        int right = (b.x + b.w / .)\*show\_img->width;  
        int top = (b.y - b.h / .)\*show\_img->height;  
        int bot = (b.y + b.h / .)\*show\_img->height;

        if (left < ) left = ;  
        if (right > show\_img->width - ) right = show\_img->width - ;  
        if (top < ) top = ;  
        if (bot > show\_img->height - ) bot = show\_img->height - ;

        //int b\_x\_center = (left + right) / 2;  
        //int b\_y\_center = (top + bot) / 2;  
        //int b\_width = right - left;  
        //int b\_height = bot - top;  
        //sprintf(labelstr, "%d x %d - w: %d, h: %d", b\_x\_center, b\_y\_center, b\_width, b\_height);

        float const font\_size = show\_img->height / .F;  
        CvPoint pt1, pt2, pt\_text, pt\_text\_bg1, pt\_text\_bg2;  
        pt1.x = left;  
        pt1.y = top;  
        pt2.x = right;  
        pt2.y = bot;  
        pt\_text.x = left;  
        pt\_text.y = top - ;  
        pt\_text\_bg1.x = left;  
        pt\_text\_bg1.y = top - ( +  \* font\_size);  
        pt\_text\_bg2.x = right;  
        pt\_text\_bg2.y = top;  
        CvScalar color;  
        color.val\[\] = red \* ;  
        color.val\[\] = green \* ;  
        color.val\[\] = blue \* ;

        cvRectangle(show\_img, pt1, pt2, color, width, , );  
        //printf("left=%d, right=%d, top=%d, bottom=%d, obj\_id=%d, obj=%s \\n", left, right, top, bot, class\_id, names\[class\_id\]);  
        cvRectangle(show\_img, pt\_text\_bg1, pt\_text\_bg2, color, width, , );  
        cvRectangle(show\_img, pt\_text\_bg1, pt\_text\_bg2, color, CV\_FILLED, , );    // filled  
        CvScalar black\_color;  
        black\_color.val\[\] = ;  
        CvFont font;  
        cvInitFont(&font, CV\_FONT\_HERSHEY\_SIMPLEX, font\_size, font\_size, , font\_size \* , );  
        cvPutText(show\_img, labelstr, pt\_text, &font, black\_color);  
    }  
}  

}

void draw_detections_cv(IplImage* show_img, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
{
int i;

for (i = ; i < num; ++i) {  
    int class\_id = max\_index(probs\[i\], classes);  
    float prob = probs\[i\]\[class\_id\];  
    if (prob > thresh) {

        int width = show\_img->height \* .;

        if () {  
            width = pow(prob, . / .) \*  + ;  
            alphabet = ;  
        }

        printf("%s: %.0f%%\\n", names\[class\_id\], prob \* );  
        int offset = class\_id \*  % classes;  
        float red = get\_color(, offset, classes);  
        float green = get\_color(, offset, classes);  
        float blue = get\_color(, offset, classes);  
        float rgb\[\];

        //width = prob\*20+2;

        rgb\[\] = red;  
        rgb\[\] = green;  
        rgb\[\] = blue;  
        box b = boxes\[i\];

        int left = (b.x - b.w / .)\*show\_img->width;  
        int right = (b.x + b.w / .)\*show\_img->width;  
        int top = (b.y - b.h / .)\*show\_img->height;  
        int bot = (b.y + b.h / .)\*show\_img->height;

        if (left < ) left = ;  
        if (right > show\_img->width - ) right = show\_img->width - ;  
        if (top < ) top = ;  
        if (bot > show\_img->height - ) bot = show\_img->height - ;

        float const font\_size = show\_img->height / .F;  
        CvPoint pt1, pt2, pt\_text, pt\_text\_bg1, pt\_text\_bg2;  
        pt1.x = left;  
        pt1.y = top;  
        pt2.x = right;  
        pt2.y = bot;  
        pt\_text.x = left;  
        pt\_text.y = top - ;  
        pt\_text\_bg1.x = left;  
        pt\_text\_bg1.y = top - (+\*font\_size);  
        pt\_text\_bg2.x = right;  
        pt\_text\_bg2.y = top;  
        CvScalar color;  
        color.val\[\] = red \* ;  
        color.val\[\] = green \* ;  
        color.val\[\] = blue \* ;

        cvRectangle(show\_img, pt1, pt2, color, width, , );  
        //printf("left=%d, right=%d, top=%d, bottom=%d, obj\_id=%d, obj=%s \\n", left, right, top, bot, class\_id, names\[class\_id\]);  
        cvRectangle(show\_img, pt\_text\_bg1, pt\_text\_bg2, color, width, , );  
        cvRectangle(show\_img, pt\_text\_bg1, pt\_text\_bg2, color, CV\_FILLED, , );    // filled  
        CvScalar black\_color;  
        black\_color.val\[\] = ;  
        CvFont font;  
        cvInitFont(&font, CV\_FONT\_HERSHEY\_SIMPLEX, font\_size, font\_size, , font\_size \* , );  
        cvPutText(show\_img, names\[class\_id\], pt\_text, &font, black\_color);  
    }  
}  

}

IplImage* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size)
{
int img_offset = ;
int draw_size = img_size - img_offset;
IplImage* img = cvCreateImage(cvSize(img_size, img_size), , );
cvSet(img, CV_RGB(, , ), );
CvPoint pt1, pt2, pt_text;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, , , CV_AA);
char char_buff[];
int i;
// vertical lines
pt1.x = img_offset; pt2.x = img_size, pt_text.x = ;
for (i = ; i <= number_of_lines; ++i) {
pt1.y = pt2.y = (float)i * draw_size / number_of_lines;
cvLine(img, pt1, pt2, CV_RGB(, , ), , , );
if (i % == ) {
sprintf(char_buff, "%2.1f", max_img_loss*(number_of_lines - i) / number_of_lines);
pt_text.y = pt1.y + ;
cvPutText(img, char_buff, pt_text, &font, CV_RGB(, , ));
cvLine(img, pt1, pt2, CV_RGB(, , ), , , );
}
}
// horizontal lines
pt1.y = draw_size; pt2.y = , pt_text.y = draw_size + ;
for (i = ; i <= number_of_lines; ++i) {
pt1.x = pt2.x = img_offset + (float)i * draw_size / number_of_lines;
cvLine(img, pt1, pt2, CV_RGB(, , ), , , );
if (i % == ) {
sprintf(char_buff, "%d", max_batches * i / number_of_lines);
pt_text.x = pt1.x - ;
cvPutText(img, char_buff, pt_text, &font, CV_RGB(, , ));
cvLine(img, pt1, pt2, CV_RGB(, , ), , , );
}
}
cvPutText(img, "Iteration number", cvPoint(draw_size / , img_size - ), &font, CV_RGB(, , ));
cvPutText(img, "Press 's' to save: chart.jpg", cvPoint(, img_size - ), &font, CV_RGB(, , ));
printf(" If error occurs - run training with flag: -dont_show \n");
cvNamedWindow("average loss", CV_WINDOW_NORMAL);
cvMoveWindow("average loss", , );
cvResizeWindow("average loss", img_size, img_size);
cvShowImage("average loss", img);
cvWaitKey();
return img;
}

void draw_train_loss(IplImage* img, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches)
{
int img_offset = ;
int draw_size = img_size - img_offset;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, , , CV_AA);
char char_buff[];
CvPoint pt1, pt2;
pt1.x = img_offset + draw_size * (float)current_batch / max_batches;
pt1.y = draw_size * ( - avg_loss / max_img_loss);
if (pt1.y < ) pt1.y = ;
cvCircle(img, pt1, , CV_RGB(, , ), CV_FILLED, , );

sprintf(char\_buff, "current avg loss = %2.4f", avg\_loss);  
pt1.x = img\_size / , pt1.y = ;  
pt2.x = pt1.x + , pt2.y = pt1.y + ;  
cvRectangle(img, pt1, pt2, CV\_RGB(, , ), CV\_FILLED, , );  
pt1.y += ;  
cvPutText(img, char\_buff, pt1, &font, CV\_RGB(, , ));  
cvShowImage("average loss", img);  
int k = cvWaitKey();  
if (k == 's' || current\_batch == (max\_batches-)) cvSaveImage("chart.jpg", img, );  

}
#endif // OPENCV

void transpose_image(image im)
{
assert(im.w == im.h);
int n, m;
int c;
for(c = ; c < im.c; ++c){
for(n = ; n < im.w-; ++n){
for(m = n + ; m < im.w; ++m){
float swap = im.data[m + im.w*(n + im.h*c)];
im.data[m + im.w*(n + im.h*c)] = im.data[n + im.w*(m + im.h*c)];
im.data[n + im.w*(m + im.h*c)] = swap;
}
}
}
}

void rotate_image_cw(image im, int times)
{
assert(im.w == im.h);
times = (times + ) % ;
int i, x, y, c;
int n = im.w;
for(i = ; i < times; ++i){
for(c = ; c < im.c; ++c){
for(x = ; x < n/; ++x){
for(y = ; y < (n-)/ + ; ++y){
float temp = im.data[y + im.w*(x + im.h*c)];
im.data[y + im.w*(x + im.h*c)] = im.data[n--x + im.w*(y + im.h*c)];
im.data[n--x + im.w*(y + im.h*c)] = im.data[n--y + im.w*(n--x + im.h*c)];
im.data[n--y + im.w*(n--x + im.h*c)] = im.data[x + im.w*(n--y + im.h*c)];
im.data[x + im.w*(n--y + im.h*c)] = temp;
}
}
}
}
}

void flip_image(image a)
{
int i,j,k;
for(k = ; k < a.c; ++k){
for(i = ; i < a.h; ++i){
for(j = ; j < a.w/; ++j){
int index = j + a.w*(i + a.h*(k));
int flip = (a.w - j - ) + a.w*(i + a.h*(k));
float swap = a.data[flip];
a.data[flip] = a.data[index];
a.data[index] = swap;
}
}
}
}

image image_distance(image a, image b)
{
int i,j;
image dist = make_image(a.w, a.h, );
for(i = ; i < a.c; ++i){
for(j = ; j < a.h*a.w; ++j){
dist.data[j] += pow(a.data[i*a.h*a.w+j]-b.data[i*a.h*a.w+j],);
}
}
for(j = ; j < a.h*a.w; ++j){
dist.data[j] = sqrt(dist.data[j]);
}
return dist;
}

void embed_image(image source, image dest, int dx, int dy)
{
int x,y,k;
for(k = ; k < source.c; ++k){
for(y = ; y < source.h; ++y){
for(x = ; x < source.w; ++x){
float val = get_pixel(source, x,y,k);
set_pixel(dest, dx+x, dy+y, k, val);
}
}
}
}

image collapse_image_layers(image source, int border)
{
int h = source.h;
h = (h+border)*source.c - border;
image dest = make_image(source.w, h, );
int i;
for(i = ; i < source.c; ++i){
image layer = get_image_layer(source, i);
int h_offset = i*(source.h+border);
embed_image(layer, dest, , h_offset);
free_image(layer);
}
return dest;
}

void constrain_image(image im)
{
int i;
for(i = ; i < im.w*im.h*im.c; ++i){ if(im.data[i] < ) im.data[i] = ; if(im.data[i] > ) im.data[i] = ;
}
}

void normalize_image(image p)
{
int i;
float min = ;
float max = -;

for(i = ; i < p.h\*p.w\*p.c; ++i){  
    float v = p.data\[i\];  
    if(v < min) min = v;  
    if(v > max) max = v;  
}  
if(max - min < .){  
    min = ;  
    max = ;  
}  
for(i = ; i < p.c\*p.w\*p.h; ++i){  
    p.data\[i\] = (p.data\[i\] - min)/(max-min);  
}  

}

void normalize_image2(image p)
{
float *min = calloc(p.c, sizeof(float));
float *max = calloc(p.c, sizeof(float));
int i,j;
for(i = ; i < p.c; ++i) min[i] = max[i] = p.data[i*p.h*p.w];

for(j = ; j < p.c; ++j){  
    for(i = ; i < p.h\*p.w; ++i){  
        float v = p.data\[i+j\*p.h\*p.w\];  
        if(v < min\[j\]) min\[j\] = v;  
        if(v > max\[j\]) max\[j\] = v;  
    }  
}  
for(i = ; i < p.c; ++i){  
    if(max\[i\] - min\[i\] < .){  
        min\[i\] = ;  
        max\[i\] = ;  
    }  
}  
for(j = ; j < p.c; ++j){  
    for(i = ; i < p.w\*p.h; ++i){  
        p.data\[i+j\*p.h\*p.w\] = (p.data\[i+j\*p.h\*p.w\] - min\[j\])/(max\[j\]-min\[j\]);  
    }  
}  
free(min);  
free(max);  

}

image copy_image(image p)
{
image copy = p;
copy.data = calloc(p.h*p.w*p.c, sizeof(float));
memcpy(copy.data, p.data, p.h*p.w*p.c*sizeof(float));
return copy;
}

void rgbgr_image(image im)
{
int i;
for(i = ; i < im.w*im.h; ++i){
float swap = im.data[i];
im.data[i] = im.data[i+im.w*im.h*];
im.data[i+im.w*im.h*] = swap;
}
}

#ifdef OPENCV
void show_image_cv(image p, const char *name)
{
int x,y,k;
image copy = copy_image(p);
constrain_image(copy);
if(p.c == ) rgbgr_image(copy);
//normalize_image(copy);

char buff\[\];  
//sprintf(buff, "%s (%d)", name, windows);  
sprintf(buff, "%s", name);

IplImage \*disp = cvCreateImage(cvSize(p.w,p.h), IPL\_DEPTH\_8U, p.c);  
int step = disp->widthStep;  
cvNamedWindow(buff, CV\_WINDOW\_NORMAL);  
//cvMoveWindow(buff, 100\*(windows%10) + 200\*(windows/10), 100\*(windows%10));  
++windows;  
for(y = ; y < p.h; ++y){  
    for(x = ; x < p.w; ++x){  
        for(k= ; k < p.c; ++k){  
            disp->imageData\[y\*step + x\*p.c + k\] = (unsigned char)(get\_pixel(copy,x,y,k)\*);  
        }  
    }  
}  
free\_image(copy);  
if(){  
    int w = ;  
    int h = w\*p.h/p.w;  
    if(h > ){  
        h = ;  
        w = h\*p.w/p.h;  
    }  
    IplImage \*buffer = disp;  
    disp = cvCreateImage(cvSize(w, h), buffer->depth, buffer->nChannels);  
    cvResize(buffer, disp, CV\_INTER\_LINEAR);  
    cvReleaseImage(&buffer);  
}  
cvShowImage(buff, disp);

cvReleaseImage(&disp);  

}

void show_image_cv_ipl(IplImage *disp, const char *name)
{
if (disp == NULL) return;
char buff[];
//sprintf(buff, "%s (%d)", name, windows);
sprintf(buff, "%s", name);
cvNamedWindow(buff, CV_WINDOW_NORMAL);
//cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
++windows;
cvShowImage(buff, disp);
//cvReleaseImage(&disp);
}
#endif

void show_image(image p, const char *name)
{
#ifdef OPENCV
show_image_cv(p, name);
#else
fprintf(stderr, "Not compiled with OpenCV, saving to %s.png instead\n", name);
save_image(p, name);
#endif
}

#ifdef OPENCV

image ipl_to_image(IplImage* src)
{
unsigned char *data = (unsigned char *)src->imageData;
int h = src->height;
int w = src->width;
int c = src->nChannels;
int step = src->widthStep;
image out = make_image(w, h, c);
int i, j, k, count=;;

for(k= ; k < c; ++k){  
    for(i = ; i < h; ++i){  
        for(j = ; j < w; ++j){  
            out.data\[count++\] = data\[i\*step + j\*c + k\]/.;  
        }  
    }  
}  
return out;  

}

image load_image_cv(char *filename, int channels)
{
IplImage* src = ;
int flag = -;
if (channels == ) flag = -;
else if (channels == ) flag = ;
else if (channels == ) flag = ;
else {
fprintf(stderr, "OpenCV can't force load with %d channels\n", channels);
}

if( (src = cvLoadImage(filename, flag)) ==  )  
{  
    fprintf(stderr, "Cannot load image \\"%s\\"\\n", filename);  
    char buff\[\];  
    sprintf(buff, "echo %s >> bad.list", filename);  
    system(buff);  
    return make\_image(,,);  
    //exit(0);  
}  
image out = ipl\_to\_image(src);  
cvReleaseImage(&src);  
rgbgr\_image(out);  
return out;  

}

image get_image_from_stream(CvCapture *cap)
{
IplImage* src = cvQueryFrame(cap);
if (!src) return make_empty_image(,,);
image im = ipl_to_image(src);
rgbgr_image(im);
return im;
}

image get_image_from_stream_resize(CvCapture *cap, int w, int h, IplImage** in_img, int cpp_video_capture)
{
printf("start to get_image_from_stream_resize……………..\n");
IplImage* src;
if (cpp_video_capture) {
static int once = ;
if (once) {
once = ;
do {
src = get_webcam_frame(cap);
if (!src) return make_empty_image(, , );
} while (src->width < || src->height < || src->nChannels < );
} else
src = get_webcam_frame(cap);
}
else src = cvQueryFrame(cap);

if (!src) return make\_empty\_image(, , );  
if (src->width <  || src->height <  || src->nChannels < ) return make\_empty\_image(, , );  
IplImage\* new\_img = cvCreateImage(cvSize(w, h), IPL\_DEPTH\_8U, );  
\*in\_img = cvCreateImage(cvSize(src->width, src->height), IPL\_DEPTH\_8U, );  
cvResize(src, \*in\_img, CV\_INTER\_LINEAR);  
cvResize(src, new\_img, CV\_INTER\_LINEAR);  
image im = ipl\_to\_image(new\_img);  
cvReleaseImage(&new\_img);  
if (cpp\_video\_capture) cvReleaseImage(&src);  
rgbgr\_image(im);  
printf("im is \\n",im);  
return im;  

}

void save_image_jpg(image p, const char *name)
{
image copy = copy_image(p);
if(p.c == ) rgbgr_image(copy);
int x,y,k;

char buff\[\];  
sprintf(buff, "%s.jpg", name);

IplImage \*disp = cvCreateImage(cvSize(p.w,p.h), IPL\_DEPTH\_8U, p.c);  
int step = disp->widthStep;  
for(y = ; y < p.h; ++y){  
    for(x = ; x < p.w; ++x){  
        for(k= ; k < p.c; ++k){  
            disp->imageData\[y\*step + x\*p.c + k\] = (unsigned char)(get\_pixel(copy,x,y,k)\*);  
        }  
    }  
}  
cvSaveImage(buff, disp,);  
cvReleaseImage(&disp);  
free\_image(copy);  

}
#endif

void save_image_png(image im, const char *name)
{
char buff[];
//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 = ; k < im.c; ++k){
for(i = ; i < im.w*im.h; ++i){
data[i*im.c+k] = (unsigned char) (*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);
}

void save_image(image im, const char *name)
{
#ifdef OPENCV
save_image_jpg(im, name);
#else
save_image_png(im, name);
#endif
}

void show_image_layers(image p, char *name)
{
int i;
char buff[];
for(i = ; i < p.c; ++i){
sprintf(buff, "%s - Layer %d", name, i);
image layer = get_image_layer(p, i);
show_image(layer, buff);
free_image(layer);
}
}

void show_image_collapsed(image p, char *name)
{
image c = collapse_image_layers(p, );
show_image(c, name);
free_image(c);
}

image make_empty_image(int w, int h, int c)
{
image out;
out.data = ;
out.h = h;
out.w = w;
out.c = c;
return out;
}

image make_image(int w, int h, int c)
{
image out = make_empty_image(w,h,c);
out.data = calloc(h*w*c, sizeof(float));
return out;
}

image make_random_image(int w, int h, int c)
{
image out = make_empty_image(w,h,c);
out.data = calloc(h*w*c, sizeof(float));
int i;
for(i = ; i < w*h*c; ++i){
out.data[i] = (rand_normal() * .) + .;
}
return out;
}

image float_to_image(int w, int h, int c, float *data)
{
image out = make_empty_image(w,h,c);
out.data = data;
return out;
}

image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect)
{
int x, y, c;
float cx = im.w/.;
float cy = im.h/.;
image rot = make_image(w, h, im.c);
for(c = ; c < im.c; ++c){
for(y = ; y < h; ++y){
for(x = ; x < w; ++x){
float rx = cos(rad)*((x - w/.)/s*aspect + dx/s*aspect) - sin(rad)*((y - h/.)/s + dy/s) + cx;
float ry = sin(rad)*((x - w/.)/s*aspect + dx/s*aspect) + cos(rad)*((y - h/.)/s + dy/s) + 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;
float cx = im.w/.;
float cy = im.h/.;
image rot = make_image(im.w, im.h, im.c);
for(c = ; c < im.c; ++c){
for(y = ; y < im.h; ++y){
for(x = ; 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 = bilinear_interpolate(im, rx, ry, c);
set_pixel(rot, x, y, c, val);
}
}
}
return rot;
}

void translate_image(image m, float s)
{
int i;
for(i = ; i < m.h*m.w*m.c; ++i) m.data[i] += s;
}

void scale_image(image m, float s)
{
int i;
for(i = ; i < m.h*m.w*m.c; ++i) m.data[i] *= s;
}

image crop_image(image im, int dx, int dy, int w, int h)
{
image cropped = make_image(w, h, im.c);
int i, j, k;
for(k = ; k < im.c; ++k){ for(j = ; j < h; ++j){ for(i = ; i < w; ++i){ int r = j + dy; int c = i + dx; float val = ; r = constrain_int(r, , im.h-); c = constrain_int(c, , im.w-); if (r >= && r < im.h && c >= && c < im.w) {
val = get_pixel(im, c, r, k);
}
set_pixel(cropped, i, j, k, val);
}
}
}
return cropped;
}

int best_3d_shift_r(image a, image b, int min, int max)
{
if(min == max) return min;
int mid = floor((min + max) / .);
image c1 = crop_image(b, , mid, b.w, b.h);
image c2 = crop_image(b, , mid+, b.w, b.h);
float d1 = dist_array(c1.data, a.data, a.w*a.h*a.c, );
float d2 = dist_array(c2.data, a.data, a.w*a.h*a.c, );
free_image(c1);
free_image(c2);
if(d1 < d2) return best_3d_shift_r(a, b, min, mid);
else return best_3d_shift_r(a, b, mid+, max);
}

int best_3d_shift(image a, image b, int min, int max)
{
int i;
int best = ;
float best_distance = FLT_MAX;
for(i = min; i <= max; i += ){
image c = crop_image(b, , i, b.w, b.h);
float d = dist_array(c.data, a.data, a.w*a.h*a.c, );
if(d < best_distance){
best_distance = d;
best = i;
}
printf("%d %f\n", i, d);
free_image(c);
}
return best;
}

void composite_3d(char *f1, char *f2, char *out, int delta)
{
if(!out) out = "out";
image a = load_image(f1, ,,);
image b = load_image(f2, ,,);
int shift = best_3d_shift_r(a, b, -a.h/, a.h/);

image c1 = crop\_image(b, , shift, b.w, b.h);  
float d1 = dist\_array(c1.data, a.data, a.w\*a.h\*a.c, );  
image c2 = crop\_image(b, -, shift, b.w, b.h);  
float d2 = dist\_array(c2.data, a.data, a.w\*a.h\*a.c, );

if(d2 < d1 && ){  
    image swap = a;  
    a = b;  
    b = swap;  
    shift = -shift;  
    printf("swapped, %d\\n", shift);  
}  
else{  
    printf("%d\\n", shift);  
}

image c = crop\_image(b, delta, shift, a.w, a.h);  
int i;  
for(i = ; i < c.w\*c.h; ++i){  
    c.data\[i\] = a.data\[i\];  
}  

#ifdef OPENCV
save_image_jpg(c, out);
#else
save_image(c, out);
#endif
}

void fill_image(image m, float s)
{
int i;
for (i = ; i < m.h*m.w*m.c; ++i) m.data[i] = s;
}

void letterbox_image_into(image im, int w, int h, image boxed)
{
int new_w = im.w;
int new_h = im.h;
if (((float)w / im.w) < ((float)h / im.h)) {
new_w = w;
new_h = (im.h * w) / im.w;
}
else {
new_h = h;
new_w = (im.w * h) / im.h;
}
image resized = resize_image(im, new_w, new_h);
embed_image(resized, boxed, (w - new_w) / , (h - new_h) / );
free_image(resized);
}

image letterbox_image(image im, int w, int h)
{
int new_w = im.w;
int new_h = im.h;
if (((float)w / im.w) < ((float)h / im.h)) {
new_w = w;
new_h = (im.h * w) / im.w;
}
else {
new_h = h;
new_w = (im.w * h) / im.h;
}
image resized = resize_image(im, new_w, new_h);
image boxed = make_image(w, h, im.c);
fill_image(boxed, .);
//int i;
//for(i = 0; i < boxed.w*boxed.h*boxed.c; ++i) boxed.data[i] = 0;
embed_image(resized, boxed, (w - new_w) / , (h - new_h) / );
free_image(resized);
return boxed;
}

image resize_max(image im, int max)
{
int w = im.w;
int h = im.h;
if(w > h){
h = (h * max) / w;
w = max;
} else {
w = (w * max) / h;
h = max;
}
if(w == im.w && h == im.h) return im;
image resized = resize_image(im, w, h);
return resized;
}

image resize_min(image im, int min)
{
int w = im.w;
int h = im.h;
if(w < h){
h = (h * min) / w;
w = min;
} else {
w = (w * min) / h;
h = min;
}
if(w == im.w && h == im.h) return im;
image resized = resize_image(im, w, h);
return resized;
}

image random_crop_image(image im, int w, int h)
{
int dx = rand_int(, im.w - w);
int dy = rand_int(, im.h - h);
image crop = crop_image(im, dx, dy, w, h);
return crop;
}

image random_augment_image(image im, float angle, float aspect, int low, int high, int size)
{
aspect = rand_scale(aspect);
int r = rand_int(low, high);
int min = (im.h < im.w*aspect) ? im.h : im.w*aspect;
float scale = (float)r / min;

float rad = rand\_uniform(-angle, angle) \* TWO\_PI / .;

float dx = (im.w\*scale/aspect - size) / .;  
float dy = (im.h\*scale - size) / .;  
if(dx < ) dx = ;  
if(dy < ) dy = ;  
dx = rand\_uniform(-dx, dx);  
dy = rand\_uniform(-dy, dy);

image crop = rotate\_crop\_image(im, rad, scale, size, size, dx, dy, aspect);

return crop;  

}

float three_way_max(float a, float b, float c)
{
return (a > b) ? ( (a > c) ? a : c) : ( (b > c) ? b : c) ;
}

float three_way_min(float a, float b, float c)
{
return (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c) ;
}

// http://www.cs.rit.edu/~ncs/color/t_convert.html
void rgb_to_hsv(image im)
{
assert(im.c == );
int i, j;
float r, g, b;
float h, s, v;
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
r = get_pixel(im, i , j, );
g = get_pixel(im, i , j, );
b = get_pixel(im, i , j, );
float max = three_way_max(r,g,b);
float min = three_way_min(r,g,b);
float delta = max - min;
v = max;
if(max == ){
s = ;
h = ;
}else{
s = delta/max;
if(r == max){
h = (g - b) / delta;
} else if (g == max) {
h = + (b - r) / delta;
} else {
h = + (r - g) / delta;
}
if (h < ) h += ;
h = h/.;
}
set_pixel(im, i, j, , h);
set_pixel(im, i, j, , s);
set_pixel(im, i, j, , v);
}
}
}

void hsv_to_rgb(image im)
{
assert(im.c == );
int i, j;
float r, g, b;
float h, s, v;
float f, p, q, t;
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
h = * get_pixel(im, i , j, );
s = get_pixel(im, i , j, );
v = get_pixel(im, i , j, );
if (s == ) {
r = g = b = v;
} else {
int index = floor(h);
f = h - index;
p = v*(-s);
q = v*(-s*f);
t = v*(-s*(-f));
if(index == ){
r = v; g = t; b = p;
} else if(index == ){
r = q; g = v; b = p;
} else if(index == ){
r = p; g = v; b = t;
} else if(index == ){
r = p; g = q; b = v;
} else if(index == ){
r = t; g = p; b = v;
} else {
r = v; g = p; b = q;
}
}
set_pixel(im, i, j, , r);
set_pixel(im, i, j, , g);
set_pixel(im, i, j, , b);
}
}
}

image grayscale_image(image im)
{
assert(im.c == );
int i, j, k;
image gray = make_image(im.w, im.h, );
float scale[] = {0.587, 0.299, 0.114};
for(k = ; k < im.c; ++k){
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
gray.data[i+im.w*j] += scale[k]*get_pixel(im, i, j, k);
}
}
}
return gray;
}

image threshold_image(image im, float thresh)
{
int i;
image t = make_image(im.w, im.h, im.c);
for(i = ; i < im.w*im.h*im.c; ++i){ t.data[i] = im.data[i]>thresh ? : ;
}
return t;
}

image blend_image(image fore, image back, float alpha)
{
assert(fore.w == back.w && fore.h == back.h && fore.c == back.c);
image blend = make_image(fore.w, fore.h, fore.c);
int i, j, k;
for(k = ; k < fore.c; ++k){
for(j = ; j < fore.h; ++j){
for(i = ; i < fore.w; ++i){
float val = alpha * get_pixel(fore, i, j, k) +
( - alpha)* get_pixel(back, i, j, k);
set_pixel(blend, i, j, k, val);
}
}
}
return blend;
}

void scale_image_channel(image im, int c, float v)
{
int i, j;
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
float pix = get_pixel(im, i, j, c);
pix = pix*v;
set_pixel(im, i, j, c, pix);
}
}
}

void translate_image_channel(image im, int c, float v)
{
int i, j;
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
float pix = get_pixel(im, i, j, c);
pix = pix+v;
set_pixel(im, i, j, c, pix);
}
}
}

image binarize_image(image im)
{
image c = copy_image(im);
int i;
for(i = ; i < im.w * im.h * im.c; ++i){ if(c.data[i] > .) c.data[i] = ;
else c.data[i] = ;
}
return c;
}

void saturate_image(image im, float sat)
{
rgb_to_hsv(im);
scale_image_channel(im, , sat);
hsv_to_rgb(im);
constrain_image(im);
}

void hue_image(image im, float hue)
{
rgb_to_hsv(im);
int i;
for(i = ; i < im.w*im.h; ++i){ im.data[i] = im.data[i] + hue; if (im.data[i] > ) im.data[i] -= ;
if (im.data[i] < ) im.data[i] += ;
}
hsv_to_rgb(im);
constrain_image(im);
}

void exposure_image(image im, float sat)
{
rgb_to_hsv(im);
scale_image_channel(im, , sat);
hsv_to_rgb(im);
constrain_image(im);
}

void distort_image(image im, float hue, float sat, float val)
{
rgb_to_hsv(im);
scale_image_channel(im, , sat);
scale_image_channel(im, , val);
int i;
for(i = ; i < im.w*im.h; ++i){ im.data[i] = im.data[i] + hue; if (im.data[i] > ) im.data[i] -= ;
if (im.data[i] < ) im.data[i] += ;
}
hsv_to_rgb(im);
constrain_image(im);
}

void random_distort_image(image im, float hue, float saturation, float exposure)
{
float dhue = rand_uniform_strong(-hue, hue);
float dsat = rand_scale(saturation);
float dexp = rand_scale(exposure);
distort_image(im, dhue, dsat, dexp);
}

void saturate_exposure_image(image im, float sat, float exposure)
{
rgb_to_hsv(im);
scale_image_channel(im, , sat);
scale_image_channel(im, , exposure);
hsv_to_rgb(im);
constrain_image(im);
}

float bilinear_interpolate(image im, float x, float y, int c)
{
int ix = (int) floorf(x);
int iy = (int) floorf(y);

float dx = x - ix;  
float dy = y - iy;

float val = (-dy) \* (-dx) \* get\_pixel\_extend(im, ix, iy, c) +  
    dy     \* (-dx) \* get\_pixel\_extend(im, ix, iy+, c) +  
    (-dy) \*   dx   \* get\_pixel\_extend(im, ix+, iy, c) +  
    dy     \*   dx   \* get\_pixel\_extend(im, ix+, iy+, c);  
return val;  

}

image resize_image(image im, int w, int h)
{
image resized = make_image(w, h, im.c);
image part = make_image(w, im.h, im.c);
int r, c, k;
float w_scale = (float)(im.w - ) / (w - );
float h_scale = (float)(im.h - ) / (h - );
for(k = ; k < im.c; ++k){
for(r = ; r < im.h; ++r){
for(c = ; c < w; ++c){
float val = ;
if(c == w- || im.w == ){
val = get_pixel(im, im.w-, r, k);
} else {
float sx = c*w_scale;
int ix = (int) sx;
float dx = sx - ix;
val = ( - dx) * get_pixel(im, ix, r, k) + dx * get_pixel(im, ix+, r, k);
}
set_pixel(part, c, r, k, val);
}
}
}
for(k = ; k < im.c; ++k){
for(r = ; r < h; ++r){
float sy = r*h_scale;
int iy = (int) sy;
float dy = sy - iy;
for(c = ; c < w; ++c){
float val = (-dy) * get_pixel(part, c, iy, k);
set_pixel(resized, c, r, k, val);
}
if(r == h- || im.h == ) continue;
for(c = ; c < w; ++c){
float val = dy * get_pixel(part, c, iy+, k);
add_pixel(resized, c, r, k, val);
}
}
}

free\_image(part);  
return resized;  

}

void test_resize(char *filename)
{
image im = load_image(filename, ,, );
float mag = mag_array(im.data, im.w*im.h*im.c);
printf("L2 Norm: %f\n", mag);
image gray = grayscale_image(im);

image c1 = copy\_image(im);  
image c2 = copy\_image(im);  
image c3 = copy\_image(im);  
image c4 = copy\_image(im);  
distort\_image(c1, ., 1.5, 1.5);  
distort\_image(c2, -., ., .);  
distort\_image(c3, ., 1.5, .);  
distort\_image(c4, ., ., 1.5);

show\_image(im,   "Original");  
show\_image(gray, "Gray");  
show\_image(c1, "C1");  
show\_image(c2, "C2");  
show\_image(c3, "C3");  
show\_image(c4, "C4");  

#ifdef OPENCV
while(){
image aug = random_augment_image(im, , ., , , );
show_image(aug, "aug");
free_image(aug);

    float exposure = 1.15;  
    float saturation = 1.15;  
    float hue = .;

    image c = copy\_image(im);

    float dexp = rand\_scale(exposure);  
    float dsat = rand\_scale(saturation);  
    float dhue = rand\_uniform(-hue, hue);

    distort\_image(c, dhue, dsat, dexp);  
    show\_image(c, "rand");  
    printf("%f %f %f\\n", dhue, dsat, dexp);  
    free\_image(c);  
    cvWaitKey();  
}  

#endif
}

image load_image_stb(char *filename, int channels)
{
int w, h, c;
unsigned char *data = stbi_load(filename, &w, &h, &c, channels);
if (!data) {
fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n", filename, stbi_failure_reason());
exit();
}
if(channels) c = channels;
int i,j,k;
image im = make_image(w, h, c);
for(k = ; k < c; ++k){
for(j = ; j < h; ++j){
for(i = ; i < w; ++i){
int dst_index = i + w*j + w*h*k;
int src_index = k + c*i + c*w*j;
im.data[dst_index] = (float)data[src_index]/.;
}
}
}
free(data);
return im;
}

image load_image(char *filename, int w, int h, int c)
{
#ifdef OPENCV

#ifndef CV_VERSION_EPOCH
//image out = load_image_stb(filename, c); // OpenCV 3.x
image out = load_image_cv(filename, c);
#else
image out = load_image_cv(filename, c); // OpenCV 2.4.x
#endif

#else
image out = load_image_stb(filename, c); // without OpenCV
#endif

if((h && w) && (h != out.h || w != out.w)){  
    image resized = resize\_image(out, w, h);  
    free\_image(out);  
    out = resized;  
}  
return out;  

}

image load_image_color(char *filename, int w, int h)
{
return load_image(filename, w, h, );
}

image get_image_layer(image m, int l)
{
image out = make_image(m.w, m.h, );
int i;
for(i = ; i < m.h*m.w; ++i){
out.data[i] = m.data[i+l*m.h*m.w];
}
return out;
}

void print_image(image m)
{
int i, j, k;
for(i = ; i < m.c; ++i){ for(j = ; j < m.h; ++j){ for(k = ; k < m.w; ++k){ printf("%.2lf, ", m.data[i*m.h*m.w + j*m.w + k]); if(k > ) break;
}
printf("\n");
if(j > ) break;
}
printf("\n");
}
printf("\n");
}

image collapse_images_vert(image *ims, int n)
{
int color = ;
int border = ;
int h,w,c;
w = ims[].w;
h = (ims[].h + border) * n - border;
c = ims[].c;
if(c != || !color){
w = (w+border)*c - border;
c = ;
}

image filters = make\_image(w, h, c);  
int i,j;  
for(i = ; i < n; ++i){  
    int h\_offset = i\*(ims\[\].h+border);  
    image copy = copy\_image(ims\[i\]);  
    //normalize\_image(copy);  
    if(c ==  && color){  
        embed\_image(copy, filters, , h\_offset);  
    }  
    else{  
        for(j = ; j < copy.c; ++j){  
            int w\_offset = j\*(ims\[\].w+border);  
            image layer = get\_image\_layer(copy, j);  
            embed\_image(layer, filters, w\_offset, h\_offset);  
            free\_image(layer);  
        }  
    }  
    free\_image(copy);  
}  
return filters;  

}

image collapse_images_horz(image *ims, int n)
{
int color = ;
int border = ;
int h,w,c;
int size = ims[].h;
h = size;
w = (ims[].w + border) * n - border;
c = ims[].c;
if(c != || !color){
h = (h+border)*c - border;
c = ;
}

image filters = make\_image(w, h, c);  
int i,j;  
for(i = ; i < n; ++i){  
    int w\_offset = i\*(size+border);  
    image copy = copy\_image(ims\[i\]);  
    //normalize\_image(copy);  
    if(c ==  && color){  
        embed\_image(copy, filters, w\_offset, );  
    }  
    else{  
        for(j = ; j < copy.c; ++j){  
            int h\_offset = j\*(size+border);  
            image layer = get\_image\_layer(copy, j);  
            embed\_image(layer, filters, w\_offset, h\_offset);  
            free\_image(layer);  
        }  
    }  
    free\_image(copy);  
}  
return filters;  

}

void show_image_normalized(image im, const char *name)
{
image c = copy_image(im);
normalize_image(c);
show_image(c, name);
free_image(c);
}

void show_images(image *ims, int n, char *window)
{
image m = collapse_images_vert(ims, n);
/*
int w = 448;
int h = ((float)m.h/m.w) * 448;
if(h > 896){
h = 896;
w = ((float)m.w/m.h) * 896;
}
image sized = resize_image(m, w, h);
*/
normalize_image(m);
save_image(m, window);
show_image(m, window);
free_image(m);
}

void free_image(image m)
{
if(m.data){
free(m.data);
}
}

上边的代码是darknet/image.c的相关代码,我们主要在函数draw_detections_v3下进行修改,主要几处修改如下:

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器