简体   繁体   English

Stb 图像未正确加载数据

[英]Stb Image not loading in data correctly

Im using the stb_image.h library and my goal is to get the rgb data for each of the pixels of a jpg.我使用stb_image.h库,我的目标是获取 jpg 的每个像素的 rgb 数据。 But when I call the load function, it only gives me a bunch of weird symbols and not actual numbers.但是当我调用负载 function 时,它只给了我一堆奇怪的符号而不是实际数字。 Could this be due to the image or is my code just wrong?这可能是由于图像还是我的代码错了?

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#include <iostream>
#include <string>

using namespace std;
int main()
{
    int width, height;
    int comp;
    const char* filename = "C:/Users/graha/source/repos/AiFirsy/Debug/rainbow.jpg";

    unsigned char* data = stbi_load(
        filename, &width, &height, &comp, 0);

    if (data == nullptr) {
        std::cerr << "ERROR: Could not load texture image file '" << filename << "'.\n";
        width = height = 0;
    }

    cout << data[0];

}

Heres the image这是图像

彩虹.jpg

The output stream ( std::cout in this case) will interpret 'char' types as characters . output stream (在这种情况下为std::cout )会将'char' 类型解释为 characters If you want to see the numerical values, you can cast the values to an appropriate type.如果要查看数值,可以将值转换为适当的类型。 For example:例如:

unsigned char const value = 'c';
    
std::cout << value << std::endl;
std::cout << static_cast<unsigned int>(value) << std::endl;

Outputs:输出:

c
99

Also, as an aside, you're accessing data even if it's null, which I'm guessing isn't what you want.另外,顺便说一句,您正在访问data ,即使它是 null,我猜这不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM