qt图像处理技术四:图像二值化
github
如果你觉得有用的话,期待你的小星星 github:https://github.com/dependon/QImageFilter/tree/main //qt美颜滤镜(不使用opencv) 实战应用项目: github :https://github.com/dependon/simple-image-filter //纯qt图像处理项目
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/9167b7ae68714d9d8fa44f528e2a8f89.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5aSP5pyJ5YeJ6aOO77yM5Yas5pyJ6Zuq,size_20,color_FFFFFF,t_70,g_se,x_16) ![在这里插入图片描述](https://img-blog.csdnimg.cn/a2148b7d14114759b281de3e9169acd5.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5aSP5pyJ5YeJ6aOO77yM5Yas5pyJ6Zuq,size_20,color_FFFFFF,t_70,g_se,x_16)
原理
rgb 每一点取rgb的平均值, 当平均值>128,设置点为255,255,255 当平均值<128,设置点为0,0,0
源码
QImage Binaryzation(const QImage &origin)
{
int width = origin.width();
int height = origin.height();
QImage newImg = QImage(width, height, QImage::Format_RGB888);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int gray = qGray(origin.pixel(x, y));
int newGray;
if (gray > 128)
newGray = 255;
else
newGray = 0;
newImg.setPixel(x, y, qRgb(newGray, newGray, newGray));
}
}
return newImg;
}
|