在 C/C++ 中读取图像文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2076475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading an image file in C/C++
提问by skyrulz
I need to read an image file in C/C++. It would be very great, if some one can post the code for me.
我需要在 C/C++ 中读取图像文件。如果有人可以为我发布代码,那就太好了。
I work on gray scale images and the images are JPEG. I would like to read the images into a 2D array which will make my work easy.
我处理灰度图像,图像是 JPEG。我想将图像读入一个二维数组,这将使我的工作变得容易。
回答by GManNickG
You could write your own by looking at the JPEG format.
您可以通过查看JPEG 格式来编写自己的文件。
That said, try a pre-existing library like CImg, or Boost's GIL. Or for strictly JPEG's, libjpeg. There is also the CxImageclass on CodeProject.
也就是说,尝试一个预先存在的库,如CImg或Boost 的 GIL。或者对于严格的 JPEG,libjpeg。CodeProject 上还有CxImage类。
Here's a big list.
这是一个很大的清单。
回答by Jaime Ivan Cervantes
If you decide to go for a minimal approach, without libpng/libjpeg dependencies, I suggest using stb_image
and stb_image_write
, found here.
如果您决定采用最少的方法,没有 libpng/libjpeg 依赖项,我建议使用stb_image
和stb_image_write
,在此处找到。
It's as simple as it gets, you just need to place the header files stb_image.h
and stb_image_write.h
in your folder.
它是那样简单,因为它得到,你只需要放置头文件stb_image.h
和stb_image_write.h
你的文件夹中。
Here's the code that you need to read images:
这是您读取图像所需的代码:
#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;
}
And here's the code to write an image:
这是编写图像的代码:
#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;
}
You can compile without flags or dependencies:
您可以在没有标志或依赖项的情况下进行编译:
g++ main.cpp
Other lightweight alternatives include:
其他轻量级替代品包括:
- lodepngto read and write png files
- jpeg-compressorto read and write jpeg files
- lodepng读取和写入 png 文件
- jpeg-compressor读取和写入 jpeg 文件
回答by Ashish
Check out Intel Open CV library...
查看英特尔 Open CV 库...
回答by Lazer
Check this thread out: read and write image file.
检查此线程:读取和写入图像文件。
Also, have a look at this other question at Stackoverflow.
另外,看看Stackoverflow 上的另一个问题。
回答by Firas Assaad
coronais nice. From the tutorial:
电晕很好。从教程:
corona::Image* image = corona::OpenImage("img.jpg", corona::PF_R8G8B8A8);
if (!image) {
// error!
}
int width = image->getWidth();
int height = image->getHeight();
void* pixels = image->getPixels();
// we're guaranteed that the first eight bits of every pixel is red,
// the next eight bits is green, and so on...
typedef unsigned char byte;
byte* p = (byte*)pixels;
for (int i = 0; i < width * height; ++i) {
byte red = *p++;
byte green = *p++;
byte blue = *p++;
byte alpha = *p++;
}
pixels would be a one dimensional array, but you could easily convert a given x and y position to a position in a 1D array. Something like pos = (y * width) + x
pixel 将是一维数组,但您可以轻松地将给定的 x 和 y 位置转换为一维数组中的位置。像 pos = (y * width) + x
回答by R Samuel Klatchko
Check out the Magick++ APIto ImageMagick.