1. Hough线变换
//Hough线变换 #include "cv.h" #include "highgui.h" int main() { // TODO: Add your command handler code here IplImage* pImage= NULL;// 声明IplImage 变量 IplImage* pImg8u= NULL;// 声明IplImage 变量,用于图像格式转换 IplImage* pImgCanny= NULL;// 声明IplImage 变量,用于灰度图像Canny变换 CvMemStorage* storage = NULL;// 声明storage 变量,用于存储检测到的线段 CvSeq* lines = NULL; //声明lines变量,用于存储直线的轮廓 //读入图像 pImage=cvLoadImage("Airplane.jpg", -1); //建立和原始图像一样图像内存区,图像元素的位深度设为IPL_DEPTH_8U //即无符号8位整型 pImg8u = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U, 1); pImgCanny = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U, 1); //将彩色图像转换为灰度图像 cvCvtColor(pImage, pImg8u, CV_BGR2GRAY); //创建内存空间 storage = cvCreateMemStorage(0); //利用Canny变换找出图像边缘 cvCanny( pImg8u, pImgCanny, 50, 500, 3 ); //Hough线变换 lines = cvHoughLines2( pImgCanny, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 80, 30, 10 ); //在原图上画红直线 int i; for( i = 0; i < lines->total; i++ ) { CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i); cvLine( pImage, line[0], line[1], CV_RGB(255,0,0), 3, 8 ); } //创建窗口,显示图像 cvNamedWindow( "Hough Line Transform", 1 ); cvShowImage( "Hough Line Transform", pImage); //等待按键 cvWaitKey(0); //销毁窗口 cvDestroyWindow( " Hough Line Transform " ); //将程序开始定义的变量释放 cvReleaseImage( & pImage); cvReleaseImage( & pImgCanny); cvReleaseImage( & pImg8u); }
2. Hough园变换
//Hough园变换 #include "cv.h" #include "highgui.h" int main() { // TODO: Add your command handler code here IplImage* pImage= NULL;// 声明IplImage 变量 IplImage* pImg8u= NULL;// 声明IplImage 变量,用于图像格式转换 CvMemStorage* storage = NULL;// 声明storage 变量,用于存储检测到的线段 CvSeq* circles = NULL; //读入图像 pImage=cvLoadImage("Airplane.jpg", -1); //建立和原始图像一样图像内存区,图像元素的位深度设为IPL_DEPTH_8U //即无符号8位整型 pImg8u = cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U, 1); //转换成灰度图像 if(pImage->nChannels != 1) cvCvtColor( pImage, pImg8u, CV_BGR2GRAY ); else cvCopy(pImage, pImg8u); //平滑化 cvSmooth( pImg8u, pImg8u, CV_GAUSSIAN, 7, 7 ); //创建内存空间 storage = cvCreateMemStorage(0); //Hough圆变换 circles = cvHoughCircles( pImg8u, storage, CV_HOUGH_GRADIENT, 2, pImg8u->height/4, 250, 55 ); // 画出识别出的圆 int i; for( i = 0; i < circles->total; i++ ) { float* p = (float*)cvGetSeqElem( circles, i ); cvCircle(pImage, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } //创建窗口,显示图像 cvNamedWindow( "Hough Circle Transform", 1 ); cvShowImage( "Hough Circle Transform", pImage); //等待按键 cvWaitKey(0); //销毁窗口 cvDestroyWindow( " Hough Circle Transform " ); //将程序开始定义的变量释放 cvReleaseImage( & pImage); cvReleaseImage( & pImg8u); }