简体   繁体   English

使用opencv将旋转和倾斜的图像插入到另一个图像中检测到的矩形中?

[英]Insert a rotated and skewed image into a detected rectangle in another image with opencv?

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? 我需要为此使用opengl吗?

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 . 如果要插入的图像是一个真正的矩形并且裁剪得很好,则只能使用getPerspectiveTransformwarpPerspective的组合使用OpenCV进行此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). 如果要插入的图像不是真实的矩形(例如信用卡),则需要使用Alpha通道,这可能需要对warpPerspective进行重新编码(不是那么难)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM