0

注意:StackOverflow 不会让我回答我自己的问题,所以我在这里回答。请滚动到底部以查看我的答案。

问题给定一个二值图像,我希望能够确定哪个区域具有最大的 y 坐标,即哪个区域最接近底部。在下面的函数中,我尝试使用轮廓和边界矩形来获得我需要的答案。但是,我使用函数 cvContourBOundingRect 在编译过程中会出现以下错误消息:

"_cvContourBoundingRectemphasized", referenced from: 
GetLowestContourBoundingRectangle(_IplImage * img, bool) 
in main.o. Symbol(s) not found. Collect2: Id returned 1 exit status. 
Build Failed.

这很奇怪,因为我已经成功地使用了其他轮廓函数,如 cvFindContours 和 cvContourArea 没有任何麻烦。我尝试在 Google 上进行一些搜索,但没有任何结果。如果有人能指出我正确的方向,我将不胜感激。

提前致谢。

CvRect GetLowestContourBoundingRectangle(IplImage *img, bool invertFlag) {
    // NOTE: CONTOURS ARE DRAWN AROUND WHITE AREAS
    IplImage *output = invertFlag ? cvCloneImage(InvertImage(img)) : cvCloneImage(img); // this goes into find contours and is consequently modified

    // find contours
    CvMemStorage *contourstorage = cvCreateMemStorage(0);
    CvSeq* contours = NULL;
    cvFindContours(output, contourstorage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

    // analyze each contour
    int lowestRectangleCoordinate = 0;
    CvRect currentBoundingRectangle;
    CvRect lowestBoundingRectangle;

    while (contours) {
        currentBoundingRectangle = cvContourBoundingRect(contours);
        if (currentBoundingRectangle.y + currentBoundingRectangle.height > lowestRectangleCoordinate) {
            lowestRectangleCoordinate = currentBoundingRectangle.y + currentBoundingRectangle.height;
            lowestBoundingRectangle = currentBoundingRectangle;
        }

        contours = contours->h_next;
    }

    cvReleaseMemStorage(&contourstorage);
    return lowestBoundingRectangle;
}

回答: 好的,具有讽刺意味的是,我在起草我的原始问题后不久就发现了它为什么会中断(尽管公平地说,此时我已经为此苦苦挣扎了几个小时)。

我查找了定义以下三个函数的头文件:

  1. cvFindContours——imgproc_c.h
  2. cvContourArea -- imgproc_c.h
  3. cvContourBoundingRect——compat.hpp

compat.hpp 显然是为了向后兼容而保留的已弃用功能。这是标题中写的内容:

/*
   A few macros and definitions for backward compatibility
   with the previous versions of OpenCV. They are obsolete and
   are likely to be removed in future. To check whether your code
   uses any of these, define CV_NO_BACKWARD_COMPATIBILITY before
   including cv.h.
*/

话虽如此,有谁知道我如何才能真正用非过时的定义编写这个函数?

4

1 回答 1

1

关于您最初的问题“OpenCV:cvContourBoundingRect 给出“未找到符号””。

用于链接该(已弃用)方法的库是这个:

libopencv_legacy.so

于 2013-03-06T09:08:12.513 回答