【问题标题】:Go/OpenCV: Filter ContoursGo/OpenCV:过滤轮廓
【发布时间】:2017-04-23 12:57:43
【问题描述】:

我正在使用this library 在 Golang 中编写 OpenCV 应用程序。我正在尝试做一些非常基本的事情,但似乎无法让它发挥作用。我只是想获取一组轮廓,删除那些没有最小面积的轮廓,然后返回过滤结果。

这是我的代码的当前状态:

// given *opencv.Seq and image, draw all the contours
func opencvDrawRectangles(img *opencv.IplImage, contours *opencv.Seq) {
    for c := contours; c != nil; c = c.HNext() {
        rect := opencv.BoundingRect(unsafe.Pointer(c))
        fmt.Println("Rectangle: ", rect.X(), rect.Y())
        opencv.Rectangle(img, 
            opencv.Point{ rect.X(), rect.Y() }, 
            opencv.Point{ rect.X() + rect.Width(), rect.Y() + rect.Height() },
            opencv.ScalarAll(255.0), 
            1, 1, 0)
    }
}

// return contours that meet the threshold
func opencvFindContours(img *opencv.IplImage, threshold float64) *opencv.Seq {
    defaultThresh := 10.0
    if threshold == 0.0 {
        threshold = defaultThresh
    }
    contours := img.FindContours(opencv.CV_RETR_LIST, opencv.CV_CHAIN_APPROX_SIMPLE, opencv.Point{0, 0})

    if contours == nil {
        return nil
    }

    defer contours.Release()

    threshContours := opencv.CreateSeq(opencv.CV_SEQ_ELTYPE_POINT,
                            int(unsafe.Sizeof(opencv.CvPoint{})))

    for ; contours != nil; contours = contours.HNext() {
        v := *contours
        if opencv.ContourArea(contours, opencv.WholeSeq(), 0) > threshold {
            threshContours.Push(unsafe.Pointer(&v))
        }
    }
    return threshContours
}

opencvFindContours 中,我试图仅将那些满足面积阈值的轮廓添加到新变量中。当我将这些结果传递给opencvDrawRectangles 时,contours 充满了无意义的数据。另一方面,如果我直接在opencvFindContours 中返回contours,然后将其传递给opencvDrawRectangles,我会根据图像中检测到的运动得到我期望的矩形。

有谁知道如何使用这个库正确过滤轮廓?我显然遗漏了一些关于这些数据结构如何工作的东西,只是不确定是什么。

但是最好实现它,我在这里想要弄清楚的主要事情就是如何获取一系列轮廓并过滤掉低于某个区域的轮廓......我见过的所有 c++ 示例让这看起来很简单,但我发现使用 C API 的 Go 包装器非常具有挑战性。

【问题讨论】:

    标签: opencv go


    【解决方案1】:

    您正在使用 CreateSeq 返回的指针 Sizeof。您可能想要 Sizeof 结构 opencv.CVPoint{} 代替。

    【讨论】:

    • 感谢您的回复。这是有道理的,实际上是我最初所做的——我将其更改为发布的内容,只是为了尝试任何事情……无论哪种方式,它都不能解决问题。
    猜你喜欢
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2018-05-06
    • 1970-01-01
    • 2014-07-12
    相关资源
    最近更新 更多