1.介绍
反色的实际含义是将R、G、B值反转。如果颜色的范围为0-255之间,则新图的R、G、B值为255减去原图的R、G、B值。其公式为:
R_new = 255 - R_old
G_new = 255 - G_old
B_new = 255 - B_old
2.代码实现(代码是我以前自学图像处理时写的,代码很粗糙没做任何优化,但很好理解)
/*反色操作*/
QImage* MainWindow::inverseColor(QImage* origiin)
{QImage* newImage = new QImage(origiin->width(), origiin->height(), QImage::Format_ARGB32);QColor oldColor;int r = 0;int g = 0;int b = 0;for(int y = 0; y < newImage->height(); y++){for(int x = 0; x < newImage->width(); x++){oldColor = QColor(origiin->pixel(x,y));r = 255 - oldColor.red();g = 255 - oldColor.green();b = 255 - oldColor.blue();newImage->setPixel(x, y, qRgb(r, g, b));}}return newImage;
}
opencv对二值图片进行反色操作(输入和输出都是二值化图片),代码如下:
bitwise_not(singleROI,singleROI);//颜色反转
3.参考资料:
数字图像处理——技术详解与Visual C++实践(左飞等著),写代码与写博客的时间相差两年,至于还参考其他的资料不,我已经忘记了,如若需要,我可以补上去