ホモグラフィによる画像の変換
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
変換行列を元に、画像を変形する
#contents
*void warpPerspective(InputArray before, OutputArray afte...
-beforeをMatrix(ホモグラフィ)に基づいて変換
**引数 [#y819908b]
-before:Mat型の画像。座標変換前の画像
-after:Mat型の画像。座標変換後の画像
-matrix:Mat型の変換行列(ホモグラフィ)
-dsize:Size型の出力画像のサイズ
-flag:int型で変換方法のフラグ。詳細は後述。省略した場合は...
-borderMode:int型の、境界の対処方法。省略した場合はBORDER...
-borderValue:Scalar型の値。WARP_FILL_OUTLIERSを指定した場...
**返り値 [#a16c0fa4]
-void型なのでなし
*void warpAffine(InputArray before, OutputArray after, In...
-beforeをMatrix(アフィン行列)に基いて変換
**引数 [#b02d976e]
-基本的にwarpPerspectiveと同じ
-before:Mat型の画像。座標変換前の画像
-after:Mat型の画像。座標変換後の画像
-matrix:Mat型の変換行列。2x3(2 rows , 3 columns)の変換行列
-dsize:Size型の出力画像のサイズ
-flag:int型で変換方法のフラグ。詳細は後述。省略した場合は...
-borderMode:int型の、境界の対処方法。省略した場合はBORDER...
-borderValue:Scalar型の値。WARP_FILL_OUTLIERSを指定した場...
**返り値 [#b2663204]
-void型なのでなし
*解説 [#caf29950]
-flag、borderMode、borderValueの引数については下記の通り
**引数flagについて [#gb8eeeee]
-flagのデフォルト値はINTER_LINEAR。取れる値は以下の通り
--INTER_NEAREST:最近傍補間(0)
--INTER_LINEAR:線型補間(1)
--INTER_CUBIC:バイキュービック補間(2)
--INTER_LANCZOS4:Lanczos補間(4)
--WARP_INVERSE_MAP:逆変換による補間(16)
--INTER_AREAを指定すると、INTER_LINEARに置き換えられる。
-flagsは基本的に補間方法を指定する
--補間方法による違いはサンプルコードの実行結果参照
-WARP_INVERSE_MAPは、ホモグラフィ、もしくはアフィン変換を...
-WARP_FILL_OUTLIERSは、C時代の名残。指定されても、内部で...
--代わりに、borderModeのBORDER_CONSTANTを使え、ということ...
--ちなみに両関数ともCインタフェースはC++インタフェースの...
**引数borderModeについて [#t2d621ad]
-borderModeのデフォルト値はBORDER_CONSTANT。取れる値は以...
--BORDER_CONSTANT:指定した値(borderValue)で塗りつぶす(0)
--BORDER_REPLICATE:縁の画素値を、繰り返し並べる(1)
--BORDER_REFLECT:同じ画像を、反転させながら並べる(2)
--BORDER_WRAP:同じ画像を繰り返し並べる(3)
--BORDER_REFLECT_101:同じ画像を反転させながら並べる(4)
--BORDER_TRANSPARENT:画像の外側には触れない(5)
-画像を変換させるので、もともとの画像の外側、縁をどう扱う...
-各borderModeで生成されうる画像についてはサンプルコードの...
-BORDER_REFLECTとBORDER_REFLECT_101は非常に似た挙動を示す...
--BORDER_REFLECTは、縁の画素が繰り返されるため、2度現れる
--BORDER_REFLECT_101は、縁の画素は繰り返されない
--サンプルコードで、この挙動の違いが見えるので、参照のこと
-以下2つの定義も存在するが、エイリアスである
--BORDER_REFLECT101とBORDER_DEFAULT、BORDER_REFLECT_101の...
--ちなみに省略した場合はBORDER_CONSTANTであり、BORDER_DEF...
--このenum定数群を使うのはwarpPerspectiveやwarpAffine以外...
**引数borderValueについて [#vc9bc3b6]
-borderValueのデフォルトは黒(Scalar())
**変換方法について [#ka29d2bb]
-ホモグラフィを用いて変換する場合はwarpPerspectiveを、ア...
-warpPerspectiveは以下の数式に基づいて変換を行う
--&mimetex(\left(\begin{array}{c} x^{\prime} \\ y^{\prime...
--書き換えると、ヘッダ/ドキュメントにある様な、下記の式に...
--&mimetex(\left(\begin{array}{c} x^{\prime} \\ y^{\prime...
-warpAffineは以下の数式に基づいて変換を行う
--&mimetex(\left(\begin{array}{c} x^{\prime} \\ y^{\prime...
--書き換えると、ヘッダ/ドキュメントにある様な、下記の式に...
--&mimetex(\left(\begin{array}{c} x^{\prime} \\ y^{\prime...
*サンプルコード [#pde0c698]
#geshi(c++){{
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
const char filename[] = "/path/to/opencv/samples/dat...
const char windowName[] = "output";
const float srcPoints[] = { 0.0, 0.0,1.0, 512.0, 0....
const float dstPoints[] = {218.0,110.0,1.0, 451.0,202....
const float dstPoints2[] = {502.0,-10.0,1.0, 522.0,-10....
const unsigned int cPoints = 4;
void warpAffineAndShow(cv::Mat& before, cv::Mat& after, c...
{
using namespace cv;
// warp the image
warpAffine(before, after, matrix, dstSize, flag, borderM...
imshow(windowName, after);
waitKey(0);
}
void warpAndShow(cv::Mat& before, cv::Mat& after, cv::Mat...
{
using namespace cv;
// warp the image
warpPerspective(before, after, matrix, dstSize, flag, bo...
imshow(windowName, after);
waitKey(0);
}
int main(int argc, char **argv)
{
using namespace cv;
Mat lena = imread(filename, IMREAD_GRAYSCALE);
// prepare the corresponding points
Mat src = Mat(cPoints, 3, CV_32FC1, (void*)srcPoi...
Mat dst = Mat(cPoints, 3, CV_32FC1, (void*)dstPoi...
// find the homography
Mat homography = findHomography(src, dst);
// prepare the result image
Mat warpImage;
Size dstSize = lena.size();
// make a window
namedWindow(windowName);
// warp the image using homography
warpPerspective(lena, warpImage, homography, dstSize);
// show the result
imshow(windowName, warpImage);
// wait
waitKey(0);
// cut out 3 points from corresponding points
// Affine matrix only acceps 3 x 2 matrix
Mat src1 = src.colRange(0,2).rowRange(0,3).clone();
Mat dst1 = dst.colRange(0,2).rowRange(0,3).clone();
// get affine matrix
Mat affine = getAffineTransform(src1, dst1);
// warp the image using affine matrix
warpAffineAndShow(lena, warpImage, affine, dstSize, INTE...
warpAffineAndShow(lena, warpImage, affine, dstSize, INTE...
// try various border types
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena.clone(), lena, homography, dstSize, INT...
// find the homography
dst = Mat(cPoints, 3, CV_32FC1, (void*)dstPoints2);
homography = findHomography(src, dst);
// try various interpolation method
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
// check difference of BORDER_REFLECT and BORDER_REFLECT...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
destroyAllWindows();
return 0;
}
}}
**実行結果 [#tff79c15]
-TBW
#ref(result0000AffineConstant.png)
#ref(result0001AffineReplicate.png)
#ref(result0002PerspectiveConstant.png)
#ref(result0003PerspectiveReplicate.png)
#ref(result0004PerspectiveWrap.png)
#ref(result0005PerspectiveReflect.png)
#ref(result0006PerspectiveReflect101.png)
#ref(result0007PerspectiveTransparent.png)
#ref(result0008InterpolationNearest.png)
#ref(result0009InterpolationLinear.png)
#ref(result0010InterpolationCubic.png)
#ref(result0011InterpolationLanczos4.png)
#ref(result0012InterpolationArea.png)
#ref(result0013BorderReflect.png)
#ref(result0014BorderReflect101.png)
*実体ファイル [#me219ce5]
**OpenCV 2.4系列 [#b7c56afd]
-warpPerspective、warpAffine
--modules/imgproc/include/opencv2/imgproc/iimgproc.hpp
--modules/imgproc/src/imgwarp.cpp
**OpenCV 3.0系列 [#fe0ecd83]
-warpPerspective、warpAffine
--modules/imgproc/include/opencv2/imgproc/iimgproc.hpp
--modules/imgproc/src/imgwarp.cpp
ジャンル[[:OpenCV]][[:OpenCV 2.4]][[:OpenCV 3.0]][[:OpenC...
終了行:
変換行列を元に、画像を変形する
#contents
*void warpPerspective(InputArray before, OutputArray afte...
-beforeをMatrix(ホモグラフィ)に基づいて変換
**引数 [#y819908b]
-before:Mat型の画像。座標変換前の画像
-after:Mat型の画像。座標変換後の画像
-matrix:Mat型の変換行列(ホモグラフィ)
-dsize:Size型の出力画像のサイズ
-flag:int型で変換方法のフラグ。詳細は後述。省略した場合は...
-borderMode:int型の、境界の対処方法。省略した場合はBORDER...
-borderValue:Scalar型の値。WARP_FILL_OUTLIERSを指定した場...
**返り値 [#a16c0fa4]
-void型なのでなし
*void warpAffine(InputArray before, OutputArray after, In...
-beforeをMatrix(アフィン行列)に基いて変換
**引数 [#b02d976e]
-基本的にwarpPerspectiveと同じ
-before:Mat型の画像。座標変換前の画像
-after:Mat型の画像。座標変換後の画像
-matrix:Mat型の変換行列。2x3(2 rows , 3 columns)の変換行列
-dsize:Size型の出力画像のサイズ
-flag:int型で変換方法のフラグ。詳細は後述。省略した場合は...
-borderMode:int型の、境界の対処方法。省略した場合はBORDER...
-borderValue:Scalar型の値。WARP_FILL_OUTLIERSを指定した場...
**返り値 [#b2663204]
-void型なのでなし
*解説 [#caf29950]
-flag、borderMode、borderValueの引数については下記の通り
**引数flagについて [#gb8eeeee]
-flagのデフォルト値はINTER_LINEAR。取れる値は以下の通り
--INTER_NEAREST:最近傍補間(0)
--INTER_LINEAR:線型補間(1)
--INTER_CUBIC:バイキュービック補間(2)
--INTER_LANCZOS4:Lanczos補間(4)
--WARP_INVERSE_MAP:逆変換による補間(16)
--INTER_AREAを指定すると、INTER_LINEARに置き換えられる。
-flagsは基本的に補間方法を指定する
--補間方法による違いはサンプルコードの実行結果参照
-WARP_INVERSE_MAPは、ホモグラフィ、もしくはアフィン変換を...
-WARP_FILL_OUTLIERSは、C時代の名残。指定されても、内部で...
--代わりに、borderModeのBORDER_CONSTANTを使え、ということ...
--ちなみに両関数ともCインタフェースはC++インタフェースの...
**引数borderModeについて [#t2d621ad]
-borderModeのデフォルト値はBORDER_CONSTANT。取れる値は以...
--BORDER_CONSTANT:指定した値(borderValue)で塗りつぶす(0)
--BORDER_REPLICATE:縁の画素値を、繰り返し並べる(1)
--BORDER_REFLECT:同じ画像を、反転させながら並べる(2)
--BORDER_WRAP:同じ画像を繰り返し並べる(3)
--BORDER_REFLECT_101:同じ画像を反転させながら並べる(4)
--BORDER_TRANSPARENT:画像の外側には触れない(5)
-画像を変換させるので、もともとの画像の外側、縁をどう扱う...
-各borderModeで生成されうる画像についてはサンプルコードの...
-BORDER_REFLECTとBORDER_REFLECT_101は非常に似た挙動を示す...
--BORDER_REFLECTは、縁の画素が繰り返されるため、2度現れる
--BORDER_REFLECT_101は、縁の画素は繰り返されない
--サンプルコードで、この挙動の違いが見えるので、参照のこと
-以下2つの定義も存在するが、エイリアスである
--BORDER_REFLECT101とBORDER_DEFAULT、BORDER_REFLECT_101の...
--ちなみに省略した場合はBORDER_CONSTANTであり、BORDER_DEF...
--このenum定数群を使うのはwarpPerspectiveやwarpAffine以外...
**引数borderValueについて [#vc9bc3b6]
-borderValueのデフォルトは黒(Scalar())
**変換方法について [#ka29d2bb]
-ホモグラフィを用いて変換する場合はwarpPerspectiveを、ア...
-warpPerspectiveは以下の数式に基づいて変換を行う
--&mimetex(\left(\begin{array}{c} x^{\prime} \\ y^{\prime...
--書き換えると、ヘッダ/ドキュメントにある様な、下記の式に...
--&mimetex(\left(\begin{array}{c} x^{\prime} \\ y^{\prime...
-warpAffineは以下の数式に基づいて変換を行う
--&mimetex(\left(\begin{array}{c} x^{\prime} \\ y^{\prime...
--書き換えると、ヘッダ/ドキュメントにある様な、下記の式に...
--&mimetex(\left(\begin{array}{c} x^{\prime} \\ y^{\prime...
*サンプルコード [#pde0c698]
#geshi(c++){{
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
const char filename[] = "/path/to/opencv/samples/dat...
const char windowName[] = "output";
const float srcPoints[] = { 0.0, 0.0,1.0, 512.0, 0....
const float dstPoints[] = {218.0,110.0,1.0, 451.0,202....
const float dstPoints2[] = {502.0,-10.0,1.0, 522.0,-10....
const unsigned int cPoints = 4;
void warpAffineAndShow(cv::Mat& before, cv::Mat& after, c...
{
using namespace cv;
// warp the image
warpAffine(before, after, matrix, dstSize, flag, borderM...
imshow(windowName, after);
waitKey(0);
}
void warpAndShow(cv::Mat& before, cv::Mat& after, cv::Mat...
{
using namespace cv;
// warp the image
warpPerspective(before, after, matrix, dstSize, flag, bo...
imshow(windowName, after);
waitKey(0);
}
int main(int argc, char **argv)
{
using namespace cv;
Mat lena = imread(filename, IMREAD_GRAYSCALE);
// prepare the corresponding points
Mat src = Mat(cPoints, 3, CV_32FC1, (void*)srcPoi...
Mat dst = Mat(cPoints, 3, CV_32FC1, (void*)dstPoi...
// find the homography
Mat homography = findHomography(src, dst);
// prepare the result image
Mat warpImage;
Size dstSize = lena.size();
// make a window
namedWindow(windowName);
// warp the image using homography
warpPerspective(lena, warpImage, homography, dstSize);
// show the result
imshow(windowName, warpImage);
// wait
waitKey(0);
// cut out 3 points from corresponding points
// Affine matrix only acceps 3 x 2 matrix
Mat src1 = src.colRange(0,2).rowRange(0,3).clone();
Mat dst1 = dst.colRange(0,2).rowRange(0,3).clone();
// get affine matrix
Mat affine = getAffineTransform(src1, dst1);
// warp the image using affine matrix
warpAffineAndShow(lena, warpImage, affine, dstSize, INTE...
warpAffineAndShow(lena, warpImage, affine, dstSize, INTE...
// try various border types
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena, warpImage, homography, dstSize, INT...
warpAndShow(lena.clone(), lena, homography, dstSize, INT...
// find the homography
dst = Mat(cPoints, 3, CV_32FC1, (void*)dstPoints2);
homography = findHomography(src, dst);
// try various interpolation method
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
// check difference of BORDER_REFLECT and BORDER_REFLECT...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
warpAndShow(lena, warpImage, homography, dstSize, INTER_...
destroyAllWindows();
return 0;
}
}}
**実行結果 [#tff79c15]
-TBW
#ref(result0000AffineConstant.png)
#ref(result0001AffineReplicate.png)
#ref(result0002PerspectiveConstant.png)
#ref(result0003PerspectiveReplicate.png)
#ref(result0004PerspectiveWrap.png)
#ref(result0005PerspectiveReflect.png)
#ref(result0006PerspectiveReflect101.png)
#ref(result0007PerspectiveTransparent.png)
#ref(result0008InterpolationNearest.png)
#ref(result0009InterpolationLinear.png)
#ref(result0010InterpolationCubic.png)
#ref(result0011InterpolationLanczos4.png)
#ref(result0012InterpolationArea.png)
#ref(result0013BorderReflect.png)
#ref(result0014BorderReflect101.png)
*実体ファイル [#me219ce5]
**OpenCV 2.4系列 [#b7c56afd]
-warpPerspective、warpAffine
--modules/imgproc/include/opencv2/imgproc/iimgproc.hpp
--modules/imgproc/src/imgwarp.cpp
**OpenCV 3.0系列 [#fe0ecd83]
-warpPerspective、warpAffine
--modules/imgproc/include/opencv2/imgproc/iimgproc.hpp
--modules/imgproc/src/imgwarp.cpp
ジャンル[[:OpenCV]][[:OpenCV 2.4]][[:OpenCV 3.0]][[:OpenC...
ページ名: