之前路标匹配[opencv完整项目详解] 传统图像算法解决路标的检测和识别 的一个改进版。 之前路标匹配存在的一个问题: 所有路标与模板的相似度都处于较高状态(基本都在50%以上),其主要原因就是虽然我们通过中间的红圈内进行相似度比较,但是红环的部分是路标的共同部分,这样增加了匹配过程中所有路标的相似度,后果会导致匹配路标和不匹配路标的相似度差异不大,导致最后判断的阈值过高,推广后的鲁棒性较差。 这路就是改进后的路标匹配相似度: 可以看出,改进后所有路标的匹配相似度都有不同程度的下降,差异更加明显,其路标的检测和识别的效果更好!!!
完整代码:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <math.h>
using namespace std;
using namespace cv;
#define PI 3.1415926
void Rgb2Hsv(float R, float G, float B, float& H, float& S, float&V)
{
float min, max, delta, tmp;
tmp = R>G ? G : R;
min = tmp>B ? B : tmp;
tmp = R>G ? R : G;
max = tmp>B ? tmp : B;
V = max;
delta = max - min;
if (max != 0)
S = delta / max;
else
{
S = 0;
H = 0;
return;
}
if (delta == 0) {
H = 0;
return;
}
else if (R == max) {
if (G >= B)
H = (G - B) / delta;
else
H = (G - B) / delta + 6.0;
}
else if (G == max)
H = 2.0 + (B - R) / delta;
else if (B == max)
H = 4.0 + (R - G) / delta;
H *= 60.0;
}
void fillHole(const Mat srcBw, Mat &dstBw)
{
Size m_Size = srcBw.size();
Mat Temp = Mat::zeros(m_Size.height + 2, m_Size.width + 2, srcBw.type());
srcBw.copyTo(Temp(Range(1, m_Size.height + 1), Range(1, m_Size.width + 1)));
cv::floodFill(Temp, Point(0, 0), Scalar(255));
Mat cutImg;
Temp(Range(1, m_Size.height + 1), Range(1, m_Size.width + 1)).copyTo(cutImg);
dstBw = srcBw | (~cutImg);
}
bool isInside(Rect rect1, Rect rect2)
{
Rect t = rect1&rect2;
if (rect1.area() > rect2.area())
{
return false;
}
else
{
if (t.area() != 0)
return true;
}
}
int main()
{
Mat srcImg = imread("./src/3.jpg");
if (srcImg.empty())
{
cout << "找不到相关图像,检查路径" << endl;
return 0;
}
int width = srcImg.cols;
int height = srcImg.rows;
if (width > 1280 || height > 720)
{
float factor = min((float)1280 / width, (float)720 / height);
cout << factor << endl;
resize(srcImg, srcImg, Size(factor*width, factor*height));
width *= factor;
height *= factor;
}
Mat matRgb = Mat::zeros(srcImg.size(), CV_8UC1);
int x, y;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
{
float B = srcImg.at<Vec3b>(y, x)[0];
float G = srcImg.at<Vec3b>(y, x)[1];
float R = srcImg.at<Vec3b>(y, x)[2];
float H, S, V;
Rgb2Hsv(R, G, B, H, S, V);
if ((H >= 135 * 2 && H <= 180 * 2 || H >= 0 && H <= 10 * 2) && S * 255 >= 16
&& S * 255 <= 255 && V >= 46 && V <= 255)
{
matRgb.at<uchar>(y, x) = 255;
}
}
imshow("hsv", matRgb);
waitKey(0);
medianBlur(matRgb, matRgb, 3);
medianBlur(matRgb, matRgb, 5);
Mat element = getStructuringElement(MORPH_ELLIPSE, Size(2 * 1 + 1, 2 * 1 + 1), Point(1, 1));
Mat element1 = getStructuringElement(MORPH_ELLIPSE, Size(2 * 3 + 1, 2 * 3 + 1), Point(3, 3));
erode(matRgb, matRgb, element);
dilate(matRgb, matRgb, element1);
imshow("dilate", matRgb);
waitKey(0);
fillHole(matRgb, matRgb);
imshow("fillHole", matRgb);
waitKey(0);
vector<vector<Point>>contours;
vector<Vec4i> hierarchy;
findContours(matRgb, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point>> contours_poly(contours.size());
vector<Rect> boundRect(contours.size());
for (int i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
boundRect[i] = boundingRect(Mat(contours_poly[i]));
}
Mat drawing = Mat::zeros(matRgb.size(), CV_8UC3);
Mat imageContours1 = Mat::zeros(matRgb.size(), CV_8UC1);
vector<Mat> vec_roi;
vector<Rect> vec_rect;
for (int i = 0; i < contours.size(); i++)
{
Rect rect = boundRect[i];
bool inside = false;
for (int j = 0; j < contours.size(); j++)
{
Rect t = boundRect[j];
if (rect == t)
continue;
else if (isInside(rect, t))
{
inside = true;
break;
}
}
if (inside)
continue;
float Area = (float)rect.width * (float)rect.height;
float dConArea = (float)contourArea(contours[i]);
float dConLen = (float)arcLength(contours[i], 1);
if (dConArea < 500)
continue;
float ratio = (float)rect.width / (float)rect.height;
if (ratio > 1.3 || ratio < 0.4)
continue;
Mat roi = srcImg(Rect(boundRect[i].tl(), boundRect[i].br()));
vec_roi.push_back(roi);
vec_rect.push_back(Rect(boundRect[i].tl(), boundRect[i].br()));
}
Mat template_srcimg = imread("./template/template.jpg");
cvtColor(template_srcimg, template_srcimg, COLOR_BGR2GRAY);
Mat gray_template, gray_roi;
for (int i = 0; i < vec_roi.size(); i++)
{
template_srcimg.copyTo(gray_template);
Mat tmp_roi = vec_roi[i].clone();
tmp_roi.resize(min(tmp_roi.rows, tmp_roi.cols), min(tmp_roi.rows, tmp_roi.cols));
cvtColor(tmp_roi, gray_roi, COLOR_BGR2GRAY);
int w = gray_template.cols, h = gray_template.rows;
resize(gray_roi, gray_roi, cv::Size(w, h));
vector<vector<bool>> enclosingcircle_flag;
Point center(0.5*w, 0.5*h);
for (int col = 0; col < w; col++)
{
vector<bool> col_flag;
for (int row = 0; row < h; row++)
{
bool flag;
if (((col - center.x)*(col - center.x) + (row - center.y)*(row - center.y)) < center.x*center.x)
flag = true;
else
flag = false;
col_flag.push_back(flag);
}
enclosingcircle_flag.push_back(col_flag);
}
cv::GaussianBlur(gray_roi, gray_roi, cv::Size(7, 7), 3, 3);
cv::GaussianBlur(gray_roi, gray_roi, cv::Size(5, 5), 3, 3);
cv::GaussianBlur(gray_template, gray_template, cv::Size(7, 7), 3, 3);
cv::GaussianBlur(gray_template, gray_template, cv::Size(5, 5), 3, 3);
int gray_mean1 = 0, gray_mean2 = 0;
for (int x = 0; x < w; x++)
for (int y = 0; y < h; y++) {
gray_mean1 += gray_roi.at<uchar>(y, x);
gray_mean2 += gray_template.at<uchar>(y, x);
}
gray_mean1 /= (w*h);
gray_mean2 /= (w*h);
threshold(gray_roi, gray_roi, gray_mean1, 255, cv::THRESH_BINARY_INV);
threshold(gray_template, gray_template, gray_mean2, 255, cv::THRESH_BINARY_INV);
imshow("gray_template.jpg", gray_template);
int w_r = 0.5*w, n_r;
if (w_r > 2)
n_r = w_r - 2;
vector<Point> vec_p;
while (n_r > 0)
{
vec_p.clear();
int sum_roi = 0,sum_template=0;
for (int col = 0; col < w; col++)
{
for (int row = 0; row < h; row++)
{
if (enclosingcircle_flag[row][col] == false)
continue;
if ((((col - center.x)*(col - center.x) + (row - center.y)*(row - center.y)) < w_r*w_r)
&& (((col - center.x)*(col - center.x) + (row - center.y)*(row - center.y)) > n_r*n_r))
{
vec_p.push_back(Point(col, row));
sum_roi += gray_roi.at<uchar>(row, col);
sum_template += gray_template.at<uchar>(row, col);
}
}
}
if (vec_p.size() == 0)
break;
int avg_roi = sum_roi / vec_p.size();
int avg_template = sum_template / vec_p.size();
if (avg_roi > 0.8 * 255 || avg_template > 0.8 * 255)
{
for (int i = 0; i < vec_p.size(); i++)
{
enclosingcircle_flag[vec_p[i].y][vec_p[i].x] = false;
}
w_r = n_r;
n_r -= 2;
}
else
break;
}
#if 0
float jiaoji = 0, bingji = 0;
for (int x = 0; x < w; x++)
for (int y = 0; y < h; y++)
{
if (enclosingcircle_flag[x][y] == false)
continue;
if (gray_roi.at<uchar>(y, x) == 255 && gray_template.at<uchar>(y, x) == 255)
jiaoji++;
if (gray_roi.at<uchar>(y, x) == 255 || gray_template.at<uchar>(y, x) == 255)
bingji++;
}
float score = jiaoji / bingji;
float score_max = score;
# else
int offset_max = 10;
float score_max = 0, jiaoji = 0, bingji = 0, score;
for (int offset = 0; offset < offset_max; offset++)
{
for (int x = 0; x < w - offset; x++)
for (int y = 0; y < h; y++)
{
if (enclosingcircle_flag[x][y] == false)
continue;
if (gray_roi.at<uchar>(y, x+offset) == 255 && gray_template.at<uchar>(y, x) == 255)
jiaoji++;
if (gray_roi.at<uchar>(y, x+offset) == 255 || gray_template.at<uchar>(y, x) == 255)
bingji++;
}
score = jiaoji / bingji;
if (score > score_max)
score_max = score;
jiaoji = 0;
bingji = 0;
for (int x = 0; x < w; x++)
for (int y = 0; y < h - offset; y++)
{
if (enclosingcircle_flag[x][y] == false)
continue;
if (gray_roi.at<uchar>(y + offset, x) == 255 && gray_template.at<uchar>(y, x) == 255)
jiaoji++;
if (gray_roi.at<uchar>(y + offset, x) == 255 || gray_template.at<uchar>(y, x) == 255)
bingji++;
}
score = jiaoji / bingji;
if (score > score_max)
score_max = score;
jiaoji = 0;
bingji = 0;
}
#endif
std::stringstream buf;
buf.precision(3);
buf.setf(std::ios::fixed);
buf << score_max;
std::string str;
str = buf.str();
putText(srcImg, str, Point(vec_rect[i].x, vec_rect[i].y), FONT_HERSHEY_PLAIN, 2, Scalar(255, 255, 0), 2);
if (score_max > 0.8)
{
rectangle(srcImg, vec_rect[i], Scalar(255, 0, 0), 4, 8, 0);
}
else
{
rectangle(srcImg, vec_rect[i], Scalar(0, 0, 255), 4, 8, 0);
}
}
imshow("result.jpg", srcImg);
waitKey(0);
system("pause");
return 0;
}
代码讲解部分的前面版本算法讲解的部分大致相同,看过的朋友可以跳过,直接看8.5,8.6部分。
算法思路:
我们从整体上将这个算法分为检测和匹配两个阶段。检测阶段就是对整个图进行搜索,找出所有类似于路标的候选框;匹配阶段就是对之前从图像中获取的候选框进行一一的匹配。匹配前先进行处理, 通过8.5节的收缩圆环策略,截取到路标中红色圆环内的区域。匹配算法在红色圆环区域内进行。 根据特定的匹配评估公式得出相似度,然后根据我们自己设定的阈值判断相似度是否达标,相似度达标则认定为为”禁止超车”标记,相似度不达标则认定为是干扰框。
检测阶段: 在这一阶段我们需要标记所有类似于路标的ROI区域,不能采用机器学习的方法,我们只能直接来寻找并且设定检测目标的特征。经过仔细观察后发现,这些路标的共同点特征就是路标的边缘一圈都是红色的圈。 因此,我们获取到两个主要特征:1.红色2.圆圈。并且我们根据这两个主要特征以及一些筛选就可以完成第一阶段的检测工作。
匹配阶段: 此时我们以获取候选的目标,并且我们的程序里存有一张搜索目标路标的模板。我们的工作就是判断我们找到的候选目标与我们模板的相似度,相似度高则可认为是匹配到了我们需要搜索的路牌,相似度低则认为这不是我们的需要搜索的路牌
代码讲解
第一步:颜色分割
首先进行颜色提取,这需要我们先进行RGB转HSV,然后再筛选出红色范围内的区域.
颜色范围(该图H取值范围[0,180],S取值范围[0,255], V取值范围[0,255]) 而我们代码中的范围:H取值范围[0,360],S取值范围[0,1],V取值范围[0,1]。 RGB转HSV的代码:
void Rgb2Hsv(float R, float G, float B, float& H, float& S, float&V)
{
float min, max, delta, tmp;
tmp = R>G ? G : R;
min = tmp>B ? B : tmp;
tmp = R>G ? R : G;
max = tmp>B ? tmp : B;
V = max;
delta = max - min;
if (max != 0)
S = delta / max;
else
{
S = 0;
H = 0;
return;
}
if (delta == 0) {
H = 0;
return;
}
else if (R == max) {
if (G >= B)
H = (G - B) / delta;
else
H = (G - B) / delta + 6.0;
}
else if (G == max)
H = 2.0 + (B - R) / delta;
else if (B == max)
H = 4.0 + (R - G) / delta;
H *= 60.0;
}
调用代码: 注,由于HSV取值范围的不同,映射对照表时,需要进行数值转换. 比如 H需要除以2,S需要乘以255,V需要乘以255.
Mat matRgb = Mat::zeros(srcImg.size(), CV_8UC1);
int x, y;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
{
float B = srcImg.at<Vec3b>(y, x)[0];
float G = srcImg.at<Vec3b>(y, x)[1];
float R = srcImg.at<Vec3b>(y, x)[2];
float H, S, V;
Rgb2Hsv(R, G, B, H, S, V);
if ((H >= 135 * 2 && H <= 180 * 2 || H >= 0 && H <= 10 * 2) && S * 255 >= 16
&& S * 255 <= 255 && V >= 46 && V <= 255)
{
matRgb.at<uchar>(y, x) = 255;
}
}
imshow("hsv", matRgb);
waitKey(0);
第二步:去噪
先进行两次中值滤波,再进行一个开运算.
medianBlur(matRgb, matRgb, 3);
medianBlur(matRgb, matRgb, 5);
Mat element = getStructuringElement(MORPH_ELLIPSE, Size(2 * 1 + 1, 2 * 1 + 1), Point(1, 1));
Mat element1 = getStructuringElement(MORPH_ELLIPSE, Size(2 * 3 + 1, 2 * 3 + 1), Point(3, 3));
erode(matRgb, matRgb, element);
dilate(matRgb, matRgb, element1);
效果:
第三步:填充
函数定义:
void fillHole(const Mat srcBw, Mat &dstBw)
{
Size m_Size = srcBw.size();
Mat Temp = Mat::zeros(m_Size.height + 2, m_Size.width + 2, srcBw.type());
srcBw.copyTo(Temp(Range(1, m_Size.height + 1), Range(1, m_Size.width + 1)));
cv::floodFill(Temp, Point(0, 0), Scalar(255));
Mat cutImg;
Temp(Range(1, m_Size.height + 1), Range(1, m_Size.width + 1)).copyTo(cutImg);
dstBw = srcBw | (~cutImg);
}
函数调用:
fillHole(matRgb, matRgb);
效果:
第四步:找轮廓
findContours常规操作:
vector<vector<Point>>contours;
vector<Vec4i> hierarchy;
findContours(matRgb, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point>> contours_poly(contours.size());
vector<Rect> boundRect(contours.size());
第五步:找轮廓的最小外接矩形
for (int i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
boundRect[i] = boundingRect(Mat(contours_poly[i]));
}
第六步:筛选轮廓
筛选轮廓,主要排除面积过小的,交叉的等等:
Mat drawing = Mat::zeros(matRgb.size(), CV_8UC3);
Mat imageContours1 = Mat::zeros(matRgb.size(), CV_8UC1);
vector<Mat> vec_roi;
vector<Rect> vec_rect;
for (int i = 0; i < contours.size(); i++)
{
Rect rect = boundRect[i];
bool inside = false;
for (int j = 0; j < contours.size(); j++)
{
Rect t = boundRect[j];
if (rect == t)
continue;
else if (isInside(rect, t))
{
inside = true;
break;
}
}
if (inside)
continue;
float Area = (float)rect.width * (float)rect.height;
float dConArea = (float)contourArea(contours[i]);
float dConLen = (float)arcLength(contours[i], 1);
if (dConArea < 500)
continue;
float ratio = (float)rect.width / (float)rect.height;
if (ratio > 1.3 || ratio < 0.4)
continue;
Mat roi = srcImg(Rect(boundRect[i].tl(), boundRect[i].br()));
vec_roi.push_back(roi);
vec_rect.push_back(Rect(boundRect[i].tl(), boundRect[i].br()));
}
第七步:载入模板
载入模板图,并设置为单通道:
Mat template_srcimg = imread("./template/template.jpg");
cvtColor(template_srcimg, template_srcimg, COLOR_BGR2GRAY);
第八步:相似度匹配
我们需要遍历所有的轮廓vec_roi .
Mat gray_template, gray_roi;
for (int i = 0; i < vec_roi.size(); i++)
{
''''''
}
以下,是一次迭代中的操作步骤:
8.1 轮廓与模板统一尺寸
先将该轮廓tmp_roi resize为方形,然后再resize与模板尺寸一致.
template_srcimg.copyTo(gray_template);
Mat tmp_roi = vec_roi[i].clone();
tmp_roi.resize(min(tmp_roi.rows, tmp_roi.cols), min(tmp_roi.rows, tmp_roi.cols));
cvtColor(tmp_roi, gray_roi, COLOR_BGR2GRAY);
int w = gray_template.cols, h = gray_template.rows;
resize(gray_roi, gray_roi, cv::Size(w, h));
8.2 标记最大内接圆
这一步的意思就是相似度匹配只对最大内接圆内的像素点进行判断,不对最大内接圆之外的像素点进行判断,因为我们认定的路标都是圆形的. 我当时想不起来怎么再抠成一个圆形的ROI,就想到这个很好理解看起来又很蠢的方法,设置一个和图像尺寸一致的bool二维数组,分别标志对应的像素点是否是最大内接圆内.
vector<vector<bool>> enclosingcircle_flag;
Point center(0.5*w, 0.5*h);
for (int col = 0; col < w; col++)
{
vector<bool> col_flag;
for (int row = 0; row < h; row++)
{
bool flag;
if (((col - center.x)*(col - center.x) + (row - center.y)*(row - center.y)) < center.x*center.x)
flag = true;
else
flag = false;
col_flag.push_back(flag);
}
enclosingcircle_flag.push_back(col_flag);
}
8.3 高斯滤波优化
同时对模板和目标的roi图进行高斯滤波,当时的想法就是进行一定的模糊,然后能够淡化一下细节上的差异.最终的实验也证明,进行高斯滤波后,得分提高了(在其他外景图中,得分会有较大提升).
cv::GaussianBlur(gray_roi, gray_roi, cv::Size(7, 7), 3, 3);
cv::GaussianBlur(gray_roi, gray_roi, cv::Size(5, 5), 3, 3);
cv::GaussianBlur(gray_template, gray_template, cv::Size(7, 7), 3, 3);
cv::GaussianBlur(gray_template, gray_template, cv::Size(5, 5), 3, 3);
8.4 对两个图同时进行二值化
由于阈值不好确定,目前将灰度值的均值作为二值化的阈值. 注:在外景很容易失效,二值化这个操作比较适合于光照,背景都比较稳定的,比如机器视觉,黑盒子搞个工业光源,流水线产品检测,效果不错.
其实这个二值化对算法的泛性影响不好,测试其他算法出问题多半就是这个二值化的阈值选的不够好.
int gray_mean1 = 0, gray_mean2 = 0;
for (int x = 0; x < w; x++)
for (int y = 0; y < h; y++) {
gray_mean1 += gray_roi.at<uchar>(y, x);
gray_mean2 += gray_template.at<uchar>(y, x);
}
gray_mean1 /= (w*h);
gray_mean2 /= (w*h);
threshold(gray_roi, gray_roi, gray_mean1, 255, cv::THRESH_BINARY_INV);
threshold(gray_template, gray_template, gray_mean2, 255, cv::THRESH_BINARY_INV);
imshow("gray_template.jpg", gray_template);
8.5 【改进部分】去除圆环部分
核心思想:寻找红色圆环内的最大圆。通过观察二值化路标可以看到,圆环内为白色,红色圆环内边缘多为黑色。 最大外接圆像素一定是白色,我们通过不断地收缩最大外接圆,然后判断这一圈的圆环内像素点的阈值均值,如果依旧在红色圆环中,那么我们计算的圆环的灰度值均值一定是非常高的,还需要继续网往圆心内慢慢收缩。当某一次迭代后,这一圈的圆环内像素点的普遍降低非常厉害,说明我们收缩的圆环到达了红色圆环内边缘。
去除圆环的工作针对模板图和目标图同时进行。
w_r:表示我们收缩的圆环外圈半径,初始值就是图表的最大外接圆半径 n_r :表示我们收缩的圆环内圈半径 w_r - n_r:表示我们收缩的圆环的宽度。本代码中始终设置为2.若宽度设置越小,我们的迭代次数就会越多,计算越慢,但最后的定位越精确。宽度设置过大,每次缩进程度就会加大,减少迭代次数,但是圆环的范围扩大,最后红色圆环内边缘的定位就会越模糊。
int w_r = 0.5*w, n_r;
if (w_r > 2)
n_r = w_r - 2;
vector<Point> vec_p;
while (n_r > 0)
{
vec_p.clear();
int sum_roi = 0,sum_template=0;
for (int col = 0; col < w; col++)
{
for (int row = 0; row < h; row++)
{
if (enclosingcircle_flag[row][col] == false)
continue;
if ((((col - center.x)*(col - center.x) + (row - center.y)*(row - center.y)) < w_r*w_r)
&& (((col - center.x)*(col - center.x) + (row - center.y)*(row - center.y)) > n_r*n_r))
{
vec_p.push_back(Point(col, row));
sum_roi += gray_roi.at<uchar>(row, col);
sum_template += gray_template.at<uchar>(row, col);
}
}
}
if (vec_p.size() == 0)
break;
int avg_roi = sum_roi / vec_p.size();
int avg_template = sum_template / vec_p.size();
if (avg_roi > 0.8 * 255 || avg_template > 0.8 * 255)
{
for (int i = 0; i < vec_p.size(); i++)
{
enclosingcircle_flag[vec_p[i].y][vec_p[i].x] = false;
}
w_r = n_r;
n_r -= 2;
}
else
break;
}
8.6 相似度计算,相似度判断
int offset_max = 10;
float score_max = 0, jiaoji = 0, bingji = 0, score;
for (int offset = 0; offset < offset_max; offset++)
{
for (int x = 0; x < w - offset; x++)
for (int y = 0; y < h; y++)
{
if (enclosingcircle_flag[x][y] == false)
continue;
if (gray_roi.at<uchar>(y, x+offset) == 255 && gray_template.at<uchar>(y, x) == 255)
jiaoji++;
if (gray_roi.at<uchar>(y, x+offset) == 255 || gray_template.at<uchar>(y, x) == 255)
bingji++;
}
score = jiaoji / bingji;
if (score > score_max)
score_max = score;
jiaoji = 0;
bingji = 0;
for (int x = 0; x < w; x++)
for (int y = 0; y < h - offset; y++)
{
if (enclosingcircle_flag[x][y] == false)
continue;
if (gray_roi.at<uchar>(y + offset, x) == 255 && gray_template.at<uchar>(y, x) == 255)
jiaoji++;
if (gray_roi.at<uchar>(y + offset, x) == 255 || gray_template.at<uchar>(y, x) == 255)
bingji++;
}
score = jiaoji / bingji;
if (score > score_max)
score_max = score;
jiaoji = 0;
bingji = 0;
}
std::stringstream buf;
buf.precision(3);
buf.setf(std::ios::fixed);
buf << score_max;
std::string str;
str = buf.str();
putText(srcImg, str, Point(vec_rect[i].x, vec_rect[i].y), FONT_HERSHEY_PLAIN, 2, Scalar(255, 255, 0), 2);
if (score_max > 0.8)
{
rectangle(srcImg, vec_rect[i], Scalar(255, 0, 0), 4, 8, 0);
}
else
{
rectangle(srcImg, vec_rect[i], Scalar(0, 0, 255), 4, 8, 0);
}
}
imshow("result.jpg", srcImg);
waitKey(0);
结束,最后效果:
|