OpenCV 的 matchShapes 总是返回 0

标签 opencv

我正在使用 matchShapes 来识别一些基本轮廓。但是,无论我比较什么轮廓,它都会返回 0...

模板图片:

circle cross enter image description here

示例输入图像:

empty triangle, empty circle, cross

代码:

using std::vector;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

findContours(binary, contours, hierarchy,
             CV_RETR_CCOMP, CV_CHAIN_APPROX_TC89_KCOS);

drawContours(binary, contours, -1, Scalar(255, 255, 255));

if (!contours.size()) // avoid sigsegv
    return;

for (int idx = 0; idx >= 0; idx = hierarchy[idx][0]) {
    double bestMatch = INFINITY;
    int bestI = -1;
    for (int i = 0; i < knownContours.size(); i++) {
        vector<Point>& a = knownContours[i].contour;
        vector<Point>& b = contours[idx];
        std::cout << "a.size = " << a.size() << ", b.size = " << b.size() << std::endl;
        double match = matchShapes(b, a, CV_CONTOURS_MATCH_I1, 0);
        std::cout << "idx=" << idx << " ? " << knownContours[i].name << " = " << match << std::endl;
        if (bestI == -1 || match < bestMatch) {
            bestI = i;
            bestMatch = match;
        }
    }
}

(knownContours 显然是用模板图像数据初始化的:imread(),然后是findContours(),最后是this->轮廓 = 轮廓 [0]).

结果输出(片段):

-- new frame
a.size = 57, b.size = 74
idx=0 ? circle = 0
a.size = 80, b.size = 74
idx=0 ? cross = 0
a.size = 45, b.size = 74
idx=0 ? triangle = 0
a.size = 57, b.size = 60
idx=1 ? circle = 0
a.size = 80, b.size = 60
idx=1 ? cross = 0
a.size = 45, b.size = 60
idx=1 ? triangle = 0

-- new frame
a.size = 57, b.size = 75
idx=0 ? circle = 0
a.size = 80, b.size = 75
idx=0 ? cross = 0
a.size = 45, b.size = 75
idx=0 ? triangle = 0
a.size = 57, b.size = 57
idx=1 ? circle = 0
a.size = 80, b.size = 57
idx=1 ? cross = 0
a.size = 45, b.size = 57
idx=1 ? triangle = 0

-- new frame
a.size = 57, b.size = 76
idx=0 ? circle = 0
a.size = 80, b.size = 76
idx=0 ? cross = 0
a.size = 45, b.size = 76
idx=0 ? triangle = 0
a.size = 57, b.size = 51
idx=1 ? circle = 0
a.size = 80, b.size = 51
idx=1 ? cross = 0
a.size = 45, b.size = 51
idx=1 ? triangle = 0

所以比较轮廓的大小不同(因此轮廓不同),但 match == 0 总是。这是怎么回事?

编辑:OpenCV 版本为 2.4.9(昨天克隆和构建)。

最佳答案

此问题与 OpenCV 2.4.9 中的错误有关。

在 2.4.5(稳定版)上运行良好。

参见 this post on OpenCV Answers .

关于OpenCV 的 matchShapes 总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16976029/

相关文章:

opencv - cv::bitwise_not on cv::Mat 矩阵

c++ - 无法从 cv::imencode 中获取编码图像(变得一团糟)。如何修复损坏的 .jpg 保存?

python - Python Bot 的基本计算机视觉技术。

使用 OpenCV、OpenCL、OpenGL 的 android ndk CDT 构建错误

c++ - OpenCv Unspecified error(节点不代表用户对象(未知类型?))

python - 厌倦了多 View 多人脸检测

opencv - DevCpp 上 OpenCV 2.3.1 中的链接器干扰

opencv - 使用opencv过滤掉物体检测的误报

python - 使用opencv识别浅灰色背景上的深灰色空心细胞

python - 完成 "broken"图像的轮廓