Skip to content

Commit

Permalink
增加第二层滤波,修改之前的滤波bug(之前的if判断漏掉一些情况)
Browse files Browse the repository at this point in the history
如果这一帧检测到信号灯,如果下面10帧中有未检测到信号灯,则计数器加1,若计数器值大于阈值,认为信号灯不存在了,反之则认为存在,同时,如果检测到红灯,计数器清零
  • Loading branch information
JayYangSS committed Nov 14, 2015
1 parent 1a1948a commit 7179d22
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 20 deletions.
22 changes: 13 additions & 9 deletions Main_TrafficSignRecognition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ CvANN_MLP nnetwork,nnetwork_RoundRim,nnetwork_RectBlue;//neural networks for thr
PCA pca,pca_RoundRim,pca_RectBlue;
deque<float> signFilters[5];
deque<float> TLFilters[3];
int TLCount[3]={0,0,0};//用来进行结果的稳定
int TLCountThreshold=10;//如果丢失10帧以上,则认为没有检测到了

//test function
void testCamera(PCA &pca,PCA &pca_RoundRim,PCA &pca_RectBlue,CvANN_MLP &nnetwork,
Expand Down Expand Up @@ -198,13 +200,15 @@ void TLDetectionPerFrame(IplImage *frame,float *TLDSend)
IplConvKernel *t=cvCreateStructuringElementEx(7,7,3,3,CV_SHAPE_ELLIPSE);
cvMorphologyEx(imageSeg,closeImg,NULL,t,CV_MOP_CLOSE);
//cvShowImage("closeImg",closeImg);
//imageNoiseRem=noiseRemoval(imageSeg);
//cvWaitKey(5);

#if ISDEBUG_TL
//imageNoiseRem=noiseRemoval(imageSeg);
cvNamedWindow("imgseg");
cvShowImage("imgseg",imageSeg);
cvWaitKey(5);
cvShowImage("imageNoiseRem",imageNoiseRem);
cvWaitKey(5);
//cvShowImage("imageNoiseRem",imageNoiseRem);
//cvWaitKey(5);
#endif
//componentExtraction(imageNoiseRem,resize_TLR,TLDSend,found_TL);
componentExtractionTL(closeImg,resize_TLR,TLDSend);
Expand Down Expand Up @@ -653,8 +657,8 @@ void openMP_MultiThreadVideo()
bool saveFlag=false;
IplImage * frame,*copyFrame;
float connectResult[8]={0,0,0,0,0,0,0,0};
CvCapture * cap=cvCreateFileCapture("D:\\JY\\JY_TrainingSamples\\changshu data\\TL\\RedFalsePositive.avi");
//CvCapture * cap=cvCreateFileCapture("D:\\JY\\JY_TrainingSamples\\2014.11.16\\1_clip.mp4");
//CvCapture * cap=cvCreateFileCapture("D:\\JY\\JY_TrainingSamples\\changshu data\\TL\\RedFalsePositive.avi");
CvCapture * cap=cvCreateFileCapture("CamraVideo\\Video_20151114103311.avi");
float startTime=1000*(float)getTickCount()/getTickFrequency();
CvVideoWriter * writer=NULL;
//findColorRange();
Expand All @@ -664,7 +668,7 @@ void openMP_MultiThreadVideo()
SYSTEMTIME stTime;
GetLocalTime(&stTime);
char *videoPath = new char[100];// "D:/123.dat";
sprintf(videoPath, "ResultVideo//Video_%04d%02d%02d%02d%02d%02d.avi", stTime.wYear, stTime.wMonth, stTime.wDay, stTime.wHour, stTime.wMinute, stTime.wSecond);
sprintf(videoPath, "ResultVideo//Video_%04d.%02d.%02d_%02d.%02d.%02d.avi", stTime.wYear, stTime.wMonth, stTime.wDay, stTime.wHour, stTime.wMinute, stTime.wSecond);
writer=cvCreateVideoWriter(videoPath,CV_FOURCC('X', 'V', 'I', 'D'), 20, cvGetSize(resize_TLR), 1);
}

Expand Down Expand Up @@ -710,9 +714,9 @@ void openMP_MultiThreadVideo()
cvShowImage("TL",resize_TLR);
cvWaitKey(5);
//show the detection result of TSR
namedWindow("TSR");
imshow("TSR",re_src);
waitKey(5);
//namedWindow("TSR");
//imshow("TSR",re_src);
//waitKey(5);
#endif
if (saveFlag)
{
Expand Down
4 changes: 2 additions & 2 deletions TrafficLightDetection/RectangleDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ bool isLighInBox(Mat src)
#if ISDEBUG_TL
ofstream outfile;
outfile.open(debugTLPath,ios::app);//ios::app: 以追加的方式打开文件
outfile<<"===black ratio===:"<<ratio<<endl;//输出到调试文件中
cout<<"===black ratio===:"<<ratio<<endl;//输出到控制台
outfile<<"===black VerticalBlackRatio===:"<<VerticalBlackRatio<<endl;//输出到调试文件中
cout<<"===black VerticalBlackRatio===:"<<VerticalBlackRatio<<endl;//输出到控制台
outfile.close();
#endif

Expand Down
148 changes: 139 additions & 9 deletions TrafficLightDetection/componentExtrationTL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define RECT_FILTER 1

extern deque<float> TLFilters[3];
extern int TLCount[3];
extern int TLCountThreshold;

//识别nhls图像中矩形框内的颜色
int RecColor(Mat img)
Expand Down Expand Up @@ -85,7 +87,7 @@ void componentExtractionTL(IplImage* inputImage,IplImage* srcImage,float* TLDSen
int containCount=0;//计算容器中有效检测结果数目

//检测结果滤波
if(p1==0&&p2==0)//未检测到信号灯
/* if(p1==0&&p2==0)//未检测到信号灯
{
for (int i=0;i<3;i++)
{
Expand All @@ -105,12 +107,33 @@ void componentExtractionTL(IplImage* inputImage,IplImage* srcImage,float* TLDSen
if (i==0)
cout<<"TSContainCount:"<<TSContainCount<<endl;
#endif
if((float)(TSContainCount)/(float)TLFilters[i].size()>=0.4)
if((float)(TSContainCount)/(float)TLFilters[i].size()>=0.39)
{
TLDSend[i]=1.0;
TLCount[i]=0;//如果检测到,则计数器清零
}
else
TLDSend[i]=0;
{
//TLDSend[i]=0;
if(TLCount[i]<TLCountThreshold+5)
{
TLCount[i]=TLCount[i]+1;//如果未检测到,则计数器加1
}
if (TLCount[i]>TLCountThreshold)//如果丢失帧数大于阈值,则认为没有信号灯
{
TLDSend[i]=0;
}else
{//如果丢失帧数在阈值范围内,则认为还有信号灯
TLDSend[i]=1.0;
}
}
}
}else{
}*/

// else{
if (p1==1)//前行禁止灯
{
TLFilters[0].push_back(1.0);//red
Expand All @@ -123,13 +146,66 @@ void componentExtractionTL(IplImage* inputImage,IplImage* srcImage,float* TLDSen
if(*it==1.0)containCount++;
it++;
}
if ((float)(containCount)/(float)TLFilters[0].size()>=0.4)
if ((float)(containCount)/(float)TLFilters[0].size()>=0.39)
{
TLDSend[0]=1.0;//表示检测到红灯
TLCount[0]=0;//如果检测到,则计数器清零
}
else
TLDSend[0]=0;
{
if(TLCount[0]<TLCountThreshold+5)//防止增加的过大溢出
{
TLCount[0]=TLCount[0]+1;//如果未检测到,则计数器加1
}

if (TLCount[0]>TLCountThreshold)//如果丢失帧数大于阈值,则认为没有信号灯
{
TLDSend[0]=0;
}else
{//如果丢失帧数在阈值范围内,则认为还有信号灯
TLDSend[0]=1.0;
}
}
containCount=0;
}else{
TLFilters[0].push_back(0);
if (TLFilters[0].size()>5)
TLFilters[0].pop_front();

deque<float>::iterator it;
int TSContainCount=0;
it=TLFilters[0].begin();
while(it<TLFilters[0].end())
{
if (*it==1.0)TSContainCount++;
it++;
}

if((float)(TSContainCount)/(float)TLFilters[0].size()>=0.39)
{
TLDSend[0]=1.0;
TLCount[0]=0;//如果检测到,则计数器清零
}

else
{
//TLDSend[0]=0;
if(TLCount[0]<TLCountThreshold+5)
{
TLCount[0]=TLCount[0]+1;//如果未检测到,则计数器加1
}

if (TLCount[0]>TLCountThreshold)//如果丢失帧数大于阈值,则认为没有信号灯
{
TLDSend[0]=0;
}else
{//如果丢失帧数在阈值范围内,则认为还有信号灯
TLDSend[0]=1.0;
}
}
}


if(p2==1)//左转禁止
{
TLFilters[1].push_back(1.0);//green
Expand All @@ -143,11 +219,65 @@ void componentExtractionTL(IplImage* inputImage,IplImage* srcImage,float* TLDSen
it++;
}
if ((float)(containCount)/(float)TLFilters[1].size()>=0.4)
{
TLCount[1]=0;//如果检测到,则计数器清零
TLDSend[1]=1.0;
}

else
TLDSend[1]=0;
{
if(TLCount[1]<TLCountThreshold+5)//防止增加的过大溢出
{
TLCount[1]=TLCount[1]+1;//如果未检测到,则计数器加1
}

if (TLCount[1]>TLCountThreshold)//如果丢失帧数大于阈值,则认为没有信号灯
{
TLDSend[1]=0;
}else
{//如果丢失帧数在阈值范围内,则认为还有信号灯
TLDSend[1]=1.0;
}
}
containCount=0;
}else{
TLFilters[1].push_back(0);
if (TLFilters[1].size()>5)
TLFilters[1].pop_front();

deque<float>::iterator it;
int TSContainCount=0;
it=TLFilters[1].begin();
while(it<TLFilters[1].end())
{
if (*it==1.0)TSContainCount++;
it++;
}

if((float)(TSContainCount)/(float)TLFilters[1].size()>=0.39)
{
TLDSend[1]=1.0;
TLCount[1]=0;//如果检测到,则计数器清零
}

else
{
//TLDSend[0]=0;
if(TLCount[1]<TLCountThreshold+5)
{
TLCount[1]=TLCount[1]+1;//如果未检测到,则计数器加1
}

if (TLCount[1]>TLCountThreshold)//如果丢失帧数大于阈值,则认为没有信号灯
{
TLDSend[1]=0;
}else
{//如果丢失帧数在阈值范围内,则认为还有信号灯
TLDSend[1]=1.0;
}
}
}

/*暂时去掉右转检测
if(p3==1)//右转禁止
{
Expand All @@ -167,9 +297,9 @@ void componentExtractionTL(IplImage* inputImage,IplImage* srcImage,float* TLDSen
TLDSend[2]=0;
containCount=0;
}*/
}
// }


//cout<<"count[0]:"<<TLCount[0]<<endl;
/* if (r<1&&g<1)//红灯绿灯均无
{
for (int i=0;i<3;i++)
Expand Down

0 comments on commit 7179d22

Please sign in to comment.