我正在使用gocv,我有一个播放视频的窗口。我已经获得了窗口的ROI,在那里我知道有移动。我已经放大并将颜色转换为HSV。我现在可以在我的ROI中找到最大的轮廓,并在它周围画一个框。
但是,我想在每次检测到对象在窗口内移动时记录时间戳。这并不是很难,但是我只想花一次,而不是现在发生的很多很多。
我的代码目前看起来像这样
for {
gocv.GaussianBlur(imgCrop, &imgCrop, blur, 0, 0, gocv.BorderReflect101)
gocv.CvtColor(imgCrop, &imgCrop, gocv.ColorBGRToHSV)
thresholded := gocv.NewMat()
gocv.InRangeWithScalar(imgCrop,
lhsv,
hhsv,
&thresholded)
gocv.Erode(thresholded, &thresholded, kernel)
gocv.Dilate(thresholded, &thresholded, kernel)
const minArea = 500
cnt, set := bestContour(thresholded, minArea)
gocv.Line(&imgCrop, image.Pt(0, line), image.Pt(imgCrop.Cols(), line), color.RGBA{255, 0, 0, 0}, 2)
if set {
cntBox := gocv.BoundingRect(cnt)
gocv.Rectangle(&imgCrop, cntBox, blue, 2)
log.Println("time ", time.Since(startTime))
}
// draw it.
gocv.Rectangle(&img, rect, blue, 3)
wi.IMShow(img)
wc.IMShow(imgCrop)
wt.IMShow(thresholded)
if wi.WaitKey(1) == 27 || wt.WaitKey(1) == 27 {
break
}
}并且输出

然而,这可能会多次超时,因为它在对象离开ROI之前多次检测到相同的运动。我尝试在每次对象进入ROI时仅获得一个计时
我添加了红线,因为我认为可能有一个技巧,我可以检查cntBox是否越过了这条线,然后读取它,然而我的大脑融合了。
我还想也许我可以在ROI中画一个矩形,并检查蓝色的cntbox是否进入矩形,但同样的问题...
我注意到有一个使用朋友圈来做这件事的例子,我试过了,但我相信朋友圈也在做简单的检测。我想知道moments是否可以与我现有的代码一起使用。
仅供参考,在ROI中永远只有一个对象。非常感谢您的帮助。
发布于 2021-05-14 01:51:10
看起来你共享了一个函数,而我只能复制部分代码,所以我的语法是关闭的。因此,我将更详细地阐述一下...
我认为你每秒抓取许多帧,对象不会进入和离开你的ROI在单个帧的跨度,所以你想过滤掉任何连续的帧在你的ROI中的“命中”?
我添加了一个变量“timestamp counter”,它只是一个计数器,每当你的ROI中出现“命中”时,它就会倒计时,一旦它完全倒计时(30帧,如果你(希望)时钟帧那么快,这将是一秒),你现在最多每秒只发送一次时间戳。
请记住,我编程的时间不长,但我已经做了一些YOLO视频捕获工作,所以尽管它听起来可能很原始,但它确实有效。
#add a "buffer" for your calls to grab a timestamp. the buffer delays the calls to write a timestamp
timeStampBuffer=30
gocv.GaussianBlur(imgCrop, &imgCrop, blur, 0, 0, gocv.BorderReflect101)
gocv.CvtColor(imgCrop, &imgCrop, gocv.ColorBGRToHSV)
thresholded := gocv.NewMat()
gocv.InRangeWithScalar(imgCrop,
lhsv,
hhsv,
&thresholded)
gocv.Erode(thresholded, &thresholded, kernel)
gocv.Dilate(thresholded, &thresholded, kernel)
const minArea = 500
cnt, set := bestContour(thresholded, minArea)
gocv.Line(&imgCrop, image.Pt(0, line), image.Pt(imgCrop.Cols(), line), color.RGBA{255, 0, 0, 0}, 2)
if set {
cntBox := gocv.BoundingRect(cnt)
gocv.Rectangle(&imgCrop, cntBox, blue, 2)
if timeStampBuffer=30:
log.Println("time ", time.Since(startTime))
else if timeStampBuffer=0:
timeStampBuffer=31
timeStampBuffer-=1
}
// draw it.
gocv.Rectangle(&img, rect, blue, 3)
wi.IMShow(img)
wc.IMShow(imgCrop)
wt.IMShow(thresholded)
if wi.WaitKey(1) == 27 || wt.WaitKey(1) == 27 {
break
} https://stackoverflow.com/questions/67523123
复制相似问题