C++将32位深图片处理成灰度图
图片像素读取参考:
https://blog.csdn.net/ceffans/article/details/142357189?spm=1001.2014.3001.5502
拿到像素值后对像素做灰度化处理,根据RGB颜色的权重计算灰度值。
计算公式为:灰度 = RED * 0.299 + GREEN * 0.587 + BLUE 0.114*
std::shared_ptr<unsigned char> pixelsGray(new unsigned char[width * height]);
for (int i = 0; i < width * height; ++i) {
int r = pixels32[i * 4];
int g = pixels32[i * 4 + 1];
int b = pixels32[i * 4 + 2];
int gray = 0.299 * r + 0.587 * g + 0.114 * b;
pixelsGray.get()[i] = static_cast<unsigned char>(gray);
}