关于我之前的问题 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/