OpenCV:椭圆上点的计算方程(3)

从一个椭圆上面获取特定个数的点的函数:

//参数描述:椭圆;输出的点集;欲获取的点的个数 int polygon::GetElipseEdge( const cv::RotatedRect &ecf, std::vector<std::pair< cv::Point2f, double > > &ellipseEdge, const int numPs, cv::Rect &roiRestrict, bool openEdgeRestrict ) { if ( numPs == 0 ) { return numPs; } else { ellipseEdge.resize( numPs ); } //对椭圆进行划分 const double angleGap = PI_4_2/numPs; const double cx = ecf.center.x; const double cy = ecf.center.y; const float angleOfDip = PI_1_2 + ecf.angle*3.1415926 /180.0;//为何偏移了 半个pi //const double angleOfDip =0- ecf.angle*3.1415926 /180.0;// double w = ecf.size.width /2.0; double h = ecf.size.height/2.0; for (int i=0 ;i< numPs;++i ) { double as = i*angleGap ; double a = as ; a += angleOfDip; a = a>PI_4_2? a-PI_4_2:a; double y = (w) *sin( a ); double x = (h) *cos( a ); //旋转 float xDeta = x*cos( angleOfDip ) - y*sin( angleOfDip ); float yDeta = x*sin( angleOfDip ) + y*cos( angleOfDip ); cv::Point2f p( cx+xDeta, cy+yDeta); //ellipseEdge[i] = (std::pair< T1, T2 >)(std::make_pair( p,as ) ); //ellipseEdge[i] = (std::pair< cv::Point2f, double >)(std::make_pair( p,as ) );//此处代码只为运行于GCC修改,有问题,模板库不能使用!!!wishchin!!! ellipseEdge[i].first.x = p.x; ellipseEdge[i].first.y = p.y; ellipseEdge[i].second = as; } if (openEdgeRestrict) { float x,y; float xS(roiRestrict.x), yS(roiRestrict.y), xE(roiRestrict.x+roiRestrict.width), yE(roiRestrict.y+roiRestrict.height ); for (int i=0 ;i< numPs;++i ) { x = ellipseEdge[i].first.x; y = ellipseEdge[i].first.y; x = (std::min)( (std::max)(x,xS),xE ); y = (std::min)( (std::max)(y,yS),yE ); //ellipseEdge[i].first = cv::Point2f(x,y); ellipseEdge[i].first.x = x; ellipseEdge[i].first.y = y; } } else { } return 1; }

 结果显示:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/828cf9678cfb70b2199b79d816ba77bc.html