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

Posted

技术标签:

【中文标题】使用opencv将旋转和倾斜的图像插入另一个图像中检测到的矩形?【英文标题】:Insert a rotated and skewed image into a detected rectangle in another image with opencv? 【发布时间】:2014-03-26 13:48:08 【问题描述】:

我在一个图像内检测到一个矩形,该矩形可能变形或倾斜,现在我旋转并倾斜了第二个图像以插入到第一个图像中检测到的矩形中。我这样做的最佳方法是什么?我需要为此使用 opengl 吗?

【问题讨论】:

发布一些示例图片将对观众有所帮助,或者您在此处发布的相同图片***.com/questions/22645735/… 【参考方案1】:

如果要插入的图像是一个真正的矩形并且裁剪得很好,您可以使用 OpenCV 执行此操作,仅使用 getPerspectiveTransformwarpPerspective 的组合。

// 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);

如果要插入的图像不是真正的矩形,例如信用卡,则需要使用 Alpha 通道,这可能需要重新编码 warpPerspective(不是那么难)。

【讨论】:

以上是关于使用opencv将旋转和倾斜的图像插入另一个图像中检测到的矩形?的主要内容,如果未能解决你的问题,请参考以下文章

在 C++ 中计算相似图像的偏移/倾斜/旋转

在opencv python中对图像进行倾斜检测的代码

RotateRect(旋转矩形)的倾斜旋转变换矫正

使用OpenCL+OpenCV实现图像旋转

opencv图像倾斜校正和切边

使用Python,OpenCV旋转图像任意角度(完整和局部丢失~)