如何在轮廓上找到具有特定度数的点(Opencv)

时间:2013-11-26 10:55:41

标签: opencv point contour angle

我有轮廓点,我想得到特定度数点的(x,y)。从附图中,我想得到黄点。

Get the yellow points that are on 0,45,90,135,180,125,270,and 360 degree.

1 个答案:

答案 0 :(得分:1)

我使用了@Aleksander和@Zaw给我的提示。 轮廓的质心用作绘制线条的一个点,我使用线方程得到每条线的第二个点并绘制它。

y - y1 = m (x - x1)

得到 m 我们使用以下等式

arctan( **y** of the centroid / **x** of the centroid) - angle in radiant = Phi

tan(Phi) = m 

然后我用

y = m (x - x1) + y1

当x1和y1是质心点时,x是绘制直线所需的第二个点的假定坐标。

void MyDraw (Mat Draw, vector<Point> hull,Scalar color,int thickness){
  std::vector<std::vector<cv::Point> > T = std::vector<std::vector<cv::Point> >(1,hull); 
  drawContours(Draw, T, -1, color, thickness);} 

Point GetCentroidOfConvexHull(vector<Point> Hull){
// Get the moments
  Moments mu;
  mu= moments(Hull, false );
// Get the mass centers:
  return Point( mu.m10/mu.m00 , mu.m01/mu.m00 );}   



void _tmain(int argc, _TCHAR* argv[]){ 
    vector<vector<Point>> contours;
    vector<Point> hull;
    Mat testContour = Mat::zeros(width, height,CV_8UC3);
    Mat testLine = Mat::zeros(width, height,CV_8UC3);
    Mat andResult;
    Point centroid;

    findContours(Dilated,contour,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,Point (0,0));
    convexHull(contour,hull,false);
    centroid = GetCentroidOfConvexHull(hull);
    MyDraw(testContour,hull,White,1);
    Rect myRect = boundingRect(hull);
    imshow("test contour",testContour);
    waitKey(0);
    float degrees[8] = {0,45,90,135,180,225,270,315};
    Point point;
    // for each degree                
    for (int d = 0 ; d < 8 ; d++){
       //Get line equation from the slope and the center point of the contour
       float m = std::tan( std::atan2((double)centroid.y,(double)centroid.x) - (degrees[d]*3.14/180));
       // using the upper left and lower right points to get the x of the other point
       if ((d >= 0 && d <= 2) || d == 7) point.x = myRect.tl().x;
       else point.x = myRect.br().x;
       point.y = (int)(m * (float)(point.x - centroid.x) + centroid.y);
       line(testLine,centroid,point,White,1,8);}

       imshow("test Line",testLine);
       waitKey(0);
       // and operation
       andResult = testContour & testLine;
       imshow("Anding Result",andResult);
       waitKey(0);}

在绘制轮廓和线条之后,我做了一些明智的 AND 操作来获得线条和轮廓之间的交集。

contour and lines

Extracted points