Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV:Hough圆变换-如何对输出进行排序_C++_Opencv_Image Processing_Computer Vision_Hough Transform - Fatal编程技术网

C++ OpenCV:Hough圆变换-如何对输出进行排序

C++ OpenCV:Hough圆变换-如何对输出进行排序,c++,opencv,image-processing,computer-vision,hough-transform,C++,Opencv,Image Processing,Computer Vision,Hough Transform,我写了一个可以找到重心的代码,如下所示: 我有6个圆圈,每个圆圈都有重心 我需要把两个一个地分组,找出重心之间距离的一半 在这一阶段,我有三点 取这三个点的平均值,我确定一个质心 我不知道如何对存储圆圈日期的向量进行排序 例1这里的指数很好 例2这里他们没有订单 这是我的密码 有没有可能用颜色对矢量和圆的数据进行排序? 比如: 数组元素中的两个圆:一个红色和一个绿色,它们彼此相邻 你只需要取所有6个的平均值。将它们成对相加并除以2,然后再加上三个均值并除以3,这与将所有6相加并除以6是一样的

我写了一个可以找到重心的代码,如下所示:

我有6个圆圈,每个圆圈都有重心 我需要把两个一个地分组,找出重心之间距离的一半 在这一阶段,我有三点 取这三个点的平均值,我确定一个质心 我不知道如何对存储圆圈日期的向量进行排序

例1这里的指数很好

例2这里他们没有订单

这是我的密码

有没有可能用颜色对矢量和圆的数据进行排序? 比如:
数组元素中的两个圆:一个红色和一个绿色,它们彼此相邻

你只需要取所有6个的平均值。将它们成对相加并除以2,然后再加上三个均值并除以3,这与将所有6相加并除以6是一样的。K均值聚类技术可能会对您有所帮助
int loadMyFrameWithHough (string framePath){

Mat myFrame1,myFrameGray,myFrameEdge;

myFrame1 = imread(framePath,CV_LOAD_IMAGE_COLOR);

cvtColor(myFrame1,myFrameGray,CV_BGR2GRAY);

vector<Vec3f> circles;

Canny( myFrameGray, myFrameEdge, 50, 150, 3);

HoughCircles( myFrameEdge, circles, CV_HOUGH_GRADIENT, 1, myFrameEdge.rows/10, 100, 9, 50, 90 );

for( int i = 0; i < circles.size(); i++ )
{
  Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  int radius = cvRound(circles[i][2]);

  string box_text = format("index : %d", i);
  putText(myFrame1, box_text, center, FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);//afisare index
  circle( myFrame1, center, 3, Scalar(0,0,0), -1, 8, 0 );//centrele cercurilor
  circle( myFrame1, center, radius, Scalar(0,0,0), 3, 8, 0 );//marginile cercurilor
}


float xMassVect[10],yMassVect[10];
int sizeofMass=0;
float xGMass=0,yGMass=0;
float xGmassAns=0,yGmassAns=0;
for( int i = 1; i < circles.size(); i=i+2 )
{

    Point Mass((cvRound(circles[i-1][0])+cvRound(circles[i][0]))/2,(cvRound(circles[i-1][1])+cvRound(circles[i][1]))/2);//masa a doua cate doua cercuri
    circle( myFrame1, Mass, 3, Scalar(0,0,0), -1, 8, 0 );
    sizeofMass=sizeof(Mass.x)-1; //marimea structurii Mass pentru a calcula centrul de greutatate ansamblului ((centru1+centru2+centru3)/3)

}
int k=0;
for (int i=1;i<circles.size();i=i+2)
{
    xMassVect[k]=(cvRound(circles[i-1][0])+cvRound(circles[i][0]))/2;//scrierea coordonatelor x in vector ale lui Mass
    yMassVect[k]=(cvRound(circles[i-1][1])+cvRound(circles[i][1]))/2;//scrierea coordonatelor y in vector ale lui Mass
    xGMass+=xMassVect[k];//suma centrelor de greutate pe x
    yGMass+=yMassVect[k];//suma centrelor de creutate pe y
    k++;
}

for (int k=0;k<circles.size()/2;k++)
{
    cout <<"Coordonate doua cate doua"<<endl;
    cout<<"x["<<k<<"]="<<xMassVect[k];
    cout<<"y["<<k<<"]="<<yMassVect[k];
    cout<<endl;
    xGmassAns+=xMassVect[k];
    yGmassAns+=yMassVect[k];

}
    cout<<"x centruAnsamblu="<<xGmassAns<<endl;
    cout<<"y centruAnsamblu="<<yGmassAns<<endl;

float distance[10];
float sumOfDistance=0;
float optimDistance=0;

for (int k=0;k<circles.size()/2;k++)
{
    distance[k]=sqrt(pow(xMassVect[k]-xGmassAns/(circles.size()/2),2)+pow(yMassVect[k]-yGmassAns/(circles.size()/2),2));//distantele de la centrul ansmblului la centrele cercurilor grupate 2 cate 2
    cout<<"distanta"<<k<<"="<<distance[k]<<endl;
}

for (int i=0;i<circles.size()/2;i++)
{
    sumOfDistance+=distance[i];//suma distantelor
}

    optimDistance=sumOfDistance/sizeofMass;//distanta optima ca medie aritmetica
    cout<<optimDistance;
    Point GmassAns(xGMass/sizeofMass,yGMass/sizeofMass);//centrul de greutate pentru intreg ansamblul
    circle(myFrame1,GmassAns,3,Scalar(10,10,100),-1,8,0);
    circle( myFrame1, GmassAns, optimDistance, Scalar(100,50,50), 3, 8, 0 );



     /// afisare rezultat
    namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
    imshow( "Hough Circle Transform Demo", myFrame1 );
    imshow( "Hough Circle Transform Edge", myFrameEdge );
    string savePath="D:/frame_1.bmp";
            imwrite(savePath,myFrame1);
    waitKey(0);
    return 0;


  }