求opencv代码详解
求此代码的详解,最好能精确到每一行,谢谢大神!#include<cv.h>#include<highgui.h>#include<iostream>//#pragmaco...
求此代码的详解,最好能精确到每一行,谢谢大神!
#include<cv.h>
#include<highgui.h>
#include<iostream>
// #pragma comment(lib, "cv.lib")
// #pragma comment(lib, "cxcore.lib")
// #pragma comment(lib, "highgui.lib")
using namespace std;
using namespace cv;
int main(int argc,char** argv)
{
CvCapture* capture1 = cvCreateCameraCapture(0);
double w=640,h=480;
//cvNamedWindow("camera_1",CV_WINDOW_AUTOSIZE);
cvSetCaptureProperty(capture1,CV_CAP_PROP_FPS,5);
IplImage* pImgSrc = NULL; //源图像
IplImage* pImg8u = NULL; //灰度图
IplImage* pImgCanny = NULL; //边缘检测后的图
IplImage* pImgDst = NULL; //在图像上画上检测到的直线后的图像
CvSeq* lines = NULL;
CvMemStorage* storage = NULL;
/*边缘检测*/
//pImgSrc = cvLoadImage ("D:\\wode.jpg", 1);
while(1)
{
cout<<"1"<<endl;
pImgSrc=cvQueryFrame(capture1);
pImg8u = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 1);
pImgCanny = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 1);
pImgDst = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 1);
cvCvtColor (pImgSrc, pImg8u, CV_BGR2GRAY);
cvCanny (pImg8u, pImgCanny, 20, 200, 3);
/*检测直线*/
storage = cvCreateMemStorage (0);
lines = cvHoughLines2 (pImgCanny, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 80, 200, 10);
pImgDst = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 3);
cvCvtColor (pImg8u, pImgDst, CV_GRAY2BGR);
/*在pImgDst上画出检测到的直线*/
for (int i = 0; i < lines->total; i++)
{
CvPoint* line = (CvPoint*)cvGetSeqElem (lines, i);
cvLine (pImgDst, line[0], line[1], CV_RGB(255,0,0), 3, 8);
}
//cvNamedWindow ("src",1);
//cvNamedWindow ("canny", 1);
//cvNamedWindow ("hough", 1);
cvShowImage ("src", pImgSrc);
cvShowImage ("canny", pImgCanny);
cvShowImage ("hough", pImgDst);
cvWaitKey (0);
}
cvReleaseImage (&pImgSrc);
cvReleaseImage (&pImg8u);
cvReleaseImage (&pImgCanny);
cvReleaseImage (&pImgDst);
cvReleaseMemStorage (&storage);
return 0;
} 展开
#include<cv.h>
#include<highgui.h>
#include<iostream>
// #pragma comment(lib, "cv.lib")
// #pragma comment(lib, "cxcore.lib")
// #pragma comment(lib, "highgui.lib")
using namespace std;
using namespace cv;
int main(int argc,char** argv)
{
CvCapture* capture1 = cvCreateCameraCapture(0);
double w=640,h=480;
//cvNamedWindow("camera_1",CV_WINDOW_AUTOSIZE);
cvSetCaptureProperty(capture1,CV_CAP_PROP_FPS,5);
IplImage* pImgSrc = NULL; //源图像
IplImage* pImg8u = NULL; //灰度图
IplImage* pImgCanny = NULL; //边缘检测后的图
IplImage* pImgDst = NULL; //在图像上画上检测到的直线后的图像
CvSeq* lines = NULL;
CvMemStorage* storage = NULL;
/*边缘检测*/
//pImgSrc = cvLoadImage ("D:\\wode.jpg", 1);
while(1)
{
cout<<"1"<<endl;
pImgSrc=cvQueryFrame(capture1);
pImg8u = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 1);
pImgCanny = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 1);
pImgDst = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 1);
cvCvtColor (pImgSrc, pImg8u, CV_BGR2GRAY);
cvCanny (pImg8u, pImgCanny, 20, 200, 3);
/*检测直线*/
storage = cvCreateMemStorage (0);
lines = cvHoughLines2 (pImgCanny, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 80, 200, 10);
pImgDst = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 3);
cvCvtColor (pImg8u, pImgDst, CV_GRAY2BGR);
/*在pImgDst上画出检测到的直线*/
for (int i = 0; i < lines->total; i++)
{
CvPoint* line = (CvPoint*)cvGetSeqElem (lines, i);
cvLine (pImgDst, line[0], line[1], CV_RGB(255,0,0), 3, 8);
}
//cvNamedWindow ("src",1);
//cvNamedWindow ("canny", 1);
//cvNamedWindow ("hough", 1);
cvShowImage ("src", pImgSrc);
cvShowImage ("canny", pImgCanny);
cvShowImage ("hough", pImgDst);
cvWaitKey (0);
}
cvReleaseImage (&pImgSrc);
cvReleaseImage (&pImg8u);
cvReleaseImage (&pImgCanny);
cvReleaseImage (&pImgDst);
cvReleaseMemStorage (&storage);
return 0;
} 展开
1个回答
展开全部
#include<cv.h>
#include<highgui.h>
#include<iostream>
// #pragma comment(lib, "cv.lib")
// #pragma comment(lib, "cxcore.lib")
// #pragma comment(lib, "highgui.lib")
using namespace std;
using namespace cv;
int main(int argc,char** argv)
{
CvCapture* capture1 = cvCreateCameraCapture(0);//创建一个capture指针,用于打开摄像 头
double w=640,h=480; //后面有用到这两个变量吗?我没看到
//cvNamedWindow("camera_1",CV_WINDOW_AUTOSIZE);
cvSetCaptureProperty(capture1,CV_CAP_PROP_FPS,5);//设置视频属性以帧的位置为单位,具体的去网上查这个函数的用法
IplImage* pImgSrc = NULL; //源图像
IplImage* pImg8u = NULL; //灰度图
IplImage* pImgCanny = NULL; //边缘检测后的图
IplImage* pImgDst = NULL; //在图像上画上检测到的直线后的图像;
CvSeq* lines = NULL; //创建一个序列,相当于一个链表;
CvMemStorage* storage = NULL; //创建一个存储器,用于存储序列;
/*边缘检测*/
//pImgSrc = cvLoadImage ("D:\\wode.jpg", 1); //以彩图的形式载入一幅图片;
while(1)
{
cout<<"1"<<endl;//这个不用说了吧;
pImgSrc=cvQueryFrame(capture1);//从视频中获取一帧图片;
pImg8u = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 1); //创建一副8位无符型单通道的图片,大小和pImgSrc相同;
pImgCanny = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 1); //同上;
pImgDst = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 1); //同上;
cvCvtColor (pImgSrc, pImg8u, CV_BGR2GRAY); 将彩图转化为灰度图,储存在pImg8u中;
cvCanny (pImg8u, pImgCanny, 20, 200, 3); //canny算子,用于边缘检测;
/*检测直线*/
storage = cvCreateMemStorage (0); //为存储器申请存储空间;
lines = cvHoughLines2 (pImgCanny, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 80, 200, 10); //累计概率霍夫变换(PPHT)返回一个序列存储在存储器中
pImgDst = cvCreateImage (cvGetSize(pImgSrc), IPL_DEPTH_8U, 3); 创建一副8位无符3通道图片;
cvCvtColor (pImg8u, pImgDst, CV_GRAY2BGR); //将灰度图转化为彩图;
/*在pImgDst上画出检测到的直线*/
for (int i = 0; i < lines->total; i++)
{
CvPoint* line = (CvPoint*)cvGetSeqElem (lines, i); //访问链表,获取点的数据;
cvLine (pImgDst, line[0], line[1], CV_RGB(255,0,0), 3, 8); //用红色画出line[0]与line[1]之间直线;
}
//cvNamedWindow ("src",1);
//cvNamedWindow ("canny", 1);
//cvNamedWindow ("hough", 1);
cvShowImage ("src", pImgSrc); //显示图片;
cvShowImage ("canny", pImgCanny);//同上;
cvShowImage ("hough", pImgDst); //同上;
cvWaitKey (0); //暂停在这,等待键盘敲入数据;
}
cvReleaseImage (&pImgSrc); //释放图片指针
cvReleaseImage (&pImg8u); //同上;
cvReleaseImage (&pImgCanny); //同上;
cvReleaseImage (&pImgDst); //同上;
cvReleaseMemStorage (&storage); //释放存储器空间;
return 0;
}
我是一个初学者,大神一般是不会给人看代码的,我是按照一条条语句翻译的,但是就我感觉来说这段程序似乎有点问题,这应该不是教程吧,上面有一句灰度图转彩图的,个人认为灰度图本省就是由pImgSrc转化过来的,要转化回去,为什么不直接Copy源图像呢?个人见解,如果你发现有什么不足的地方直接说出来就好,大家一起进步
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询