【问题标题】:cv2.GaussianBlur at multiple kernel sizes多个内核大小的 cv2.GaussianBlur
【发布时间】:2021-01-15 04:23:44
【问题描述】:

目前,我正在尝试使用 OpenCV 执行运动检测。对于每个新帧,我使用下面的函数来与之前的帧进行比较:

    def detect(new_frame, kernel_size):
        frame=cv2.cvtColor(new_frame,cv2.COLOR_BGR2GRAY) #Grayscale conversion of the frame
        frame=cv2.GaussianBlur(frame, (kernel_size, kernel_size),0) 
        
        deltaFrame=cv2.absdiff(old_frame, frame)    
        old_frame = frame

        threshFrame=cv2.threshold(deltaFrame, 5, 255, cv2.THRESH_BINARY)[1]
        threshFrame=cv2.dilate(threshFrame, None, iterations=2)
            
        (cnts,_)=cv2.findContours(threshFrame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        return cnts

我的问题是,我必须检测两种类型的对象的运动,它们中的每一种都有它自己的有效内核大小参数值(即:5 和 11)。所以我必须在每个新框架中使用该功能 2 次。但是我的设备有资源限制,所以我想尽可能地减少这个过程。我该怎么做?

【问题讨论】:

    标签: python opencv motion-detection


    【解决方案1】:

    在掩码上尝试按位函数。检测每个像素都在移动。速度很快。

    对我来说,诀窍是使用调整大小的框架图像。

    import numpy as np
    import cv2 as cv2
    
    fid=0
    
    video_path="videos/example.mp4"
    cap = cv2.VideoCapture(video_path)
    
    # Some characteristics from the original video
    w_frame, h_frame = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps, num_frames = cap.get(cv2.CAP_PROP_FPS), cap.get(cv2.CAP_PROP_FRAME_COUNT)
    print(fps,w_frame,h_frame)
    x,y,h,w = 0,0,h_frame,w_frame
    
    fnum=0
    while(True):
        
        ret, frame = cap.read()
    
        if ret == None: pass 
    
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        edges = gray    
    
    
        if  fnum==0:
            last_edges = edges.copy() 
            
        ret, mask1 = cv2.threshold(edges, 127, 255, cv2.THRESH_BINARY)
        ret, mask2 = cv2.threshold(last_edges, 127 , 255, cv2.THRESH_BINARY)
    
        dst1 = cv2.bitwise_and(mask2,mask1)
        dst2 = cv2.bitwise_not(dst1)
        dst4 = cv2.bitwise_and(dst2,dst2,mask=mask1)
    
        scale_percent = 10 # percent of original size
        width = int(dst4.shape[1] * scale_percent / 100)
        height = int(dst4.shape[0] * scale_percent / 100)
        dim = (width, height)
        
        # resize image
        mini = cv2.resize(dst4, dim, interpolation = cv2.INTER_AREA)
    
        h,w = mini.shape
    
        th=30 #my threshold
    
        points=[]
    
        for y in range(0, len(mini),4):
            for x in range(0,len(mini[y]),4):
                c1 = mini[y][x] > th and mini[y][x+1] > th and mini[y][x+2] > th and mini[y][x+3] > th  
                c2 = mini[y][x] > th and mini[y+1][x] > th and mini[y+2][x] > th and mini[y+3][x] > th
                if c1 or c2:
                    
                    start_point=(x*scale_percent,y*scale_percent)
    
                    points.append(start_point)
    
                    color1=(0,0,255)
                    color2=(0,255,255)
                    thickness=2
                    cv2.circle(frame, start_point, 20, color1, thickness) 
        
        if len(points) >= 2:
            cx1 , cy1 = points[0][0] , points[0][1]
            cx2 , cy2 = points[-1][0] , points[-1][1]
    
            cxmin = min(cx1,cx2)
            cymin = min(cy1,cy2)
    
            cxmax = max(cx1,cx2)
            cymax = max(cy1,cy2)
    
            print(cymin,cymax , '--' , cxmin,cxmax)
    
            cv2.rectangle(frame, (cxmin,cymin) , (cxmax,cymax), color2, thickness)
    
    
        
        # Display the resulting frame 
        cv2.imshow('frame4', frame)
        cv2.imshow('framemin', mini)
    
        last_edges = edges.copy() 
        fnum+=1
    
        if cv2.waitKey(33) & 0xFF == ord('q'):
            break
    
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    
    

    您也可以应用自己的蒙版来检测一个或另一个正在使用模糊值的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多