题目要求 |
程序代码 |
结果图片 |
要言妙道 |
题目要求:
优秀代码参考:
1 #include <iostream> 2 #include <cv.h> 3 #include <highgui.h> 4 #include <cxcore.h> 5 6 using namespace std; 7 const CvSize size = cvSize(200,200); 8 const int aperture[] = {3,5,9,11,13,17}; 9 int main() 10 { 11 IplImage *src = cvCreateImage(size,8,1); 12 cvZero(src); 13 cvLine(src 14 //,cvPoint(0,size.height - 1) 15 //,cvPoint(size.width -1 , 0) 16 ,cvPoint(0,0) 17 ,cvPoint(size.width - 1, size.height - 1) 18 ,CV_RGB(255,255,255) 19 ,3 20 ); 21 cvShowImage("src",src); 22 23 IplImage *deriv_x = cvCreateImage(size,IPL_DEPTH_32F,1); 24 IplImage *deriv_y = cvCreateImage(size,IPL_DEPTH_32F,1); 25 IplImage *magnitude = cvCreateImage(size,IPL_DEPTH_32F,1); 26 IplImage *angle = cvCreateImage(size,IPL_DEPTH_32F,1); 27 IplImage *mask = cvCreateImage(size,IPL_DEPTH_8U,1); 28 for(int i=0;i<sizeof(aperture)/sizeof(aperture[0]);++i){ 29 cvSobel(src,deriv_x,1,0,aperture[i]); 30 cvSobel(src,deriv_y,0,1,aperture[i]); 31 32 cvCartToPolar(deriv_x,deriv_y,magnitude,angle,1); 33 34 cvSave("x.xml",deriv_x); 35 cvSave("y.xml",deriv_y); 36 cvSave("magnitude.xml",magnitude); 37 cvSave("angle.xml",angle); 38 39 double max_mag ,min_mag ,max_angle , min_angle; 40 cvMinMaxLoc(magnitude,&min_mag,&max_mag); 41 cvMinMaxLoc(angle,&min_angle,&max_angle); 42 cout<<"magnitude: max = "<<max_mag<<" min = "<<min_mag<<endl; 43 cout<<"angle : max = "<<max_angle<<" min = "<<min_angle<<endl; 44 45 cvCmpS(magnitude,max_mag*3/4,mask,CV_CMP_GT); 46 cvShowImage("mask",mask); 47 48 CvScalar scalar = cvAvg(angle,mask); 49 cout<<"aperture = "<<aperture[i]<<", line angle = "<<scalar.val[0]<<endl; 50 cvWaitKey(); 51 } 52 cvReleaseImage(&src); 53 cvReleaseImage(&magnitude); 54 cvReleaseImage(&angle); 55 cvReleaseImage(&mask); 56 cvDestroyAllWindows(); 57 58 }
源自:学习opencv第六章习题5 , 使用x,y阶层数求出图像内唯一直线的角度
程序代码:
结果图片:
要言妙道:
时间: 2024-10-08 10:04:25