# How to do a perspectiveTransform when I don't know the matrix

If I have a such empty paper, then I write some text on it,then I deform it in Photoshop. So I get such wraped image. Now hope this result image align that empty paper. I mean this is my expected result. Actually I can do it almost by other language. I just not very familiar the OpenCV. This is my try in Wolfram Mathematica :

Before Align

After Align

As you see,the result is very good, But I have to implement it by C++ and OpenCV now. I note we have a function getPerspectiveTransform can get a transform matrix. But I have to get four point from the source image and target image. I really don't want to do such thing. I mean those four point is hard to get in some case.

# Question

When I don't want to find those four points. How to do a perspective transform to align this two image?

# Current try with C++/OpenCV

#include "opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
rectangle(mark, Rect(0, 0, mark.cols, mark.rows), Scalar(0));
Mat rect = mark.clone();
Mat tempmark = rect.clone();
floodFill(mark, Point(0, 0), Scalar(255), 0, 10, 40, 8);

rectangle(empty, Rect(0, 0, empty.cols, empty.rows), Scalar(0));
rect = empty.clone();
Mat tempempty = rect.clone();
floodFill(empty, Point(0, 0), Scalar(255), 0, 10, 40, 8);
rect.release();

double start_time = (double)getTickCount();

resize(mark, mark, empty.size());

Mat m = Mat::eye(3, 3, CV_32F);
findTransformECC(empty, mark, m, 3);
Mat result;
warpPerspective(mark, result, m, mark.size());

cvtColor(255-empty, empty, COLOR_GRAY2BGR);
cvtColor(mark, mark, COLOR_GRAY2BGR);
cvtColor(result, result, COLOR_GRAY2BGR);

double total_time = ((double)getTickCount() - start_time) / getTickFrequency();
cout << "Total time is： " << total_time << "s" << endl;

Mat element = getStructuringElement(MORPH_RECT, Size(2, 2));
dilate(empty - Scalar(0, 0, 255), empty, element);
namedWindow("before", WINDOW_NORMAL);
imshow("before", mark-empty);
namedWindow("after", WINDOW_NORMAL);
imshow("after", result-empty);

waitKey(0);

return 0;


}

edit retag close merge delete