load_image代码:
image load_image(char *filename, int w, int h, int c)
{
#ifdef OPENCV
//image out = load_image_stb(filename, c);
image out = load_image_cv(filename, c);
#else
image out = load_image_stb(filename, c); // without OpenCV
#endif // OPENCV
if((h && w) && (h != out.h || w != out.w)){
image resized = resize_image(out, w, h);
free_image(out);
out = resized;
}
return out;
}
load_image_stb()
image load_image_stb(char *filename, int channels)
{
int w, h, c;
unsigned char *data = stbi_load(filename, &w, &h, &c, channels);
if (!data) {
char shrinked_filename[1024];
if (strlen(filename) >= 1024) sprintf(shrinked_filename, "name is too long");
else sprintf(shrinked_filename, "%s", filename);
fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n", shrinked_filename, stbi_failure_reason());
FILE* fw = fopen("bad.list", "a");
fwrite(shrinked_filename, sizeof(char), strlen(shrinked_filename), fw);
char *new_line = "\n";
fwrite(new_line, sizeof(char), strlen(new_line), fw);
fclose(fw);
if (check_mistakes) getchar();
return make_image(10, 10, 3);
//exit(EXIT_FAILURE);
}
if(channels) c = channels;
int i,j,k;
image im = make_image(w, h, c);
for(k = 0; k < c; ++k){
for(j = 0; j < h; ++j){
for(i = 0; 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]/255.;
}
}
}
free(data);
return im;
}
make_image()
image make_image(int w, int h, int c)
{
image out = make_empty_image(w,h,c);
out.data = (float*)calloc(h * w * c, sizeof(float));
return out;
}
image结构体定义如下:
// image.h
typedef struct image {
int w;
int h;
int c;
float *data;
} image;
make_empty_image
image make_empty_image(int w, int h, int c)
{
image out;
out.data = 0;
out.h = h;
out.w = w;
out.c = c;
return out;
}
std_image加载图片函数:stbi_load()
stbi_load加载图片是怎样加载的?可以参见文章:stb_image库加载图片函数研究
load_image_cv()
这里暂时不研究了,先研究一种……