图片简单操作的库stb

2020-01-06  本文已影响0人  半笔闪

阿里巴巴的深度学习前向推理库MNN中使用了一个图像库stb,github地址是https://github.com/nothings/stb
,这个库包含一堆.h头文件,如果你想要对图片进行简单操作(如:将图像数据载入内存、进行缩放、图像保存等),又不想因为这些简单操作,引入opencv等大库,那stb将是一个好选择。

int x, y, channels_in_file, desired_channels = 3;
        unsigned char* data = stbi_load(image.c_str(), &x, &y, &channels_in_file, desired_channels);
                free(data);
int width_resize = x * 1.5, height_resize = y * 1.4;
        unsigned char* output_pixels = (unsigned char*)malloc(width_resize * height_resize * desired_channels);
        int ret = stbir_resize_uint8(data, x, y, 0, output_pixels, width_resize, height_resize, 0, desired_channels);
                free(output_pixels);
                const std::string save_name_png = image + ".png";
        const std::string save_name_jpg = image + ".jpg";
 
        ret = stbi_write_png(save_name_png.c_str(), width_resize, height_resize, desired_channels, output_pixels, 0);
        if (ret == 0) {
            fprintf(stderr, "fail to write image png: %s\n", image.c_str());
            return -1;
        }
 
        ret = stbi_write_jpg(save_name_jpg.c_str(), width_resize, height_resize, desired_channels, output_pixels, 90);
        if (ret == 0) {
            fprintf(stderr, "fail to write image jpg: %s\n", image.c_str());
            return -1;
        }
上一篇 下一篇

猜你喜欢

热点阅读