|
#include "stdafx.h"
#include "stdio.h"
#include "cv.h"
#include "highgui.h"
int _tmain(int argc, _TCHAR* argv[])
{
LARGE_INTEGER freq;
LONGLONG QPart1,QPart2;
double dfMinus, dfFreq, dfTim;
QueryPerformanceFrequency(&freq);
dfFreq = (double)freq.QuadPart;
//初始化从文件中获取视频
CvCapture* capture = cvCaptureFromFile( "D:\\360\\Q.mp4" );
if (!capture)
{
printf("unable to load a video");
cvWaitKey(0);
exit(0);
}
//创建窗口
cvNamedWindow("Source Video", 1);
cvNamedWindow("Destination Video", 1);
IplImage* pImgFrame;
IplImage* pImgEdge = 0;
IplImage* pImgGray = 0;
IplImage* pImgSmooth = 0;
//动态内存存储器
CvMemStorage* storage=cvCreateMemStorage(0); //cvCreateMemStorage 用于创建一内存块并返回指向块首的指针
//CvSeq 定义非固定元素的序列,是所有OpenCV动态数据结构的基础
CvSeq* lines=NULL;/*The detected lines will be stored here*/
//获得视频获取结构的帧率
double fps = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
printf("fps=%d n",(int)fps);
//CvSize以像素为单位定义矩形框的大小
CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));
printf("frame (w, h) = (%d, %d)n",size.width,size.height);
/*Creating the Video writer If you do not know about the Video writer study this post and come back to tis line*/
int counter=0;
//定义矩形框的偏离以及长和宽
CvRect ROI_rect_src;
ROI_rect_src.x = 0;
ROI_rect_src.y = (size.height*1)/2;
ROI_rect_src.width = size.width;
ROI_rect_src.height = (size.height/8)*4;
while( (pImgFrame = cvQueryFrame(capture)) != NULL ) //cvQueryFrame是从视频中抓取并返回一帧图像
{
counter++;
QueryPerformanceCounter(&freq);
QPart1 = freq.QuadPart;
//创建图像头并分配数据
pImgGray = cvCreateImage(cvGetSize(pImgFrame),IPL_DEPTH_8U,1);
pImgEdge = cvCreateImage(cvSize(pImgFrame->width,pImgFrame->height),IPL_DEPTH_8U,1);
pImgSmooth = cvCreateImage(cvSize(pImgFrame->width,pImgFrame->height),IPL_DEPTH_8U,1);
/*gray-scale color images*/
cvCvtColor(pImgFrame, pImgGray, CV_BGR2GRAY);
/*Gaussian filtering*/
cvSmooth(pImgGray,pImgSmooth,CV_GAUSSIAN,3,0,0);
/*Canny Edge Detection*/
cvCanny(pImgSmooth, pImgEdge, 40, 120, 3);
/*Detect straight lines*/
cvSetImageROI(pImgEdge,ROI_rect_src); //cvSetImageROI的功能是基于给定的矩形设置图像的ROI(感兴趣区域)
cvShowImage("pImgEdge",pImgEdge);
//cvHoughLines2利用Hough变换在二值图像中寻找直线
lines=cvHoughLines2(pImgEdge,storage,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,50,5,5);
cvResetImageROI(pImgEdge);
/*All detected lines marked*/
int i;
for(i = 0; i < lines->total; i++)
{
CvPoint* point = (CvPoint*)cvGetSeqElem(lines,i);//cvGetSeqElem返回索引指定的元素指针
double slope = fabs (((double)(point[1].y - point[0].y))/((double)(point[1].x - point[0].x)));
if((slope > 0.3) && (slope < 1000))
{
CvPoint Point0;
Point0.x = point[0].x;
Point0.y = point[0].y + ROI_rect_src.y;
CvPoint Point1;
Point1.x = point[1].x;
Point1.y = point[1].y + ROI_rect_src.y;
cvLine(pImgFrame,Point0,Point1,CV_RGB(255,0,0),2,CV_AA,0);
}
}
cvShowImage("Source Video", pImgFrame);
cvShowImage("Destination Video", pImgEdge);
QueryPerformanceCounter(&freq);
QPart2 = freq.QuadPart;
dfMinus = (double)(QPart2 - QPart1);
dfTim = 1000 * dfMinus / dfFreq;
printf("The current image processing takes %.2f ms\n",dfTim);
cvWaitKey(100);
}
cvReleaseCapture( &capture );
cvReleaseImage(&pImgGray);
cvReleaseImage(&pImgSmooth);
cvReleaseImage(&pImgEdge);
cvDestroyWindow("Source Video");
cvDestroyWindow("Destionation Video");
return 0;
这是一个车道线检测程序,但是通过霍夫变换,会检测出很多除车道线意外的其他不需要的直线,怎样修改程序把这些多余的线段去掉?并把车道线标记出来。
} |
|