【问题标题】:How to get pixel value of a grayscale image with 16 bits depth using libpng library?如何使用 libpng 库获取 16 位深度的灰度图像的像素值?
【发布时间】:2021-12-23 19:49:11
【问题描述】:

我有一个 16 位深度的灰度图像。我想使用 libpng 库获取图像的像素值。但是,我的程序的输出是该图像的所有像素值,介于 0 到 255 之间。我用 ImageJ 软件检查了该图像以及它们在 0 到 65535 之间的事实。我不知道我哪里出错了。这是我的代码。

#include <stdio.h>
#include <png.h>

int main(void) {
    FILE *fp = fopen("sample.png", "r");

    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    png_infop png_info = png_create_info_struct(png_ptr);

    png_init_io(png_ptr, fp);

    png_bytepp rows;

    png_read_png(png_ptr, png_info, PNG_TRANSFORM_IDENTITY, NULL);

    int WIDTH      = png_get_image_width(png_ptr, png_info);
    int HEIGHT     = png_get_image_height(png_ptr, png_info);

    rows = png_get_rows(png_ptr, png_info);

    for (int h = 0; h < HEIGHT; h++) {
        for (int w = 0; w < WIDTH; w++) {
            printf("%d ", rows[h][w]);
        }   
        printf("\n");
    }
    return 0;
}

请帮助我。非常感谢。

【问题讨论】:

  • 请不要标记您不使用的编程语言。如果您有 C 程序,则不要添加 C++ 标签。 C 和 C++ 是两种截然不同的语言。
  • @Someprogrammerdude 我是 StackOverFlow 的新手。我很抱歉。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: c image image-processing libpng


【解决方案1】:

如果您更改以下几行的类型,它会起作用:

unsigned short **rows;

rows = (unsigned short**)png_get_rows(png_ptr, png_info);

【讨论】:

  • 感谢您的帮助,图像的像素值一直在 0 到 65535 之间。但是它们与 ImageJ 软件的输出不匹配。
  • 哦!请您分享解决问题所需的信息 - 可能使用 Dropbox 或 Google Drive,因为 StackOverflow 上的 imgur 会减少、压缩和剥离文件。有用的信息是 1) 原始 PNG,2) 程序的几个像素输出和 3) ImageJ 计算的相应像素(屏幕截图很好)。谢谢。
猜你喜欢
  • 2020-05-15
  • 2023-03-07
  • 2012-06-13
  • 2015-06-08
  • 1970-01-01
  • 2020-12-23
  • 2015-06-29
  • 2017-11-17
  • 1970-01-01
相关资源
最近更新 更多