gpt4 book ai didi

image-processing - 使用照片中的引用对象测量对象的大小

转载 作者:行者123 更新时间:2023-12-04 19:01:55 24 4
gpt4 key购买 nike

我想计算照片中对象的大小,其中包括目标对象和引用对象。

我想我想做的是这个软件实现的(我不知道这个软件有多精确)
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

但是,我找不到

  • 用引用物体测量照片中物体的基本方法是什么。
  • 一种实现它的方法或它的标准开源。

  • 更新
  • 我无法利用物体的距离和相机的引用。
  • 引用和目标位于(大约)同一平面上。
  • 最佳答案

    由于您的假设引用和目标在(大约)同一平面上。您可以应用中描述的方法“算法1:平面测量”

    Antonio Criminisi. "Single-View Metrology: Algorithms and Applications(Invited Paper)". In: Pattern Recognition. Ed. by LucVan Gool. Vol. 2449. Lecture Notes in Computer Science. SpringerBerlin 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的计量单位是像素,例如如果 p是行 r 上的一个点和专栏 c它将表示为 [r,c,1] . P的计量单位是您的世界单位,例如米,您可以假设您的 3D 世界平面是平面 Z=0等等 P表示为齐次向量 [X,Y,1] .

    所以对“算法 1:平面测量”稍作修改。如下:
  • 给定一个平面图像,估计图像到世界的单应矩阵 H。假设 H 的 9 个元素是无量纲的。
  • 在图像中,选择两个点 p1=[r1,c1,1]p2=[r2,c2,1]属于引用对象。
  • 通过(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 的九个元素是无量纲的,这意味着 X1 的度量单位, Y1 , X2 , Y2是像素。
  • 计算距离 R之间P1P2R=sqrt(pow(X1-X2,2)+pow(Y1-Y2,2) , R仍然以像素表示。现在,由于 P1P2在引用对象上,这意味着您知道它们之间的距离(以米为单位),我们称该距离,以米为单位,M .
  • 计算比例因子 ss=M/R ,尺寸s是每像素米。
  • H 的每个元素相乘来自 s并调用 G你得到的新矩阵。现在G的元素以米/像素表示。
  • 现在,在图像中选择两个点 p3p4属于目标对象。
  • 背投 p3p4通过 G为了得到P3P4 . P3=G*p3P4=G*p4 .再次将每个向量除以它的第三个元素。P3=[X3,Y3,1]P4=[X4,Y4,1]现在 X3 , Y3 , X4Y4以米表示。
  • 计算所需的目标距离 D之间P3P4D=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 纸,目标对象是计算尺:
    enter image description here

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

    修正后的图像:
    enter image description here

    关于image-processing - 使用照片中的引用对象测量对象的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35555375/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com