【问题标题】:Measure size of object using reference object in photo使用照片中的参考对象测量对象的大小
【发布时间】:2016-02-22 13:50:49
【问题描述】:

我想计算照片中对象的大小,其中包括目标对象和参考对象

我想我想做的就是这个软件实现的(我不知道这个软件有多精确) https://itunes.apple.com/us/app/photo-meter-picture-measuring/id579961082?mt=8

我已经发现,一般来说,它被称为摄影测量,似乎是活跃的研究领域。

How would you find the height of objects given an image? https://physics.stackexchange.com/questions/151121/can-i-calculate-the-size-of-a-real-object-by-just-looking-at-the-picture-taken-b

但是,我找不到

  • 使用参考对象测量照片中对象的基本方法是什么
  • 一种实现方式或标准开源方式。

更新

  • 我无法利用对象与相机的距离。
  • 参考和目标在(大约)同一平面上。

【问题讨论】:

  • 你知道物体和参照物到相机的距离吗?
  • @photon 我无法利用物体的距离和相机的参考。我已将其添加到我的问题中。
  • 如果你对距离一无所知,一般情况下是不可能的。看到这个:s-media-cache-ak0.pinimg.com/236x/c2/2f/fb/…
  • @Photon 啊,对不起。我忘了写非常重要的假设。参考和目标在(大约)同一平面上。
  • 在这种情况下,以像素为单位的高度比率与以实际单位为单位的高度比率相同。

标签: image-processing open-source photogrammetry


【解决方案1】:

由于您的假设参考和目标位于(大约)同一平面上。您可以应用

中描述的“算法 1:平面测量”方法

Antonio Criminisi. "Single-View Metrology: Algorithms and Applications (Invited Paper)". In: Pattern Recognition. Ed. by Luc Van Gool. Vol. 2449. Lecture Notes in Computer Science. Springer Berlin Heidelberg, 2002, pp. 224-239.

该方法允许您测量位于同一平面上的两点之间的距离。

基本上

P=H*p (1)

其中p 是图像中以齐次坐标表示的点,P 是 3D 世界平面中的对应点,也以齐次坐标表示,H 是称为单应性的 3x3 矩阵矩阵* 是矩阵-向量乘法。

    h11 h12 h13
H = h21 h22 h23
    h31 h32 h33

p 的度量单位是像素,例如,如果pr 行和c 列上的一个点,它将表示为[r,c,1]P的度量单位是你的世界单位,例如米,你可以假设你的3D世界平面是平面Z=0,所以P表示为齐次向量[X,Y,1]

所以对“算法 1:平面测量”稍作修改。如下:

  1. 给定一个平面图像,估计图像到世界的单应矩阵 H。假设 H 的 9 个元素是无量纲的。

  2. 在图像中选择两个点p1=[r1,c1,1]p2=[r2,c2,1]属于参考对象。

  3. 通过(1)将每个图像点反向投影到世界平面中,以获得两个世界点P1P2。您进行矩阵向量乘法,然后将结果向量除以第三个分量,以获得齐次向量。 例如P1=[X1,Y1,1]P1=[(c1*h_12 + h_11*r1 + h_13)/(c1*h_32 + h_31*r1 + h_33),(c1*h_22 + h_21*r1 + h_23)/(c1*h_32 + h_31*r1 + h_33),1]。暂时假设H的九个元素是无量纲的,也就是说X1Y1X2Y2的度量单位是像素。

  4. 计算P1P2之间的距离RR=sqrt(pow(X1-X2,2)+pow(Y1-Y2,2)R仍以像素表示。现在,由于P1P2 在参考对象上,这意味着您知道它们之间的距离(以米为单位),我们称该距离为以米为单位的M

  5. 计算比例因子ss=M/Rs的尺寸是每像素米。

  6. H 的每个元素乘以s 并调用G 得到的新矩阵。现在G 的元素以每像素米表示。

  7. 现在,在图像中选择两个点p3p4 属于目标对象。

  8. 通过G 反向项目p3p4 以获得P3P4P3=G*p3P4=G*p4。再次将每个向量除以其第三个元素。 P3=[X3,Y3,1]P4=[X4,Y4,1] 以及现在的 X3Y3X4Y4 以米表示。

  9. 计算P3P4之间的目标距离D,即D=sqrt(pow(X3-X4,2)+pow(Y3-Y4,2)D 现在以米表示。

上述论文的附录解释了如何计算H,或者例如你可以使用OpenCV cv::findHomography:基本上你需要在现实世界中的点和图像中的点之间至少有四个对应关系。

关于如何估计H 的另一个信息来源在

JOHNSON, Micah K.; FARID, Hany. Metric measurements on a plane from a single image. Dept. Comput. Sci., Dartmouth College, Tech. Rep. TR2006-579, 2006.

如果您还需要估计测量的准确性,您可以在

中找到详细信息

A.克里米尼西。 来自单个和多个未校准图像的准确视觉计量。杰出论文系列。 Springer-Verlag London Ltd.,2001 年 9 月。ISBN:1852334681。

使用 OpenCV 的 C++ 示例:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"


void to_homogeneous(const std::vector< cv::Point2f >& non_homogeneous, std::vector< cv::Point3f >& homogeneous )
{
    homogeneous.resize(non_homogeneous.size());
    for ( size_t i = 0; i < non_homogeneous.size(); i++ ) {
        homogeneous[i].x = non_homogeneous[i].x;
        homogeneous[i].y = non_homogeneous[i].y;
        homogeneous[i].z = 1.0;
    }
}

void from_homogeneous(const std::vector< cv::Point3f >& homogeneous, std::vector< cv::Point2f >& non_homogeneous )
{
    non_homogeneous.resize(homogeneous.size());
    for ( size_t i = 0; i < non_homogeneous.size(); i++ ) {
        non_homogeneous[i].x = homogeneous[i].x / homogeneous[i].z;
        non_homogeneous[i].y = homogeneous[i].y / homogeneous[i].z;
    }
}

void draw_cross(cv::Mat &img, const cv::Point center, float arm_length, const cv::Scalar &color, int thickness = 5 )
{
    cv::Point N(center - cv::Point(0, arm_length));
    cv::Point S(center + cv::Point(0, arm_length));
    cv::Point E(center + cv::Point(arm_length, 0));
    cv::Point W(center - cv::Point(arm_length, 0));
    cv::line(img, N, S, color, thickness);
    cv::line(img, E, W, color, thickness);
}

double measure_distance(const cv::Point2f& p1, const cv::Point2f& p2, const cv::Matx33f& GG)
{
    std::vector< cv::Point2f > ticks(2);
    ticks[0] = p1;
    ticks[1] = p2;
    std::vector< cv::Point3f > ticks_h;
    to_homogeneous(ticks, ticks_h);

    std::vector< cv::Point3f > world_ticks_h(2);
    for ( size_t i = 0; i < ticks_h.size(); i++ ) {
        world_ticks_h[i] = GG * ticks_h[i];
    }
    std::vector< cv::Point2f > world_ticks_back;
    from_homogeneous(world_ticks_h, world_ticks_back);

    return cv::norm(world_ticks_back[0] - world_ticks_back[1]);
}

int main(int, char**)
{
    cv::Mat img = cv::imread("single-view-metrology.JPG");
    std::vector< cv::Point2f > world_tenth_of_mm;
    std::vector< cv::Point2f > img_px;

    // Here I manually picked the pixels coordinates of the corners of the A4 sheet.
    cv::Point2f TL(711, 64);
    cv::Point2f BL(317, 1429);
    cv::Point2f TR(1970, 175);
    cv::Point2f BR(1863, 1561);

    // This is the standard size of the A4 sheet:
    const int A4_w_mm = 210;
    const int A4_h_mm = 297;
    const int scale = 10;

    // Here I create the correspondences between the world point and the
    // image points.
    img_px.push_back(TL);
    world_tenth_of_mm.push_back(cv::Point2f(0.0, 0.0));

    img_px.push_back(TR);
    world_tenth_of_mm.push_back(cv::Point2f(A4_w_mm * scale, 0.0));

    img_px.push_back(BL);
    world_tenth_of_mm.push_back(cv::Point2f(0.0, A4_h_mm * scale));

    img_px.push_back(BR);
    world_tenth_of_mm.push_back(cv::Point2f(A4_w_mm * scale, A4_h_mm * scale));

    // Here I estimate the homography that brings the world to the image.
    cv::Mat H = cv::findHomography(world_tenth_of_mm, img_px);

    // To back-project the image points into the world I need the inverse of the homography.
    cv::Mat G = H.inv();

    // I can rectify the image.
    cv::Mat warped;
    cv::warpPerspective(img, warped, G, cv::Size(2600, 2200 * 297 / 210));

    {
        // Here I manually picked the pixels coordinates of ticks '0' and '1' in the slide rule,
        // in the world the distance between them is 10mm.
        cv::Point2f tick_0(2017, 1159);
        cv::Point2f tick_1(1949, 1143);
        // I measure the distance and I write it on the image.
        std::ostringstream oss;
        oss << measure_distance(tick_0, tick_1, G) / scale;
        cv::line(img, tick_0, tick_1, CV_RGB(0, 255, 0));
        cv::putText(img, oss.str(), (tick_0 + tick_1) / 2, cv::FONT_HERSHEY_PLAIN, 3, CV_RGB(0, 255, 0), 3);
    }

    {
        // Here I manually picked the pixels coordinates of ticks '11' and '12' in the slide rule,
        // in the world the distance between them is 10mm.
        cv::Point2f tick_11(1277, 988);
        cv::Point2f tick_12(1211, 973);
        // I measure the distance and I write it on the image.
        std::ostringstream oss;
        oss << measure_distance(tick_11, tick_12, G) / scale;
        cv::line(img, tick_11, tick_12, CV_RGB(0, 255, 0));
        cv::putText(img, oss.str(), (tick_11 + tick_12) / 2, cv::FONT_HERSHEY_PLAIN, 3, CV_RGB(0, 255, 0), 3);
    }

    // I draw the points used in the estimate of the homography.
    draw_cross(img, TL, 40, CV_RGB(255, 0, 0));
    draw_cross(img, TR, 40, CV_RGB(255, 0, 0));
    draw_cross(img, BL, 40, CV_RGB(255, 0, 0));
    draw_cross(img, BR, 40, CV_RGB(255, 0, 0));

    cv::namedWindow( "Input image", cv::WINDOW_NORMAL );
    cv::imshow( "Input image", img );
    cv::imwrite("img.png", img);

    cv::namedWindow( "Rectified image", cv::WINDOW_NORMAL );
    cv::imshow( "Rectified image", warped );
    cv::imwrite("warped.png", warped);

    cv::waitKey(0);

    return 0;
}

输入图像,在这种情况下,您的参考对象是 A4 纸,目标对象是计算尺:

带有度量的输入图像,红叉用于估计单应性:

修正后的图像:

【讨论】:

    【解决方案2】:

    最基本的方法是简单地确定参考对象的像素尺寸,并使用公制尺寸得出毫米/像素因子。然后将要测量的对象的像素尺寸乘以该因子。 仅当两个对象都位于与传感器平行的同一平面内时,这才有效。

    对于其他任何您需要距离信息的内容。您可以衡量它们,也可以根据自己的准确度做出一些假设。

    实施此类测量的方式取决于您的要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多