【问题标题】:How can I smooth the thresholding process in real time?如何实时平滑阈值处理?
【发布时间】:2019-08-18 20:09:42
【问题描述】:

首先我想用相机找到滴水时间的变化。用户将在算法检测到运动后滴水到织物上,直到水完全吸收,并绘制图表变化时间并显示吸收时间,面积等。

为了检测运动,我使用了具有恒定变化率的 absdif 函数。我将检测帧的开始时间到结束like this image。这里没有问题。但是为了计算吸水率我对帧进行阈值处理并使用 countNonZero 函数来计算黑色像素的数量。但是这里有一个问题,thresholded images 显示红线的黑色像素是不断变化的(如晃动、振动等),因此绘图过程失败。

试试

  1. 我尝试更换网络摄像头设备(使用 İpcam 的手机摄像头)
  2. 我尝试使用自适应阈值方法(otsu 等)来找到最佳阈值
  3. 平滑的闪电条件和无背景捕获

成功

  1. 当我使用手机摄像头拍摄的视频作为输入时,晃动和振动效果减少,我可以达到预期的成功this graph

问题

  • 如何实时平滑阈值图像
  • 另一种方法

代码

import cv2
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
import operator

def pixelHesaplayici(x):
    siyaholmayanpixel=cv2.countNonZero(x)
    height,width=x.shape
    toplampixel=height*width
    siyahpixelsayisi=toplampixel-siyaholmayanpixel
    return siyahpixelsayisi

def grafikciz(sure,newblackpixlist,maxValue,index,totaltime,cm):
    plt.figure(figsize=(15,15))
    plt.plot(sure,newblackpixlist)
    line,=plt.plot(sure,newblackpixlist)
    plt.setp(line,color='r')
    plt.text(totaltime/2,maxValue/2, r'$Max- 
    Pixel=%d$'%maxValue,fontsize=14,color='r')
    plt.text(totaltime/2,maxValue/2.5, r'$Max-emilim- 
    zamanı=%f$'%sure[index],fontsize=14,color='b')
    plt.text(totaltime/2,maxValue/3, r'$Max- 
    Alan=%fcm^2$'%cm,fontsize=14,color='g')
    plt.ylabel('Black Pixels')
    plt.xlabel('Time(s)')
    plt.grid(True)
    plt.show()


static_back=None
i=0
blackpixlist=[]
newblackpixlist=[]

t=[]
video=cv2.VideoCapture("kumas1.mp4")

while(True):
    ret,frame=video.read()

    if ret==True:
        gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        gray=cv2.GaussianBlur(gray,(5,5),0)
        _,threshforgraph=cv2.threshold(gray,0,255,
       cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        if static_back is None:
            static_back=gray
            continue
        diff_frame=cv2.absdiff(static_back,gray)

        threshfortime=cv2.threshold(diff_frame,127,255,cv2.THRESH_BINARY)[1]
        #threshfortime=cv2.dilate(threshfortime,None,iterations=2)
        (_,cnts,_)=cv2.findContours(threshfortime.copy(),
                               cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

        for contour in cnts:
            if cv2.contourArea(contour)<450:
                continue
            an=datetime.now()
            t.append(an.minute*60+an.second+(an.microsecond/1000000))
            cv2.fillPoly(frame,contour, (255,255,255), 8,0)
            cv2.imwrite("samples/frame%d.jpg"%i,threshforgraph)


            i+=1


        cv2.imshow("org2",frame)
        #cv2.imshow("Difference Frame",diff_frame)
        #cv2.imshow("Threshold Frame",threshfortime)
        #cv2.imshow("Threshforgraph",threshforgraph)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
ti=t[1::3]

lasttime=ti[-1]
firsttime=ti[-len(ti)]
totaltime=lasttime-firsttime

for i in range(0,i):
        img=cv2.imread('samples/frame%d.jpg'%i,0)
        blackpixlist.append(pixelHesaplayici(img))
ilkpix=blackpixlist[0]

for a in blackpixlist:
    newblackpixlist.append(a-ilkpix)
newblackpixlisti=newblackpixlist[1::3]  
index , maxValue=max(enumerate(newblackpixlisti),
key=operator.itemgetter(1))
sure=np.linspace(0,totaltime,len(newblackpixlisti))
cm=0.0007*maxValue # For 96 dpi

grafikciz(sure,newblackpixlisti,maxValue,index,totaltime,cm)

【问题讨论】:

标签: python opencv video threshold


【解决方案1】:

从后面的帧中减去第一帧怎么样?如果您可以知道或可以检测到何时没有下降并减去它,则差异只会给您下降的结果。

如果您在不同的地方有几滴并且想要丢弃前一滴,这种方法也可能很有趣。 请注意,您可以在阈值之前和之后进行减法。我会推荐在阈值之前。

如果您知道您的过程中有很多抖动,您可能需要应用数字稳定,在这种情况下,我建议您查看本教程: https://www.learnopencv.com/video-stabilization-using-point-feature-matching-in-opencv/

当然要在减法之前进行稳定化。

一般来说,对于您的问题,我不会使用自适应方法。所有帧的阈值应该相同,如果它根据图像进行调整,您可能会得到无效的结果。

希望我能正确理解您的问题!

【讨论】:

  • 找到最大黑色像素的绝妙方法,但我需要运动的时间知识,所以我必须知道停止运动的时间。实际上,在这种方法中,我使用了 2 个阈值函数(其中一个为图形计数非零像素,另一个是检测运动)。但是失败了。由于抖动像素,检测功能始终检测运动,并看到无限的运动时间。另一方面,根据面料类型,我的阈值必须更改,因为你的方法实际上我完全失败了,因为我的文档不好。如何优化这个系统 ım Python 上的新功能
猜你喜欢
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 2016-11-17
相关资源
最近更新 更多