如何在OpenCV和C+中查找图像中两个对象(点)之间的距离(物理单位为m和像素)++;? 使用OpenCV和C++。我已经完成了可以找到对象中心的代码。我还有关于相机和图像中物体之间距离的先验信息。 我需要计算图像中两个对象之间的距离(以m或cm为单位的实际物理距离,以及以像素为单位的距离),或者这些对象的两个中心之间的距离)

如何在OpenCV和C+中查找图像中两个对象(点)之间的距离(物理单位为m和像素)++;? 使用OpenCV和C++。我已经完成了可以找到对象中心的代码。我还有关于相机和图像中物体之间距离的先验信息。 我需要计算图像中两个对象之间的距离(以m或cm为单位的实际物理距离,以及以像素为单位的距离),或者这些对象的两个中心之间的距离),c++,opencv,euclidean-distance,C++,Opencv,Euclidean Distance,下面是查找矩形对象中心力矩的代码。类似的方法是找到其他形状(对象)的中心 int main(int argc,char**argv) { Mat image=imread(“000167.png”); Mat gray、bw、dil、erd、dst_最终版; Mat new_src=image.clone(); 对于(int y=0;y

下面是查找矩形对象中心力矩的代码。类似的方法是找到其他形状(对象)的中心

int main(int argc,char**argv)
{
Mat image=imread(“000167.png”);
Mat gray、bw、dil、erd、dst_最终版;
Mat new_src=image.clone();
对于(int y=0;y如果((浮动)(r.height/r.width)>1.5&&(浮动)(r.height/r.width)这个答案是关于获取两点之间的公制距离

如果:

  • 这两个点位于垂直于相机光轴的(几何)平面上
  • 您拥有相机中心和平面之间的距离
然后,可以使用投影基础计算这两点之间的度量距离:

  • d_m:两点之间的公制距离(m.)
  • d_p:两点之间的像素距离
  • f:焦点(m.)
  • k:摄像机传感器每米像素的nbe(=1/ps,ps:像素大小,通常为5到10微米)
  • z_M:两点之间的距离(M.)
我们有:
d_p=k.f.d_M/Z_M

所以:
d_M=d_p.z_M/(k.f)


如果上述条件未完全满足,则会产生一些误差。问题中不清楚这些要求是否满足。如果不满足,则可能无法得到答案。除非已知平面和轴之间的角度,否则在这种情况下,将需要进行一些基本的三角计算。

答案是关于得到两点之间的公制距离

如果:

  • 这两个点位于垂直于相机光轴的(几何)平面上
  • 您拥有相机中心和平面之间的距离
然后,可以使用投影基础计算这两点之间的度量距离:

  • d_m:两点之间的公制距离(m.)
  • d_p:两点之间的像素距离
  • f:焦点(m.)
  • k:摄像机传感器每米像素的nbe(=1/ps,ps:像素大小,通常为5到10微米)
  • z_M:两点之间的距离(M.)
我们有:
d_p=k.f.d_M/Z_M

所以:
d_M=d_p.z_M/(k.f)


如果上述条件未完全满足,则会产生一些误差。问题中不清楚这些要求是否满足。如果不满足,则可能无法得到答案。除非已知平面和轴之间的角度,否则在这种情况下,将需要进行一些基本的三角计算。

ar.你发布的代码和你的目标任务之间有什么关系?你要求的是公制距离和像素距离。对于后者,如果你有中心的坐标,这不是很明显吗?对于公制距离,理论上一个摄像头是不可能的,除非你知道摄像头和目标之间的距离e对象。你可以编辑问题并澄清吗?好的。代码是找到对象的中心。在这种情况下是飞机的门窗。清楚吗?好的,如果有两个中心的像素坐标,这很容易。我的问题我指出,我已经知道相机和对象之间的距离。我说我有两个中心之间的距离在摄像机和物体之间,我知道信息先验,原因可以用激光测量距离。现在清楚了吗?所以基本上,只需要找到公制距离,知道摄像机和物体之间的距离。好吗?我仍然不清楚:你为什么不以类似的方式获得“开放”的边界矩形签名,然后只检查两个矩形之间的距离?或者如果你找不到它,你可以在图像中搜索白色?提示:除了发布代码外,你应该用几行描述你正在实现的算法,这将有助于回答你的问题。我没有问题获取t的边界矩形他有“打开”的标志。也没什么问题。只是没有附上代码。
int main(int argc,char** argv)
{

           Mat image = imread("000167.png");
           Mat gray,bw,dil,erd, dst_final;
       Mat new_src=image.clone();

     for(int y = 0; y < image.rows; y++ )
    {
       for(int x = 0; x < image.cols; x++ )
       {
              for(int c = 0; c < 3; c++ )
              {
                     new_src.at<Vec3b>(y,x)[c]= saturate_cast<uchar>( 1.5*(image.at<Vec3b>(y,x)[c] ));

              }
       }
     }


      cv::GaussianBlur(new_src, src_gray, cv::Size(3,3),1,1,BORDER_DEFAULT); //original
      medianBlur(new_src, src_gray, 11);
      blur( new_src, src_gray, Size(3,3) );
      cvtColor(src_gray,gray,CV_BGR2GRAY);
      Canny(gray,bw,600,1200,5,true);
                    Mat grad,bw1;
                    Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(20,10));
                    morphologyEx(bw, grad, MORPH_GRADIENT, morphKernel);        
                    threshold(grad, bw1, 255.0, 255.0, THRESH_BINARY | THRESH_OTSU);                

          vector<vector<Point> > contours;
      vector<vector<Point> > rough;
      vector<vector<Point> >rough_color;
      vector<vector<Point> >precise;
          vector<Vec4i> hierarchy;
      Mat dst = image.clone();
          findContours(bw1.clone(), contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

      vector<Point> approx;

      for( int i = 0; i< contours.size(); i++ )
      {

           approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true) * 0.01, true);
           if (fabs(contourArea(approx)) <2000 || fabs(contourArea(approx))>50000)                                     
               continue;

                  Rect r = boundingRect(contours[i]);
                  if( (float) (r.height/r.width) > 1.5 && (float) (r.height/r.width) <3)
                  {
                   rough.push_back(approx);
                   }
      }

      for(int i = 0; i < rough.size(); i++)
      {
          Rect bound=boundingRect(rough[i]);
          Rect x, y, w, h = boundingRect(rough[i]);
          rectangle(dst,Point(bound.br().x,bound.br().y), Point(bound.tl().x,bound.tl().y),Scalar(255, 0, 0),2);
          RotatedRect rectPoint = minAreaRect(rough[i]);

              Point2f fourPoint2f_rough[4];
              rectPoint.points(fourPoint2f_rough);
          vector<Point> fourPoint_rough;
          for(int i = 0; i <4; i++)
         {
             fourPoint_rough.push_back(fourPoint2f_rough[i]);
         }


        {
            line(dst, fourPoint2f_rough[i], fourPoint2f_rough[i + 1], Scalar(255,0,0), 3);
        }
        line(dst, fourPoint2f_rough[0], fourPoint2f_rough[3], Scalar(255,0,0), 3); */
      }

      if(rough.size() !=0 )
      {
      for( int i = 0; i< rough.size(); i++ )
      {
          vector<Moments> mu(1);
          vector<Point2f> mc(1);
          int gray_level;
          // compute the central momment
             mu[0] = moments( rough[i], false );
             mc[0] = Point2f( mu[0].m10/mu[0].m00 , mu[0].m01/mu[0].m00 );
          circle( dst, mc[0], 4, Scalar(0,0,255), -1, 8, 0 );
          gray_level=gray.at<uchar>(mc[0]);
          if(gray_level<200 && gray_level>20)
          {rough_color.push_back(rough[i]);}
      }


      for(int i = 0; i < rough_color.size(); i++)
      {
          Rect bound=boundingRect(rough_color[i]);
          Rect x, y, w, h = boundingRect(rough_color[i]);
          RotatedRect rectPoint = minAreaRect(rough_color[i]);
              Point2f fourPoint2f_color[4];
              rectPoint.points(fourPoint2f_color);
         vector<Point> fourPoint_color;
         for(int i = 0; i <4; i++)
         {
             fourPoint_color.push_back(fourPoint2f_color[i]);
         }


         for (int i = 0; i < 3; i++)
        {
            line(dst, fourPoint2f_color[i], fourPoint2f_color[i + 1], Scalar(0,255,0), 3);
        }
        line(dst, fourPoint2f_color[0], fourPoint2f_color[3], Scalar(0,255,0), 3);
      }

      }
...