【问题标题】:How to find the distance(physical in m and pixels) between two object(points) in image in OpenCV and C+++?如何在 OpenCV 和 C++ 中找到图像中两个对象(点)之间的距离(物理单位为 m 和像素)?
【发布时间】:2017-06-28 12:25:35
【问题描述】:

我正在使用 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);
      }

      }
...

所以只需要一个代码来获取这两个中心矩(图像中对象的两个中心)之间的距离(实数以厘米或米为单位,以像素为单位)。我在矩形窗口(窗口中的红点)上包含中心的结果图像。所以在这种情况下,我想计算窗口和“OPEN”方框标志之间的距离。

【问题讨论】:

  • 不清楚。您发布的代码与您打算完成的任务之间有什么关系?您要求同时提供公制和像素距离。对于后者,如果您有中心的坐标,这不是很明显吗?对于公制距离,这在理论上用一台相机是不可能的,除非您知道相机和物体之间的距离。您能否编辑问题并澄清一下?
  • 好的。代码是找到物体的中心。在这种情况下是飞机门窗。清除?好的,是的,如果两个中心的像素坐标比它容易。我的问题是我指出已经知道相机和物体之间的距离。我说我有相机和物体之间的距离,我知道事先的信息,因为可以用激光测量那个距离。
  • 现在清楚了吗?所以基本上只需要找到公制距离,知道相机和物体之间的距离。好吗?
  • 我仍然不清楚:为什么不进行类似的操作以获取“开放”标志的边界矩形,然后检查两个矩形之间的距离?或者,如果您找不到它,您可以搜索图像中的白色?提示:除了发布代码之外,您还应该用几行描述您正在实现的算法,这将有助于回答您的问题。
  • 我没有问题得到“开放”标志的边界矩形。也没有任何问题找到它。只是没有附上代码。我的问题不在于。我的问题是获取“打开”标志和门窗之间的度量(实际物理距离)。我又一次知道了相机和物体之间的距离。现在清楚了吗???

标签: c++ opencv euclidean-distance


【解决方案1】:

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

如果:

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

那么这两个点之间的度量距离可以通过使用投影基础来计算:

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

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

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

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

【讨论】:

  • 是的,我明白了
猜你喜欢
  • 2013-10-09
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 2015-07-17
相关资源
最近更新 更多