在轮廓和垫子上应用同件

Apply homography on contours as well as Mat

本文关键字:应用 轮廓      更新时间:2023-10-16

当前我正在使用以下序列

vector<vector<Point>> contours
1. findContours(srcMat, contours, ...)
2. convert contours to Point2f
3. findHomography(src, dst, RANSAC)
4. warpPerspective(srcMat, destMat, homo)
5. findContours

我想避免步骤#4,同时还要转换垫子,因为我使用转换垫的轮廓使用一些ROI。

运行warpperspective的答案,但在轮廓上使用CV :: Perspectivetransform与Translation Matrix使用。限制是它一次只能转换一个轮廓。下面的样本。

vector<vector<Point2f>> contours; // from findContours
Mat trnsmat = getPerspectiveTransform(srcPoints, destPoints);
for (int i=0; i< contours.size(); i++)
    cv::perspectiveTransform(contours[i], contours[i], trnsmat);

我认为您的目标是在不使用整个图像的情况下将轮廓坐标投影到变换的空间中?

将轮廓坐标加载到roimat结构中,并将其乘以使用您的FindHomography函数计算的同构矩阵。无需将整个原始图像扭曲。

如果要查看图像上转换的ROI,则可能从原始图像中选择一些兴趣点(参考(并将其添加到Roimat结构中。

在python中,您可以将每个轮廓与以下代码隔离,然后在每个轮廓上执行任何处理。

contours, hierarchy = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    x, y, w, h = cv2.boundingRect(cnt)
    contor_img = image[y:y+h, x:x+w]

现在您可以单独处理每个轮廓(contor_img(