其他分享
首页 > 其他分享> > redandgreen

redandgreen

作者:互联网

if (frame.empty())
    break;
frame1 = frame.clone();
dst = Mat::zeros(frame.size(), frame.type());
mask = Mat::zeros(frame.size(), CV_8U);
cvtColor(frame, gray, COLOR_BGR2GRAY);
vector<Vec3f> circles;
HoughCircles(gray, circles, HOUGH_GRADIENT, 1, 10, 40, 40, 10, 25);![图片说明](https://www.icode9.com/i/ll/?i=img_convert/3380d90e268756fa3eb6afff613c71e6.png)

for (size_t i = 0; i < circles.size(); i++)
{
    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    circle(mask, center, radius, Scalar(255), -1); //roi
    frame1.copyTo(dst, mask);
    //绘制圆心
    circle(frame, center, 3, Scalar(0, 255, 0), -1, 8, 0);
    //绘制圆的轮廓
    circle(frame, center, radius, Scalar(155, 50, 255), 3, 8, 0);
    circle(mask, center, radius, Scalar(255), -1);
}

if(detectColor(dst)==1)
putTextZH(frame, "红灯", Point(200, 150), Scalar(255, 0, 255), 50, "楷体");
else if(detectColor(dst)==2)
    putTextZH(frame, "绿灯", Point(200, 150), Scalar(255, 0, 255), 50, "楷体");
else //if(detectColor(dst)==3)
    putTextZH(frame, "黄灯", Point(200, 150), Scalar(255, 0, 255), 50, "楷体");
imshow("vedio", frame);
if (waitKey(100) == 27)
    break;

}
cap.release();
destroyAllWindows();
return 0;
}
int detectColor(Mat image)
{
Mat mask;
cvtColor(image, image, COLOR_BGR2HSV);
const Scalar hsvRedLo(10, 193, 198);
const Scalar hsvRedHi(28, 220,206);

const Scalar hsvGreenLo(72, 242, 186);
const Scalar hsvGreenHi(90, 255, 255);

const Scalar hsvYellowLo(24, 219, 196);
const Scalar hsvYellowHi(32, 255, 217);

vector hsvLo{ hsvRedLo,hsvGreenLo,hsvYellowLo };
vector hsvHi{ hsvRedHi,hsvGreenHi,hsvYellowHi };

inRange(image, hsvLo[1], hsvHi[1], mask);
inRange(image, hsvLo[2], hsvHi[2], mask);
inRange(image, hsvLo[0], hsvHi[0], mask);

Mat redBlur, greenBlur, yellowBlur;
medianBlur(mask, redBlur, 5);
medianBlur(mask, greenBlur, 5);
medianBlur(mask, yellowBlur, 5);
int red, green, yellow, lightColor;
red = countNonZero(redBlur);
green = countNonZero(greenBlur);
yellow = countNonZero(yellowBlur);
lightColor = (red > green) ? (red > yellow ? red : yellow) : (green > yellow ? green : yellow);
if (lightColor == red)
return 1;
else if (lightColor == green)
return 2;
else if (lightColor == yellow)
return 3;
else return 0;
}

标签:const,frame,mask,yellow,Scalar,redandgreen,255
来源: https://blog.csdn.net/qq_41911261/article/details/120151982