好的,这里是更新后的代码,将所有 inRange
函数的第一个变量替换为 ZoomOutimage
:
cpp
// 绿色分岔路
if (divergerColor == "green" && nextColor == "null")
{
cv::Mat frameGreen, frameRed;
frame2.copyTo(frameGreen); // 复制原始图像用于绿色二值化处理
frame2.copyTo(frameRed); // 复制原始图像用于红色二值化处理
// 二值化绿色
inRange(ZoomOutimage, colorgroup.greenMin, colorgroup.greenMax, frameGreen); // 二值化绿色
int numberGreen = 0;
for (int i = 0; i < frameGreen.rows; i++)
{
for (int j = 0; j < frameGreen.cols; j++)
{
if (frameGreen.at<uchar>(i, j) == 255)
{
numberGreen++;
}
}
}
// 二值化红色
inRange(ZoomOutimage, colorgroup.redMin, colorgroup.redMax, frameRed); // 二值化红色
int numberRed = 0;
for (int i = 0; i < frameRed.rows; i++)
{
for (int j = 0; j < frameRed.cols; j++)
{
if (frameRed.at<uchar>(i, j) == 255)
{
numberRed++;
}
}
}
// 出现绿色条带
if (numberGreen >= 500)
{
mythread.mode = divergeleft;
cout << "recognize green divergeleft" << endl;
divergerColor = "null";
frame2 = frameGreen;
return;
}
// 出现红色条带
else if (numberRed >= 500)
{
mythread.mode = divergeleft;
cout << "recognize red divergeleft" << endl;
divergerColor = "null";
frame2 = frameRed;
return;
}
}
// 红色分岔路
else if (divergerColor == "red" && nextColor == "null")
{
cv::Mat frameGreen, frameRed;
frame2.copyTo(frameGreen); // 复制原始图像用于绿色二值化处理
frame2.copyTo(frameRed); // 复制原始图像用于红色二值化处理
// 二值化绿色
inRange(ZoomOutimage, colorgroup.greenMin, colorgroup.greenMax, frameGreen); // 二值化绿色
int numberGreen = 0;
for (int i = 0; i < frameGreen.rows; i++)
{
for (int j = 0; j < frameGreen.cols; j++)
{
if (frameGreen.at<uchar>(i, j) == 255)
{
numberGreen++;
}
}
}
// 二值化红色
inRange(ZoomOutimage, colorgroup.redMin, colorgroup.redMax, frameRed); // 二值化红色
int numberRed = 0;
for (int i = 0; i < frameRed.rows; i++)
{
for (int j = 0; j < frameRed.cols; j++)
{
if (frameRed.at<uchar>(i, j) == 255)
{
numberRed++;
}
}
}
// 出现绿色条带
if (numberGreen >= 500)
{
mythread.mode = divergeright;
cout << "recognize green divergeright" << endl;
divergerColor = "null";
frame2 = frameGreen;
return;
}
// 出现红色条带
else if (numberRed >= 500)
{
mythread.mode = divergeright;
cout << "recognize red divergeright" << endl;
divergerColor = "null";
frame2 = frameRed;
return;
}
}
// 棕色住户识别
else if (residenceColor == "brown" && divergerColor == "null")
{
// 复制原始图像用于二值化处理
cv::Mat frameBrown, frameViolet;
frame2.copyTo(frameBrown);
frame2.copyTo(frameViolet);
inRange(ZoomOutimage, colorgroup.brownMin, colorgroup.brownMax, frameBrown); // 二值化棕色
int numberBrown = 0;
for (int i = 0; i < frameBrown.rows; i++)
{
for (int j = 0; j < frameBrown.cols; j++)
{
if (frameBrown.at<uchar>(i, j) == 255)
{
numberBrown++;
}
}
}
inRange(ZoomOutimage, colorgroup.violetMin, colorgroup.violetMax, frameViolet); // 二值化紫色
int numberViolet = 0;
for (int i = 0; i < frameViolet.rows; i++)
{
for (int j = 0; j < frameViolet.cols; j++)
{
if (frameViolet.at<uchar>(i, j) == 255)
{
numberViolet++;
}
}
}
// 出现红色或棕色条带
if (numberBrown >= 500)
{
mythread.mode = residenceright;
cout << "recognize brown" << endl;
residenceColor = "null";
frame2 = frameBrown;
return;
}
else if (numberViolet >= 500)
{
mythread.mode = residenceright;
cout << "recognize Violet" << endl;
residenceColor = "null";
frame2 = frameViolet;
return;
}
}
// 紫色住户识别
else if (residenceColor == "violet" && divergerColor == "null")
{
// 复制原始图像用于二值化处理
cv::Mat frameBrown, frameViolet;
frame2.copyTo(frameBrown);
frame2.copyTo(frameViolet);
inRange(ZoomOutimage, colorgroup.brownMin, colorgroup.brownMax, frameBrown); // 二值化棕色
int numberBrown = 0;
for (int i = 0; i < frameBrown.rows; i++)
{
for (int j = 0; j < frameBrown.cols; j++)
{
if (frameBrown.at<uchar>(i, j) == 255)
{
numberBrown++;
}
}
}
inRange(ZoomOutimage, colorgroup.violetMin, colorgroup.violetMax, frameViolet); // 二值化紫色
int numberViolet = 0;
for (int i = 0; i < frameViolet.rows; i++)
{
for (int j = 0; j < frameViolet.cols; j++)
{
if (frameViolet.at<uchar>(i, j) == 255)
{
numberViolet++;
}
}
}
// 出现红色或棕色条带
if (numberBrown >= 500)
{
mythread.mode = residenceleft;
cout << "recognize brown" << endl;
residenceColor = "null";
frame2 = frameBrown;
return;
}
else if (numberViolet >= 500)
{
mythread.mode = residenceleft;
cout << "recognize Violet" << endl;
residenceColor = "null";
frame2 = frameViolet;
return;
}
}
在上述代码中,所有 inRange
函数的第一个参数都已经替换为 ZoomOutimage
。这样确保了所有二值化处理都是在 ZoomOutimage
上进行的。