letterbox_image对图像调整成输入尺寸(w,h)
//将原图进行一定比例的缩放,返回的图片尺寸为(w,h)
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_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, .5); //填充,why 0.5?相当于127,灰度!
//int i;
//for(i = 0; i < boxed.w*boxed.h*boxed.c; ++i) boxed.data[i] = 0;
//将放缩后的图片复制入boxed图片正中央
embed_image(resized, boxed, (w-new_w)/2, (h-new_h)/2);
free_image(resized); //resized保存的是图片放缩的中间结果,临时用
return boxed; //返回的图像尺寸为需要的(w,h)
}
按照偏移(dx,dy)将source中的图像移动到dest中,source和dest都是darknet中自定义的image结构,保存图像的基本信息,以及
通过一维数组的形式保存了图像的原始数据(可能是标准化后的数据)。
//move image from source to dest according to the offset (dx,dy)
void embed_image(image source, image dest, int dx, int dy)
{
int x,y,k;
for(k = 0; k < source.c; ++k){
for(y = 0; y < source.h; ++y){
for(x = 0; x < source.w; ++x){
//channel->height->width
float val = get_pixel(source, x,y,k);
set_pixel(dest, dx+x, dy+y, k, val);
}
}
}
}