変換行列を元に、画像を変形する
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について †
変換方法について †
サンプルコード †#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系列 †
|