我使用this library在Golang中编写OpenCV应用程序。我正在尝试做一些非常基本的事情,但似乎无法使其工作。我只想获取一组轮廓,删除那些没有最小面积的轮廓,然后返回过滤结果。Go/OpenCV:Filter Contours
这是我的代码的当前状态:
// 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包装非常具有挑战性。
感谢您的回复。这是有道理的,实际上是我最初做的 - 我将它改为只是为了尝试任何东西而发布的内容......无论如何,它不能解决问题。 – rumdrums