数学の道-python計算実戦(10)-機械視覚-透視投影

2801 ワード

パース投影
3 D計算機グラフィックスにおけるもう一つの重要な変換は透視投影である.平行投影が平行線に沿って物体を画像平面に投影するのとは異なり、透視投影は投影中心という点から出る直線に従って物体を画像平面に投影する.これは、投影中心から遠いほど投影が小さくなり、距離が近いほど投影が大きくなることを意味します.
最も単純なパース投影は、投影中心を座標の原点とし、z=1を画像平面とし、投影を変換します.を選択します.
△この乗算の計算結果は=です.
乗算後、通常、整列要素wcは1ではないので、実平面にマッピングするには、各要素をwcで除算する整列除算が必要です.
より複雑なパース投影は、回転、スケール、平行移動、切断などと組み合わせて画像を変換することができる.
pythonでopencvを呼び出し、次の関数でパース投影を完了します.
transform_matrix=cv2.getPerspectiveTransform(src,dst)
print transform_matrix
#        
newimg=cv2.warpPerspective(img,transform_matrix,(w,h))
cv2.imshow('preview',newimg)

cv2.waitKey()
cv2.destroyAllWindows()

当ブログのすべての内容はオリジナルで、もし転載するならば出所を明記してください
http://blog.csdn.net/myhaspl/
WarpPerspective
画像をピボット変換する
void cvWarpPerspective( const CvArr* src, CvArr* dst, const CvMat* map_matrix,
                        int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,
                        CvScalar fillval=cvScalarAll(0) );

src
入力画像
dst
画像を出力する
map_matrix
3×3変換マトリクス
flags
補間方法と次のスイッチオプションを組み合わせます.
CV_WARP_FILL_OUTIERS-縮小画像のすべての画素を埋めます.入力画像の境界外に部分画素が位置する場合、その値はfillvalに設定.
CV_WARP_INVERSE_MAP-指定matrixは、出力画像から入力画像への逆変換であるため、直接画素補間に使用できます.そうでなければ、関数はmap_からmatrixは逆変換を得た.
fillval
境界の外側を塗りつぶす値
Python:   cv2. warpPerspective (src, M, dsize
[, dst
[, flags
[, borderMode
[, borderValue
]
]
]
] ) → dst
C:  void  cvWarpPerspective (const CvArr* 
src, CvArr* 
dst, const CvMat*
map_matrix, int 
flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, CvScalar
fillval=cvScalarAll(0)  )
Parameters:
src – input image.
dst – output image that has the size dsize and the same type assrc .
M –  transformation matrix.
dsize – size of the output image.
flags – combination of interpolation methods (INTER_LINEAR orINTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that setsM as the inverse transformation (  ).
borderMode – pixel extrapolation method (BORDER_CONSTANT orBORDER_REPLICATE).
borderValue – value used in case of a constant border; by default, it equals 0.
The function warpPerspective transforms the source image using the specified matrix:
GetPerspectiveTransform
四角形の4点から透過変換を計算する
CvMat* cvGetPerspectiveTransform( const CvPoint2D32f* src, const CvPoint2D32f* dst,
                                  CvMat* map_matrix );

#define cvWarpPerspectiveQMatrix cvGetPerspectiveTransform

src
画像の四角形の頂点座標を入力します.
dst
画像の対応する四角形頂点座標を出力します.
map_matrix
指向3×3行列のポインタを出力します.
関数cvGetPerspectiveTransformは、次の関係を満たす透過変換行列を計算します.
ここで、dst(i)=(x'i,y'i)、src(i)=(xi,yi)、i=0..3.