学习opencv第六章习题5 , 使用x,y阶层数求出图像内唯一直线的角度


#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>

using namespace std;
const CvSize size = cvSize(200,200);
const int aperture[] = {3,5,9,11,13,17};
int main()
{
	IplImage *src = cvCreateImage(size,8,1);
	cvZero(src);
	cvLine(src
		//,cvPoint(0,size.height - 1)
		//,cvPoint(size.width -1 , 0)
		,cvPoint(0,0)
		,cvPoint(size.width - 1, size.height - 1)
		,CV_RGB(255,255,255)
		,3
		);
	cvShowImage("src",src);
	
	IplImage *deriv_x = cvCreateImage(size,IPL_DEPTH_32F,1);
	IplImage *deriv_y = cvCreateImage(size,IPL_DEPTH_32F,1);
	IplImage *magnitude = cvCreateImage(size,IPL_DEPTH_32F,1);
	IplImage *angle = cvCreateImage(size,IPL_DEPTH_32F,1);
	IplImage *mask = cvCreateImage(size,IPL_DEPTH_8U,1);
	for(int i=0;i<sizeof(aperture)/sizeof(aperture[0]);++i){
		cvSobel(src,deriv_x,1,0,aperture[i]);
		cvSobel(src,deriv_y,0,1,aperture[i]);

		cvCartToPolar(deriv_x,deriv_y,magnitude,angle,1);

		cvSave("x.xml",deriv_x);
		cvSave("y.xml",deriv_y);
		cvSave("magnitude.xml",magnitude);
		cvSave("angle.xml",angle);

		double max_mag ,min_mag ,max_angle , min_angle;
		cvMinMaxLoc(magnitude,&min_mag,&max_mag);
		cvMinMaxLoc(angle,&min_angle,&max_angle);
		cout<<"magnitude: max = "<<max_mag<<" min = "<<min_mag<<endl;
		cout<<"angle : max = "<<max_angle<<" min = "<<min_angle<<endl;
		
		cvCmpS(magnitude,max_mag*3/4,mask,CV_CMP_GT);
		cvShowImage("mask",mask);

		CvScalar scalar = cvAvg(angle,mask);
		cout<<"aperture = "<<aperture[i]<<", line angle = "<<scalar.val[0]<<endl;
		cvWaitKey();
	}
	cvReleaseImage(&src);
	cvReleaseImage(&magnitude);
	cvReleaseImage(&angle);
	cvReleaseImage(&mask);
	cvDestroyAllWindows();

}






你可能感兴趣的:(include)