位图上的 C++ 模糊效果有效但颜色已更改

标签 c++ bitmap

关于我之前的问题 BitMap_blur efect ,我已经成功地使位图模糊,但问题是模糊图片的颜色已更改:

原图:https://ibb.co/eFHg8G 照片模糊:https://ibb.co/mQDShb

blurring algorytm 的代码和我之前的问题一样:

for (xx = 0; xx < bitmapInfoHeader.biWidth; xx++)
{
    for (yy = 0; yy <bitmapInfoHeader.biHeight; yy++)
    {
        avgB = avgG = avgR = 0;
        Counter = 0;

        for (x = xx; x < bitmapInfoHeader.biWidth && x < xx + blurSize; x++)
        {


            for (y = yy; y < bitmapInfoHeader.biHeight && y < yy + blurSize; y++)
            {
                avgB += bitmapImage[x *3 + y*bitmapInfoHeader.biWidth * 3 + 0];     //bitmapimage[x][y];
                avgG += bitmapImage[x  *3 + y*bitmapInfoHeader.biWidth * 3 + 1];
                avgR += bitmapImage[x *3 + y*bitmapInfoHeader.biWidth * 3 + 2];
                Counter++;
            }
        }

        avgB = avgB / Counter;
        avgG = avgG / Counter;
        avgR = avgR / Counter;

        bitmapImage[xx * 3 + yy*bitmapInfoHeader.biWidth * 3 + 0] = avgB;
        bitmapImage[xx * 3 + yy*bitmapInfoHeader.biWidth * 3 + 1] = avgG;
        bitmapImage[xx * 3 + yy*bitmapInfoHeader.biWidth * 3 + 2] = avgR;
    }
}

那么这里做错了什么?

最佳答案

实际上看起来每一行的大小都被填充为 4 字节的倍数。要获得每行的正确字节偏移量,您需要替换

* bitmapInfoHeader.biWidth * 3

* (bitmapInfoHeader.biWidth * 3 + padding_bytes_count)

在哪里

padding_bytes_count =
(
    (
        bitmapFileHeader.bfSize - bitmapFileHeader.bfOffBits
        -
        bitmapInfoHeader.biWidth * bitmapInfoHeader.biHeight * 3
    )
    /
    bitmapInfoHeader.biHeight
);

对于你的老虎图像,padding_bytes_count 应该是 2。

关于位图上的 C++ 模糊效果有效但颜色已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209262/

相关文章:

c# - 图像裁剪无法正常工作

c++ - 旧版 OpenGL 纹理立方体

c++ - 静态 OpenCV 库中 undefined reference

c++ - 使本地 c 库函数可全局访问

c++ - GenICam::gcstring 来自 int

actionscript-3 - BitmapData类的threshold方法中mask参数有什么作用?

c++ - 使用 unique_ptr 保护析构函数

android - 创建一个位图,里面有一个位图

delphi - 在 GetObject() 调用后,Graphics.TBitmap 成功从 JPEG 帧解码导致空 TDibSection

c++ - 为什么我的代码不渲染灰度效果