效果
原理
使用opencv中颜色替换
核心代码
void ReplaceColorWidget::replaceColor(Mat &t_srcMat, QList<QColor> &t_colorReplace, QList<QColor> &t_colorSrc)
{
// qDebug() << t_colorReplace.size() << t_colorSrc.size();
if (t_srcMat.channels() != 4) {
qDebug() << "channels() != 4";
return;
}
if ( (t_colorSrc.isEmpty() && t_colorReplace.size() == 1) ||
(!t_colorSrc.isEmpty() && t_colorReplace.size() == t_colorSrc.size())) {
// qDebug() << "ok";
uchar* pData = t_srcMat.data;
MatStep mst = t_srcMat.step;
for (int row = 0; row < t_srcMat.rows; row++) {
for (int col = 0; col < t_srcMat.cols; col++) {
auto pMatB = pData + row * mst[0] + col * mst[1] + 0;
auto pMatG = pData + row * mst[0] + col * mst[1] + 1;
auto pMatR = pData + row * mst[0] + col * mst[1] + 2;
auto pMatA = pData + row * mst[0] + col * mst[1] + 3;
if(*pMatA == 0) {
continue;
}
if(t_colorSrc.isEmpty()) {
// 全部替换
*pMatB = t_colorReplace.at(0).blue();
*pMatG = t_colorReplace.at(0).green();
*pMatR = t_colorReplace.at(0).red();
// *pMatA = t_colorReplace.at(0).alpha();
} else {
// 部分替换
QColor matColor(*pMatR, *pMatG, *pMatB, *pMatA);
for(int i=0; i<t_colorSrc.size(); i++) {
if (compareColor(matColor, t_colorSrc.at(i), m_spinBoxRange.value())) {
*pMatB = t_colorReplace.at(i).blue();
*pMatG = t_colorReplace.at(i).green();
*pMatR = t_colorReplace.at(i).red();
// *pMatA = t_colorReplace.at(i).alpha();
}
}
}
}
}
}
}
软件及代码下载链接: https://pan.baidu.com/s/10vTgogVPVYpq8xW-Yb405Q 提取码: r9wx