在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:01:15  来源:igfitidea点击:

Reading an image file in C/C++

c++cjpeg

提问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.

也就是说,尝试一个预先存在的库,如CImgBoost 的 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_imageand stb_image_write, found here.

如果您决定采用最少的方法,没有 libpng/libjpeg 依赖项,我建议使用stb_imagestb_image_write,在此处找到。

It's as simple as it gets, you just need to place the header files stb_image.hand stb_image_write.hin your folder.

它是那样简单,因为它得到,你只需要放置头文件stb_image.hstb_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:

其他轻量级替代品包括:

回答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

回答by mwcz

Try out the CImglibrary. The tutorialwill help you get familiarized. Once you have a CImg object, the data()function will give you access to the 2D pixel buffer array.

试用CImg库。本教程将帮助您熟悉。一旦你有了一个 CImg 对象,data()函数就会让你访问 2D 像素缓冲区数组。