问题描述
我需要阅读C/C ++中的图像文件.如果有人可以为我发布代码,那就太好了.
我在灰度图像上工作,图像是JPEG.我想将图像阅读到2D阵列中,这将使我的工作变得容易.
推荐答案
如果您决定采用最小的方法,而没有libpng/libjpeg依赖性,我建议使用stb_image和stb_image_write,找到在这里.
这很简单,您只需要将标题文件stb_image.h和stb_image_write.h放在文件夹中即可.
这是您需要读取图像的代码:
#include <stdint.h> #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" int main() { int width, height, bpp; uint8_t* rgb_image = stbi_load("image.png", &width, &height, &bpp, 3); stbi_image_free(rgb_image); return 0; }
这是编写图像的代码:
#include <stdint.h> #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" #define CHANNEL_NUM 3 int main() { int width = 800; int height = 800; uint8_t* rgb_image; rgb_image = malloc(width*height*CHANNEL_NUM); // Write your code to populate rgb_image here stbi_write_png("image.png", width, height, CHANNEL_NUM, rgb_image, width*CHANNEL_NUM); return 0; }
您可以无需标志或依赖项编译:
g++ main.cpp
其他轻巧的替代方案包括:
- lodepng 读写png文件
- jpeg-compressor
其他推荐答案
您可以通过查看 jpeg格式.
也就是说,尝试一个预先存在的库,例如 cimg 或 boost的gil .或严格的jpeg, libjpeg .还有 cximage codeproject.
上课这是大列表..
其他推荐答案
查看英特尔打开CV库 ...