1

我正在尝试实现拜耳有序抖动矩阵算法以将 24 位彩色图像转换为 3 位图像。我已经阅读了维基百科页面,以及有关该主题的几个教科书部分,并且有点困惑。这是我到目前为止所拥有的:

for (int y = 0; x < image.Height; y++)
{  
    for (int x = 0; x < image.Width; x++)
    {
        Color color = image.GetPixel(x,y);  
        color.R = color.R + bayer4x4[x % 4, y % 4];  
        color.G = color.G + bayer4x4[x % 4, y % 4];  
        color.B = color.B + bayer4x4[x % 4, y % 4];  
        image[x][y] = SetPixel(x, y, GetClosestColor(color, bitdepth);  
    }  
}

但是,我没有实现 GetClosestColor 的方法......我该怎么做?

另外,我没有定义bayer4x4矩阵,我相信它应该如下所示:

1,  9,  3, 11
13, 5, 15, 7
4, 12,  2, 10
16, 8, 14, 6
4

1 回答 1

1

这是我对拜耳 4x4 的实现。
输入和输出都是 24 位 BGR 格式(用于可视化目的)。
在输出中,每个像素平面的值都是 0 或 255。

const   int BAYER_PATTERN_4X4[4][4]     =   {   //  4x4 Bayer Dithering Matrix. Color levels: 17
                                                {    15, 195,  60, 240  },
                                                {   135,  75, 180, 120  },
                                                {    45, 225,  30, 210  },
                                                {   165, 105, 150,  90  }

                                            };

void    makeColorDitherBayer4x4( BYTE* pixels, int width, int height )  noexcept
{
    int col = 0;
    int row = 0;

    for( int y = 0; y < height; y++ )
    {
        row = y & 3;    //  % 4
        
        for( int x = 0; x < width; x++ )
        {
            col = x & 3;    //  % 4

            const pixel blue    = pixels[x * 3 + 0];
            const pixel green   = pixels[x * 3 + 1];
            const pixel red     = pixels[x * 3 + 2];

            pixels[x * 3 + 0]   = (blue  < BAYER_PATTERN_4X4[col][row] ? 0 : 255);
            pixels[x * 3 + 1]   = (green < BAYER_PATTERN_4X4[col][row] ? 0 : 255);
            pixels[x * 3 + 2]   = (red   < BAYER_PATTERN_4X4[col][row] ? 0 : 255);

        }

        pixels  += width * 3;
    }
}

如果您想将输出作为“压缩”数据,您应该替换这部分代码:

pixels[x * 3 + 0]   = (blue  < BAYER_PATTERN_4X4[col][row] ? 0 : 255);
pixels[x * 3 + 1]   = (green < BAYER_PATTERN_4X4[col][row] ? 0 : 255);
pixels[x * 3 + 2]   = (red   < BAYER_PATTERN_4X4[col][row] ? 0 : 255);

像这样:

output.writeBit( (blue  < BAYER_PATTERN_4X4[col][row] ? 0 : 1) );
output.writeBit( (green < BAYER_PATTERN_4X4[col][row] ? 0 : 1) );
output.writeBit( (red   < BAYER_PATTERN_4X4[col][row] ? 0 : 1) );
于 2021-06-30T10:00:57.460 回答