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).
source share