Update warpPerspective_demo.cpp

This commit is contained in:
Suleyman TURKMEN 2020-08-02 13:33:23 +03:00 committed by sturkmen72
parent f162c08cda
commit c262eea84a
2 changed files with 13 additions and 8 deletions

View File

@ -2263,7 +2263,7 @@ CV_EXPORTS_W void warpAffine( InputArray src, OutputArray dst,
const Scalar& borderValue = Scalar());
/** @example samples/cpp/warpPerspective_demo.cpp
An example program shows using cv::findHomography and cv::warpPerspective for image warping
An example program shows using cv::getPerspectiveTransform and cv::warpPerspective for image warping
*/
/** @brief Applies a perspective transformation to an image.

View File

@ -8,7 +8,6 @@
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/calib3d.hpp"
#include <iostream>
using namespace std;
@ -36,6 +35,7 @@ Mat warping(Mat image, Size warped_image_size, vector< Point2f> srcPoints, vecto
String windowTitle = "Perspective Transformation Demo";
String labels[4] = { "TL","TR","BR","BL" };
vector< Point2f> roi_corners;
vector< Point2f> midpoints(4);
vector< Point2f> dst_corners(4);
int roiIndex = 0;
bool dragging;
@ -99,21 +99,26 @@ int main(int argc, char** argv)
imshow( windowTitle, image );
midpoints[0] = (roi_corners[0] + roi_corners[1]) / 2;
midpoints[1] = (roi_corners[1] + roi_corners[2]) / 2;
midpoints[2] = (roi_corners[2] + roi_corners[3]) / 2;
midpoints[3] = (roi_corners[3] + roi_corners[0]) / 2;
dst_corners[0].x = 0;
dst_corners[0].y = 0;
dst_corners[1].x = (float)std::max(norm(roi_corners[0] - roi_corners[1]), norm(roi_corners[2] - roi_corners[3]));
dst_corners[1].x = (float)norm(midpoints[1] - midpoints[3]);
dst_corners[1].y = 0;
dst_corners[2].x = (float)std::max(norm(roi_corners[0] - roi_corners[1]), norm(roi_corners[2] - roi_corners[3]));
dst_corners[2].y = (float)std::max(norm(roi_corners[1] - roi_corners[2]), norm(roi_corners[3] - roi_corners[0]));
dst_corners[2].x = dst_corners[1].x;
dst_corners[2].y = (float)norm(midpoints[0] - midpoints[2]);
dst_corners[3].x = 0;
dst_corners[3].y = (float)std::max(norm(roi_corners[1] - roi_corners[2]), norm(roi_corners[3] - roi_corners[0]));
dst_corners[3].y = dst_corners[2].y;
Size warped_image_size = Size(cvRound(dst_corners[2].x), cvRound(dst_corners[2].y));
Mat H = findHomography(roi_corners, dst_corners); //get homography
Mat M = getPerspectiveTransform(roi_corners, dst_corners);
Mat warped_image;
warpPerspective(original_image, warped_image, H, warped_image_size); // do perspective transformation
warpPerspective(original_image, warped_image, M, warped_image_size); // do perspective transformation
imshow("Warped Image", warped_image);
}