如何在OpenCV和C +++中找到图像中两个对象(点)之间的距离(以m和像素为单位的物理距离)?

鲍勃

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

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

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);
      }

      }
...

因此,只需要一个代码即可获得这两个中心矩(图像中对象的两个中心)之间的距离(以cm或m为单位,以像素为单位)。我在矩形窗口(窗口中的红点)中包括中心的结果图像。因此,在这种情况下,我想计算窗口与“ OPEN”框标志之间的距离。

矩形窗中心

烤肉串

这个答案是关于获取两点之间的公制距离。

如果:

  • 这两个点在垂直于相机光轴的(几何)平面上
  • 你有相机中心和飞机之间的距离

那么可以使用投影基础知识计算这两个点之间的公制距离:

  • d_m:两点之间的公制距离(米)
  • d_p:两点之间的像素距离
  • f:焦点(米)
  • k:相机传感器每米的像素nbe(= 1 / ps,ps:像素大小,通常为5到10 um)
  • z_M:两点之间的距离(米)

我们有: d_p = k . f . d_M/Z_M

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

如果上述条件不能完全满足,则会引起一些错误。从这个问题尚不清楚是否满足这些要求。如果不是,那么就不可能得到答案。除非可能知道平面和轴之间的角度,否则将需要一些基本的三角函数计算。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我如何找到两个点/像素之间的距离?

如何找到 GPS 轨迹数据集中两个相邻点之间的距离(以英尺为单位)?

如何使用python和opencv在图像上绘制两个轮廓,给定它们之间的轮廓距离为100像素

如何找到Sprite和屏幕角之间的距离(以像素为单位)?

在传单中,如何计算两个LatLng对象之间的像素距离?

如何计算R中对象(变量)和组(两个变量)之间的距离矩阵

如何在Python中找到两个一维数组之间的马氏距离?

如何找到两个矩形之间最接近的距离的两个点?

在Android中使用两个不同点的经度和纬度找到以米为单位的距离

计算Android中两个纬度和经度点之间的距离

Python中的Haversine公式(两个GPS点之间的轴承和距离)

如何通过白色像素找到两个连接点之间的距离?

如何在0和1的矩阵中找到两个随机点之间的路径

如何添加两个点之间的距离(SRID = 32636)?

如何找到两个补丁之间的距离?

如何找到React Native的两个元素之间的距离?

如何找到两个经度位置之间的距离?

如何找到序列矩阵中两点之间的距离?

如何找到两点之间的距离?

如何有效地在两个具有最小距离的点列表中找到点对?

如何计算数据框中两个连续点之间的距离和角度?

在c#或asp.net中找到地图上两个地方之间的距离以及汽车路线

如何找到两个日期之间以秒和纳秒为单位的时间差?

如何在两个列表中找到紧密匹配的独特元素?(在此使用距离功能)

如何在opencv中查找图像内像素之间的欧几里得距离

如何在SQL Server中的两个交叉列之间找到以秒为单位的时间

如何使用Geocoder计算两点之间的距离(以km为单位)

如何计算Google地图上两点之间的距离(以米为单位)

如何仅使用两个变量找到两个点之间的距离,然后存储所有点并获得形状?