Insert rotated and skewed image into detected rectangle in another image with opencv?

I found a rectangle, which may not be in shape or at an angle, inside one image, and now I rotated and skewed the second image to insert into this detected rectangle in the first image. What is the best way for me to do this? Do I need to use opengl for this?

0
source share
1 answer

If the pasted image is a true rectangle and cropped well, you can only do this with OpenCV with 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 inserted image is not a true rectangle, such as a credit card, you need to use alpha channels, which may require warpPerspective transcoding (not so difficult).

0
source

All Articles