栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

opencv识别绿灯

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

opencv识别绿灯

#include
#include
#include
#include
using namespace cv;
using namespace std;
void Pickupgreen(Mat& inputframe, Mat& outputframe);
int main( )
{
//打开摄像头
VideoCapture capture(0);
while (1)

{
                Mat frame;
                Mat greenframe;
capture >> frame;
Pickupgreen(frame, greenframe);
imshow("视频", frame);
//【1】Mat变量定义   
Mat midImage;//目标图的定义

//【2】转为灰度图并进行图像平滑
cvtColor(greenframe,midImage, COLOR_BGR2GRAY);//转化边缘检测后的图为灰度图
GaussianBlur( midImage, midImage, Size(9, 9), 0, 0 );

//【3】进行霍夫圆变换
vector circles;
HoughCircles( midImage, circles, HOUGH_GRADIENT,1.5,10,200,100,0,0 );

//【4】依次在图中绘制出圆
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( greenframe, center, 3, Scalar(0,255,0), -1, 8, 0 );
//绘制圆轮廓
circle( greenframe, center, radius, Scalar(155,50,255), 3, 8, 0 );
}

//【5】显示效果图  
namedWindow("效果");
imshow("效果", greenframe);
waitKey(30);
}
}
void Pickupgreen(Mat& inputframe, Mat& outputframe)

{
Mat hsvframe;
cvtColor(inputframe, hsvframe, COLOR_BGR2HSV);
outputframe = Mat(hsvframe.rows, hsvframe.cols, CV_8UC3, cv::Scalar(255, 255, 255));
int rowNumber = hsvframe.rows;
int colNumber = hsvframe.cols;
double H = 0.0, S = 0.0, V = 0.0;
for (int i = 0; i < rowNumber; i++)
{
for (int j = 0; j < colNumber; j++)
{
H = hsvframe.at(i, j)[0];
S = hsvframe.at(i, j)[1];
V = hsvframe.at(i, j)[2];
if ((H >= 35 && H <= 99) && S >= 43 && V >= 46)
{
outputframe.at(i, j)[0] = inputframe.at(i, j)[0];
outputframe.at(i, j)[1] = inputframe.at(i, j)[1];
outputframe.at(i, j)[2] = inputframe.at(i, j)[2];
}
}
}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/715405.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号