gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:08 24 4
gpt4 key购买 nike

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

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

最佳答案

我使用了@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

关于opencv - 如何在轮廓上找到具有特定度数的点(Opencv),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20215104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com