我需要在C/C++中读取图像文件。这将是非常伟大的,如果有人可以为我张贴的代码。
我在灰度图像上工作,图像是JPEG格式。我想将图像读入一个2D数组,这将使我的工作变得容易。
发布于 2016-11-26 06:31:40
如果你决定使用最小的方法,没有libpng/libjpeg依赖,我建议使用stb_image和stb_image_write,找到here。
这很简单,你只需要把头文件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其他轻量级替代方案包括:
读写png的
发布于 2010-01-16 15:02:48
您可以通过查看JPEG format来编写自己的代码。
也就是说,可以尝试像CImg或Boost's GIL这样的预先存在的库。或者严格意义上的JPEG,libjpeg。在CodeProject上还有CxImage类。
这是一个big list。
发布于 2010-01-16 15:49:54
查看英特尔开放式CV库 ...
https://stackoverflow.com/questions/2076475
复制相似问题