あなたの仮定により、参照とターゲットは(ほぼ)同じ平面上にあります。で説明されている方法「アルゴリズム 1: 平面測定」を適用できます。
アントニオ・クリミニシ "Single-View Metrology: Algorithms and Applications (Invited Paper)". 中:パターン認識。エド。リュック・ヴァン・グール著。巻。2449. コンピュータ サイエンスの講義ノート。スプリンガー ベルリン ハイデルベルグ、2002 年、224 ~ 239 ページ。
この方法を使用すると、同じ平面上にある 2 点間の距離を測定できます。
基本的
P=H*p (1)
ここp
で、 は同次座標で表される画像内の点、P
は同次座標で表される 3D ワールド プレーン内の対応する点であり、ホモグラフィ行列とH
呼ばれる 3x3 行列であり、行列ベクトルの乗算です。*
h11 h12 h13
H = h21 h22 h23
h31 h32 h33
の測定単位p
はピクセルなので、たとえばが行と列p
の点である場合、として表されます。の測定単位は、メートルなどのワールド単位です。3D ワールド平面は平面であると想定できるため、同次ベクトルとして表されます。r
c
[r,c,1]
P
Z=0
P
[X,Y,1]
「アルゴリズム 1: 平面測定」を少し変更します。は次のとおりです。
与えられた平面の画像から、画像から世界へのホモグラフィ行列 H を推定します。H の 9 つの要素は無次元であると仮定します。
画像で、参照オブジェクトに属する2 つのポイントp1=[r1,c1,1]
を選択します。p2=[r2,c2,1]
(1) を介して各イメージ ポイントをワールド平面に逆投影し、2 つのワールド ポイントP1
とを取得しP2
ます。行列とベクトルの乗算を実行し、結果のベクトルを 3 番目のコンポーネントで除算して、均一なベクトルを取得します。たとえば、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
の 9 つの要素が無次元であると仮定します。つまりX1
、Y1
、 、X2
の測定単位Y2
はピクセルです。
とのR
間の距離を計算します。つまり、は引き続きピクセルで表されます。ここで、とは参照オブジェクト上にあるので、それらの間の距離がメートル単位でわかっていることを意味します。P1
P2
R=sqrt(pow(X1-X2,2)+pow(Y1-Y2,2)
R
P1
P2
M
s
スケール係数を として計算します。s=M/R
の寸法s
はメートル/ピクセルです。
H
byの各要素を乗算し、取得した新しい行列をs
呼び出します。G
の要素はG
メートル/ピクセルで表されます。
ここで、イメージで 2 つのポイントp3
を選択p4
し、ターゲット オブジェクトに属します。
とを取得するために、p3
とp4
を逆投影します。と。再度、各ベクトルを 3 番目の要素で除算します。
、、、およびはメートル単位で表されます。G
P3
P4
P3=G*p3
P4=G*p4
P3=[X3,Y3,1]
P4=[X4,Y4,1]
X3
Y3
X4
Y4
とD
の間の目的のターゲット距離を計算します。メートルで表されるようになりました。P3
P4
D=sqrt(pow(X3-X4,2)+pow(Y3-Y4,2)
D
上記の論文の付録では、計算方法H
や OpenCV の使用方法について説明していますcv::findHomography
。基本的に、現実世界の点と画像の点の間に少なくとも 4 つの対応が必要です。
推定方法に関する別の情報源H
は、
JOHNSON、Micah K.; ファリッド、ハニー。単一の画像からの平面上のメトリック測定。部門コンピュータ。科学、ダートマス大学、技術。代表者 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 シートで、ターゲット オブジェクトは計算尺です。

測定値を含む入力画像。赤い十字は、ホモグラフィを推定するために使用されます。

修正された画像:
