在c++中将RGB图像更改为灰度

问题描述 投票:0回答:1

我正在尝试使用 C++ 将图像从 RGB 转换为灰度。我的结果更加颗粒化,我不知道如何修复它们,因为相同的方法适用于汇编版本。任何帮助将不胜感激。

示例图片: sample image

预期结果: expected image

我的结果: my results

我正在使用的代码:

#include "pch.h"
#include <utility>
#include <limits.h>
#include "C++.h"

void c_gradient_filter(char* values, int width, int height, int pixelSize, char* converted_values)
{
    int size = width * height * pixelSize;

    for (int i = 0; i < size; i += pixelSize) {
        // Extract color channels
        int blue = values[i];
        int green = values[i + 1];
        int red = values[i + 2];

        int gray = static_cast<int>((red+blue+green)/3);

        // Set the grayscale value for all channels
        converted_values[i] = converted_values[i + 1] = converted_values[i + 2] = static_cast<char>(gray);
    }
}

我尝试使用 ITU-R BT.709 公式,但这也不起作用

c++ image filter processing rgb
1个回答
0
投票

您的

char
可能是默认签名的,因此它的值为 -128..127,而不是您期望的 0..255。使用前投射到
unsigned char

© www.soinside.com 2019 - 2024. All rights reserved.