24,854
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include "threshold.h"
using namespace std;
int main(){
CvCapture *capture = cvCreateFileCapture("F:\\cvsource\\1.avi");//注意读取不在工程目录下文件时的路径要加双斜杠
IplImage *img = cvQueryFrame(capture);
//读取矩阵
CvMemStorage *memstorageTest=cvCreateMemStorage(0);
CvFileStorage *warp_read=cvOpenFileStorage("F:\\cvsource\\1.xml",memstorageTest,CV_STORAGE_READ);//矩阵所在的xml文件名
CvMat *map_matrix = cvCreateMat(3,3,CV_32FC1);
map_matrix = (CvMat*)cvReadByName(warp_read,NULL,"WarpMatrix",NULL);//读取矩阵,注意双括号里面是矩阵的名字
//设置原图像的ROI范围
int x = 65,y = 151;//这个y值就是要修改的,尽量裁掉天空,不然会给处理带来挺大困难
int width = img->width , height = img->height - y;
cvNamedWindow("OriginalView");//创建显示窗口
cvNamedWindow("IPMview");
cvNamedWindow("CannyShow");
cvNamedWindow("HoughShow");
int frame=0;//当前帧数
while (img!=NULL)//当读取到空的时候,即视频播放完毕则停止处理
{
frame++;
cout<<frame<<endl;
img = cvQueryFrame(capture);//不断读取下一帧
cvSetImageROI(img,cvRect(x,y,width,height));//设置ROI
IplImage *ImageCut = cvCreateImage(cvGetSize(img),8,3);
cvCopy(img,ImageCut);//将原图像的ROI赋给新图像ImageCut
//创建用于变换的图像
IplImage *ImageIPM = cvCreateImage(cvGetSize(ImageCut),8,3);
IplImage *ImageHough = cvCreateImage(cvGetSize(ImageCut),8,3);
//反透视变换
cvShowImage("OriginalView",ImageCut);
cvWarpPerspective(ImageCut,ImageIPM,map_matrix);
cvWarpPerspective(ImageCut,ImageHough,map_matrix);
cvShowImage("IPMview",ImageIPM);
//Canny变换
IplImage *ImageCanny = cvCreateImage(cvGetSize(ImageIPM),8,1);
//int best = otsuThreshold(ImageIPM);
//the best is said to be 40 by Huang Yuhong
cvCanny(ImageIPM,ImageCanny,40,100);
cvShowImage("CannyShow",ImageCanny);
//hough直线检测
CvMemStorage *storage = cvCreateMemStorage();
CvSeq *lines = 0;
///////////////////////////////////////
lines = cvHoughLines2(ImageCanny,storage,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,20,30,10);
//lines = cvHoughLines2(ImageCanny,storage,CV_HOUGH_STANDARD,1,CV_PI/180,30,30,5);
printf("Lines number: %d\n",lines->total);
for (int i=0;i<lines->total;i++)
{
CvPoint *line = (CvPoint *)cvGetSeqElem(lines,i);
//double cmp_factor = line->y * 1.0 / line->x ;
if(line[1].x>565) continue;
if(line[1].x<270) continue;
// double cmp_factor = (line[0].y - line[1].y)*1.0/(line[0].x - line[1].x);
// if((cmp_factor<5&&cmp_factor>0)||(cmp_factor>-5&&cmp_factor<0)) continue;
cvLine(ImageHough,line[0],line[1],CV_RGB(255,255,0),5,CV_AA);
}
//////////////////////////////////////
/*lines = cvHoughLines2(ImageCanny,storage,CV_HOUGH_STANDARD,1,CV_PI/180,50,0,0);
printf("Lines number: %d\n",lines->total);
for (int i=0;i<lines->total;i++)
{
float *line = (float *)cvGetSeqElem(lines,i);
float rho = line[0];
float theta = line[1];
CvPoint pt1,pt2;
double a = cos(theta);
double b = sin(theta);
if (fabs(a)<0.001)
{
pt1.x = pt2.x = cvRound(rho);
pt1.y = 0;
pt2.y = ImageCanny->height;
}
else if (fabs(b)<0.001)
{
pt1.y = pt2.y = cvRound(rho);
pt1.x = 0;
pt2.x = ImageCanny->width;
}
else
{
pt1.x = 0;
pt1.y = cvRound(rho/b);
pt2.x = cvRound(rho/a);
pt2.y = 0;
}
cvLine(ImageHough,pt1,pt2,CV_RGB(255,255,0),1,8);
}
*/
///////////////////////////////////
cvShowImage("HoughShow",ImageHough);
cvWaitKey(30);
cvReleaseImage(&ImageCut);
cvReleaseImage(&ImageIPM);
cvReleaseImage(&ImageCanny);
cvReleaseImage(&ImageHough);
}
return 0;
}
。还有,找到车道线后我得到的坐标是我进行反透视变换后的图中的坐标,那么我该如何将其转换到原图中呢?