変換行列を元に、画像を変形する

void warpPerspective(InputArray before, OutputArray after, InputArray matrix, Size dsize, int flags, int borderMode, const Scalar& borderValue);

引数

返り値

void warpAffine(InputArray before, OutputArray after, InputArray matrix, Size dsize, int flags, int borderMode, const Scalar& borderValue);

引数

返り値

解説

引数flagについて

引数borderModeについて

引数borderValueについて

変換方法について

サンプルコード

#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/data/lena.jpg"; const char windowName[] = "output"; const float srcPoints[] = { 0.0, 0.0,1.0, 512.0, 0.0,1.0, 512.0,512.0,1.0, 0.0,512.0,1.0}; const float dstPoints[] = {218.0,110.0,1.0, 451.0,202.0,1.0, 328.0,395.0,1.0, 112.0,313.0,1.0}; const float dstPoints2[] = {502.0,-10.0,1.0, 522.0,-10.0,1.0, 522.0, 10.0,1.0, 502.0, 10.0,1.0}; const unsigned int cPoints = 4;

void warpAffineAndShow(cv::Mat& before, cv::Mat& after, cv::Mat& matrix, cv::Size& dstSize, int flag, int borderMode = cv::BORDER_CONSTANT, int fillval = 0) {

	using namespace cv;
	// warp the image
	warpAffine(before, after, matrix, dstSize, flag, borderMode, fillval);
	imshow(windowName, after);
	waitKey(0);

}

void warpAndShow(cv::Mat& before, cv::Mat& after, cv::Mat& matrix, cv::Size& dstSize, int flag, int borderMode = cv::BORDER_CONSTANT, int fillval = 0) {

	using namespace cv;
	// warp the image
	warpPerspective(before, after, matrix, dstSize, flag, borderMode, fillval);
	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*)srcPoints);
	Mat dst        = Mat(cPoints, 3, CV_32FC1, (void*)dstPoints);
	// 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, INTER_LINEAR); 
	warpAffineAndShow(lena, warpImage, affine, dstSize, INTER_LINEAR, BORDER_REPLICATE); 
	// try various border types
	warpAndShow(lena,    warpImage, homography, dstSize, INTER_LINEAR);
	warpAndShow(lena,    warpImage, homography, dstSize, INTER_LINEAR, BORDER_REPLICATE);
	warpAndShow(lena,    warpImage, homography, dstSize, INTER_LINEAR, BORDER_WRAP);
	warpAndShow(lena,    warpImage, homography, dstSize, INTER_LINEAR, BORDER_REFLECT);
	warpAndShow(lena,    warpImage, homography, dstSize, INTER_LINEAR, BORDER_REFLECT_101);
	warpAndShow(lena.clone(), lena, homography, dstSize, INTER_LINEAR, BORDER_TRANSPARENT);
	// 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_NEAREST +WARP_INVERSE_MAP);
	warpAndShow(lena, warpImage, homography, dstSize, INTER_LINEAR  +WARP_INVERSE_MAP);
	warpAndShow(lena, warpImage, homography, dstSize, INTER_CUBIC   +WARP_INVERSE_MAP);
	warpAndShow(lena, warpImage, homography, dstSize, INTER_LANCZOS4+WARP_INVERSE_MAP);
	warpAndShow(lena, warpImage, homography, dstSize, INTER_AREA    +WARP_INVERSE_MAP);
	// check difference of BORDER_REFLECT and BORDER_REFLECT_101
	warpAndShow(lena, warpImage, homography, dstSize, INTER_NEAREST+WARP_INVERSE_MAP, BORDER_REFLECT);
	warpAndShow(lena, warpImage, homography, dstSize, INTER_NEAREST+WARP_INVERSE_MAP, BORDER_REFLECT_101);
	destroyAllWindows();
	return 0;

} }}

実行結果

実体ファイル

OpenCV 2.4系列

OpenCV 3.0系列

ジャンル:OpenCV:OpenCV 2.4:OpenCV 3.0:OpenCV 3.1準拠


添付ファイル: fileresult0006PerspectiveReflect101.png 1136件 [詳細] fileresult0007PerspectiveTransparent.png 1107件 [詳細] fileresult0008InterpolationNearest.png 1212件 [詳細] fileresult0009InterpolationLinear.png 1196件 [詳細] fileresult0010InterpolationCubic.png 1153件 [詳細] fileresult0011InterpolationLanczos4.png 1207件 [詳細] fileresult0012InterpolationArea.png 1201件 [詳細] fileresult0013BorderReflect.png 1203件 [詳細] fileresult0014BorderReflect101.png 1244件 [詳細] fileresult0000AffineConstant.png 1183件 [詳細] fileresult0001AffineReplicate.png 1165件 [詳細] fileresult0002PerspectiveConstant.png 1158件 [詳細] fileresult0004PerspectiveWrap.png 1160件 [詳細] fileresult0003PerspectiveReplicate.png 1212件 [詳細] fileresult0005PerspectiveReflect.png 1194件 [詳細]

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-01-15 (金) 10:50:33