Here are the files The output with the input and here is the image used in the code; The actual card used in the code. . The blue positions are from paint. Output is the output from the code. I have attached the image I am talking about. I want to get the perspective transformation so I can get the card in its canonical form. It does not work and I get a bad output.
This is my code. The points I got from point and they are correct (checked with the function circle() )
void main(){ cv::Mat begin = cv::imread("cards.jpg"); cv::Mat output; cv::Point2f Poly2[4] = { cv::Point2f(222,93), cv::Point2f(430,134), cv::Point2f(368,426), //points I got from looking in paint. cv::Point2f(162,378), }; cv::Point2f Points[4] = { cv::Point2f(0,0), cv::Point2f(300,0), cv::Point2f(0,600), //The picture I want to transform to. cv::Point2f(600,300), }; for (int i = 0; i < 4; i++) { cv::circle(begin, cv::Point2d(Poly2[i].x, Poly2[i].y), 2, cv::Scalar(244, 233, 44), 3, 8, 0); } cv::Mat Matrix = cv::getPerspectiveTransform( Points,Poly2); cv::warpPerspective(begin, output, Matrix, cv::Size(300, 600)); cv::imshow("begin", begin); cv::imshow("Output", output); cv::waitKey(0); }
Answer
Your first mistake is that the points you took by using paint ( you may also use opencv imshow
to do that ) is not in the correct order. They should be accordingly: left-up
, right-up
, left-down
and right-down
.
Your second and actual mistake is about the order of getPerspectiveTransform. You should first source then the desired(destination). If you correct these 2 mistakes you ll get the output like:
And the code:
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> #include <fstream> int main() { cv::Mat begin = cv::imread("/home/yongatek02/Documents/photos/cards.jpg"); cv::Mat output; cv::Point2f Poly2[4] = { cv::Point2f(222,93), cv::Point2f(430,134), cv::Point2f(162,378), cv::Point2f(368,426), //points I got from looking in paint. }; cv::Point2f Points[4] = { cv::Point2f(0,0), cv::Point2f(300,0), cv::Point2f(0,300), //The picture I want to transform to. cv::Point2f(300,300), }; for (int i = 0; i < 4; i++) { cv::circle(begin, cv::Point2d(Poly2[i].x, Poly2[i].y), 2, cv::Scalar(244, 233, 44), 3, 8, 0); } cv::Mat Matrix = cv::getPerspectiveTransform( Poly2,Points); cv::warpPerspective(begin, output, Matrix, cv::Size(300, 300)); cv::imshow("begin", begin); cv::imshow("Output", output); cv::waitKey(0); return 0; }
Thanks for clearly explained question