如何实时平滑阈值处理?

时间:2019-03-28 06:05:45

标签: python opencv video threshold

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

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

尝试

  1. 我试图更改网络摄像头设备(使用İpcam的手机摄像头)
  2. 我试图通过自适应阈值方法(大津等)来找到最佳阈值
  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)

1 个答案:

答案 0 :(得分:0)

从接下来的帧中减去第一帧怎么办?如果您知道或可以检测出何时没有丢弃并将其减去,则差异只会为您提供丢弃的结果。

如果您在不同位置有多个放置,并希望丢弃先前放置,则此方法也可能很有趣。 请注意,您可以在阈值化之前和之后进行减法运算。我建议先设定阈值。

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

当然,稳定化应该在减法之前完成。

通常,对于您的问题,我不会使用自适应方法。所有帧的阈值都应该相同,如果根据图像进行调整,可能会导致无效的结果。

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