首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cv2.GaussianBlur多种内核大小

cv2.GaussianBlur多种内核大小
EN

Stack Overflow用户
提问于 2021-01-15 12:23:45
回答 1查看 78关注 0票数 0

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

代码语言:javascript
复制
    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次。但是我的设备有资源限制,所以我想尽可能减少这个过程。我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2021-01-16 00:17:18

在掩码上尝试按位函数。检测每个像素是否在移动。它很快。

对我来说,诀窍是处理小尺寸的frame图像。

代码语言:javascript
复制
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()

您可以应用您自己的遮罩来检测一个或另一个玩模糊值的对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65730493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档