YOLOv3 红绿灯识别

6 篇文章 3 订阅
5 篇文章 0 订阅

YOLOv3 红绿灯识别

方法

修改 image.c文件 draw_detections函数,在检出分类 if(class >= 0) 里面添加识别代码,查看data目录coco.names文件,其中traffic light是序号10,所以即当class==9的时候进行红绿灯识别即可,具体opencv代码如下:

          if(bot > im.h-1) 
				bot = im.h-1;
     			
			/*************new add 2018-10-25***************/
			if(class == 9)
			{
				printf("%d %d %d %d\n", left,right, top, bot);
				img = creat_image(im);
				rect = cvRect(left, top, right-left, bot-top);
				roiImg = cutImage(img,rect);
		
				IplImage* hsvImg =  cvCreateImage(cvGetSize(roiImg),roiImg->depth,roiImg->nChannels);
				IplImage *threshold = cvCreateImage(cvGetSize(roiImg),IPL_DEPTH_8U,1);  
			
				cvCvtColor(roiImg, hsvImg, CV_BGR2HSV);

				cvInRangeS(hsvImg, hsvRedLo , hsvRedHi, threshold);
				cvThreshold(threshold, threshold, 0, 255, CV_THRESH_BINARY);
				cvSmooth(threshold, threshold, CV_MEDIAN, 5,5,0,0);				// 0x77 red
				r_value = cvCountNonZero(threshold);

				cvInRangeS(hsvImg, hsvGreenLo , hsvGreenHi, threshold);
				cvThreshold(threshold, threshold, 0, 255, CV_THRESH_BINARY);
				cvSmooth(threshold, threshold, CV_MEDIAN, 5,5,0,0);
				g_value = cvCountNonZero(threshold);

				if(r_value > 250 || g_value > 250)
				{
					if((msg = realloc(msg,7+msg_len)) != NULL)				// bin value, left, top, color 2019-4-21
					{
						if(r_value > g_value)
						{
							msg[msg_len+6] = 0x77;
							printf("Red light[%d]!\n",r_value);
						}
						else
						{
							msg[msg_len+6] = 0x66;
							printf("Green light[%d]!\n",g_value);
						}
						msg[msg_len+0] = (r_value&0x3F80)>>7;
						msg[msg_len+1] = (r_value&0x7F);
						msg[msg_len+2] = (left&0x3F80)>>7;
						msg[msg_len+3] = (left&0x7F);
						msg[msg_len+4] = (top&0x3F80)>>7;
						msg[msg_len+5] = (top&0x7F);
						msg_len += 7;
					}
					cvShowImage("test",threshold);
				}
				//	cvSaveImage("over.jpg", threshold, 0);
				cvReleaseImage(&hsvImg);
				cvReleaseImage(&img);
				cvReleaseImage(&roiImg);
				cvReleaseImage(&threshold);
			}
			/********************************************************/
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值