Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a transformation matrix from rotation/translation vectors?

I'm trying to deskew an image that has an element of known size. Given this image:

Source image

I can use aruco:: estimatePoseBoard which returns rotation and translation vectors. Is there a way to use that information to deskew everything that's in the same plane as the marker board? (Unfortunately my linear algebra is rudimentary at best.)

Clarification

I know how to deskew the marker board. What I want to be able to do is deskew the other things (in this case, the cloud-shaped object) in the same plane as the marker board. I'm trying to determine whether or not that's possible and, if so, how to do it. I can already put four markers around the object I want to deskew and use the detected corners as input to getPerspectiveTransform along with the known distance between them. But for our real-world application it may be difficult for the user to place markers exactly. It would be much easier if they could place a single marker board in the frame and have the software deskew the other objects.

like image 981
SSteve Avatar asked Jan 29 '16 03:01

SSteve


2 Answers

Since you tagged OpenCV: From the image I can see that you have detected the corners of all the black box. So just get the most border for points in a way or another: enter image description here

Then it is like this:

std::vector<cv::Point2f> src_points={/*Fill your 4 corners here*/};
std::vector<cv::Point2f> dst_points={cv:Point2f(0,0), cv::Point2f(width,0), cv::Point2f(width,height),cv::Point2f(0,height)}; 
auto H=v::getPerspectiveTransform(src_points,dst_points);
cv::Mat copped_image;
cv::warpPerspective(full_image,copped_image,H,cv::Size(width,height));
like image 109
Humam Helfawi Avatar answered Sep 17 '22 14:09

Humam Helfawi


I was stuck on the assumption that the destination points in the call to getPerspectiveTransform had to be the corners of the output image (as they are in Humam's suggestion). Once it dawned on me that the destination points could be somewhere within the output image I had my answer.

float boardX = 1240;
float boardY = 1570;
float boardWidth = 1730;
float boardHeight = 1400;

vector<Point2f> destinationCorners;
destinationCorners(Point2f(boardX+boardWidth, boardY));
destinationCorners(Point2f(boardX+boardWidth, boardY+boardHeight));
destinationCorners(Point2f(boardX, boardY+boardHeight));
destinationCorners(Point2f(boardX, boardY));

Mat h = getPerspectiveTransform(detectedCorners, destinationCorners);

Mat bigImage(image.size() * 3, image.type(), Scalar(0, 50, 50));

warpPerspective(image, bigImage, h, bigImage.size());

This fixed the perspective of the board and everything in its plane. (The waviness of the board is due to the fact that the paper wasn't lying flat in the original photo.)

corrected perspective

like image 35
SSteve Avatar answered Sep 19 '22 14:09

SSteve