Go/OpenCV:过滤轮廓
我正在使用Golang编写OpenCV应用程序。我正在尝试做一些非常基本的事情,但似乎无法使它工作。我只想获取一组轮廓,删除那些没有最小面积的轮廓,然后返回过滤结果 这是我的代码的当前状态:Go/OpenCV:过滤轮廓,opencv,go,Opencv,Go,我正在使用Golang编写OpenCV应用程序。我正在尝试做一些非常基本的事情,但似乎无法使它工作。我只想获取一组轮廓,删除那些没有最小面积的轮廓,然后返回过滤结果 这是我的代码的当前状态: // given *opencv.Seq and image, draw all the contours func opencvDrawRectangles(img *opencv.IplImage, contours *opencv.Seq) { for c := contours; c !=
// 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
中,我试图将满足面积阈值的轮廓添加到新变量中。当我获取这些结果并将它们传递到opencvdraw矩形
,等高线
中充满了无意义的数据。另一方面,如果我只是直接在opencvFindContours
中返回轮廓
,然后将其传递到opencvdraw矩形
,我将根据图像中检测到的运动获得预期的矩形
有人知道如何使用这个库正确过滤轮廓吗?很明显,我遗漏了一些关于这些数据结构如何工作的信息,只是不确定是什么
不管它是如何最好地实现的,我在这里想弄明白的主要事情就是如何获取一系列的轮廓,并过滤掉低于某个区域的轮廓。。。。我所看到的所有C++示例使这看起来很简单,但是我发现使用C API的GO包装器是非常有挑战性的。 < P>您使用的是
Sizeof
结构opencv.CVPoint{}
。谢谢您的回复。这是有道理的,实际上是我最初做的——我把它改成了发布的内容,只是为了尝试任何东西。。。不管怎样,它都不能解决问题。