在轮廓和Mat上应用单应性

时间:2018-02-05 19:12:12

标签: c++ opencv

目前我正在使用以下序列

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,同时也改变了Mat,因为我使用了一些相对于转换后Mat的轮廓的ROI。

3 个答案:

答案 0 :(得分:0)

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

将轮廓坐标加载到RoiMat结构中,并将其与使用findHomography函数计算的单应矩阵相乘。 没有必要扭曲整个原始图像。

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

答案 1 :(得分:0)

在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)

答案 2 :(得分:0)

运行warpPerspective但在轮廓上的答案是将cv :: perspectiveTransform与转换矩阵一起使用。 限制是它一次只能转换一个轮廓。以下示例。

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