I have detected a rectangle, which could be out of shape or at an angle, inside one image and now I have rotated and skewed a second image to insert into that detected rectangle in the first image. What is the best way for me to do this? Do I need to use opengl for this?
Asked
Active
Viewed 581 times
1 Answers
1
If the image to be inserted is a true rectangle and is well cropped, you can do this with OpenCV only using a combination of getPerspectiveTransform
and warpPerspective
.
// First estimate the perspective transform to be applied
cv::Mat srcCorners; // Rectangle corners in your second image (image to be inserted)
cv::Mat dstCorners; // Rectangle corners in your first image (image with detected rectangle)
cv::Mat H = cv::getPerspectiveTransform(srcCorners,dstCorners);
// Copy original image and insert new image
cv::Mat composite;
secondImage.copyTo(composite);
cv::warpPerspective(firstImage,composite,H,composite.size(),cv::INTER_CUBIC,cv::BORDER_TRANSPARENT);
If the image to be inserted is not a true rectangle, such as a credit card, you need to use alpha channels, which might require recoding the warpPerspective
(not that hard).

BConic
- 8,750
- 2
- 29
- 55