带有unsigned char的黑/白图像是蓝色而不是黑色C ++

时间:2014-02-12 09:10:37

标签: c++ image image-processing binary char

我正在阅读.img灰度图像(512字节标题,512 x 512像素x每像素8位)。

我正在确定将灰度图像转换为二进制图像(黑/白)的阈值。

但是当我为像素分配0时,结果是浅蓝色(#01fffd)?!?!当我使用值2时,它几乎是黑色(#020100)。

你能解释一下这是怎么来的吗?

我读了这样的图像:

int width = 512;
int height = 512;
File* imageFilePointerIn = fopen("image.img", "r+b");
File* binaryImageFilePointerOut = fopen("imageOut.img",
            "w+b");
unsigned char* header = new unsigned char[512];
unsigned char** imageData = new unsigned char*[width];
unsigned char** binaryImageArray = new unsigned char*[width];
fread(header, sizeof(char), 512, imageFilePointerIn);
for (int i = 0; i < width; i++) {
    data[i] = new unsigned char[height];
    fread(data[i], sizeof(char), height, imageFilePointerIn);
}
for (int i = 0; i < width; i++) {
    binaryImageArray[i] = new unsigned char[height];
}
char unsigned thresholdBinaryImage = 128;
for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
        if (thresholdBinaryImage <= data[i][j]) {
            binaryImageArray[i][j] = 255;
        } else {
            binaryImageArray[i][j] = 0;
        }
    }
}
fwrite(header, sizeof(char), headerSize, binaryImageFilePointerOut);
for (int i = 0; i < width; i++) {
    fwrite(binaryImageArray[i], sizeof(char), height,
           binaryImageFilePointerOut);
}
fclose(binaryImageFilePointerOut);
fclose(imageFilePointerIn);

我不知道为什么要显示颜色? 为了显示我将.img文件转移到.ras文件中,其中包含从互联网上剪下的代码。但我不认为错误可能存在,因为我看到其他人使用相同的代码将.img Grayscales转换为.ras只是为了显示没有颜色。

很高兴得到任何建议:)。

0 个答案:

没有答案