How do I get perspective tranformation for crooked (hand-drawn) rectangle?
That's what I want:
But instead I got:
Here is my code:
Rect boundingBox(boundingRect(countours[largest_contour_index]));
vector<Point> not_a_rect_shape;
not_a_rect_shape.push_back(boundingBox.tl());
not_a_rect_shape.push_back(Point(boundingBox.tl().x, boundingBox.br().y));
not_a_rect_shape.push_back(boundingBox.br());
not_a_rect_shape.push_back(Point(boundingBox.br().x, boundingBox.tl().y));
RotatedRect box = minAreaRect(cv::Mat(not_a_rect_shape));
Point2f pts[4];
box.points(pts);
cv::Point2f src_vertices[4];
src_vertices[0] = not_a_rect_shape[0];
src_vertices[1] = not_a_rect_shape[1];
src_vertices[2] = not_a_rect_shape[3];
src_vertices[3] = not_a_rect_shape[2];
Point2f dst_vertices[4];
dst_vertices[0] = Point(0, 0);
dst_vertices[1] = Point(box.boundingRect().width-1, 0);
dst_vertices[2] = Point(0, box.boundingRect().height-1);
dst_vertices[3] = Point(box.boundingRect().width-1, box.boundingRect().height-1);
Mat warpAffineMatrix = getAffineTransform(src_vertices, dst_vertices);
cv::Mat rotated;
cv::Size size(box.boundingRect().width, box.boundingRect().height);
warpAffine(src, rotated, warpAffineMatrix, size, INTER_LINEAR, BORDER_CONSTANT);
imshow("rotated.jpg", rotated);
P.S. All rectangles that I worked with are hand drawn. Maybe, is it working only for straight rectangles?
You are using Affine Transforms for a perspective problem. The affine transform only models rotation, translation and scaling (both vertical and horizontal), so your problem cannot be solved using this kind of transform. Use perspective transform instead.
EDIT: Hey, look at this nice tutorial using cv::getPerspectiveTransform
and cv::WarpPerspective
, also using 4 corners as in your scenario. It may solve your problem!