博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vibe目标检测python代码
阅读量:4303 次
发布时间:2019-05-27

本文共 5191 字,大约阅读时间需要 17 分钟。

#! /usr/bin/env pythonfrom cProfile import Profileimport pstatsimport numpy as npimport cv2import randomimport timeimport matplotlib.pyplot as pltdefaultNbSamples = 20defaultReqMatches = 2defaultRadius = 20defaultSubsamplingFactor = 16background = 0foreground = 255def Initial_ViBe(gray, samples):    # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    height = gray.shape[0]    width = gray.shape[1]    print("*****Initlize Time******")    start1 = time.clock()    for i in range(height):        for j in range(width):            for k in range(defaultNbSamples):                rand = round(random.uniform(-1, 1))                r = i + rand                if r < 0:                    r = 0                if r > height:                    r = height - 1                rand = round(random.uniform(-1, 1))                c = j + rand                if c < 0:                    c = 0                if c > width:                    c = width - 1                samples[i][j][k] = gray[i][j]            samples[i][j][defaultNbSamples - 1] = 0    end = time.clock()    print(end - start1)    print("******************************")    return samplesdef update(gray, samples):    height = gray.shape[0]    width = gray.shape[1]    # print(height, width)    # segMat = np.zeros((gray.shape[0], gray.shape[1], 1), np.uint8)    segMat = np.zeros((gray.shape[0], gray.shape[1], 1))    for i in range(height):        for j in range(width):            count = 0            idex = 0            dist = 0            # 遍历每个像素,判断与背景样本的相似程度            while count < defaultReqMatches and idex < defaultNbSamples:                dist = abs(gray[i][j] - samples[i][j][idex])                if dist < defaultRadius:                    # 统计相似度小于阈值的个数                    count = count + 1                idex = idex + 1            # 大于#min,则认为该点为背景点            if count >= defaultReqMatches:                # 判断为背景像素,只有背景点才可以被用来传播和更新存储样本值                samples[i][j][defaultNbSamples - 1] = 0                # 输出二值图像用                segMat[i][j] = background                rand = round(random.uniform(0, defaultSubsamplingFactor - 1))                if rand == 0:                    rand = round(random.uniform(0, defaultNbSamples - 1))                    samples[i][j][rand] = gray[i][j]                rand = round(random.uniform(0, defaultSubsamplingFactor - 1))                if rand == 0:                    rand = round(random.uniform(-1, 1))                    iN = i + rand                    # 以下是防止越界                    if iN < 0:                        iN = 0                    if iN > height:                        iN = height - 1                    rand = round(random.uniform(-1, 1))                    jN = j + rand                    if jN < 0:                        jN = 0                    if jN > width:                        jN = width - 1                    rand = round(random.uniform(0, defaultNbSamples - 1))                    samples[i][j][rand] = gray[i][j]            else:                segMat[i][j] = foreground                samples[i][j][defaultNbSamples -                              1] = samples[i][j][defaultNbSamples - 1] + 1                if samples[i][j][defaultNbSamples - 1] > 50:                    rand = round(random.uniform(0, defaultNbSamples - 1))                    if rand == 0:                        rand = round(random.uniform(0, defaultNbSamples - 1))                        samples[i][j][rand] = gray[i][j]    return segMat, samplesdef main():    vc = cv2.VideoCapture("video.avi")    c = 0    if vc.isOpened():        rval, frame = vc.read()    else:        rval = False    samples = np.zeros((500, 500, defaultNbSamples))    while rval:        rval, frame = vc.read()        if c == 1:            # 将输入转为灰度图            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)            samples = Initial_ViBe(gray, samples)        else:            # 将输入转为灰度图            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)            # 输出二值图            (segMat, samples) = update(gray, samples)            # 转为uint8类型            segMat = np.array(segMat, np.uint8)            # 形态学处理模板初始化            kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))            # 开运算            opening = cv2.morphologyEx(segMat, cv2.MORPH_OPEN, kernel1)            # 形态学处理模板初始化            kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))            # 闭运算            closed = cv2.morphologyEx(segMat, cv2.MORPH_CLOSE, kernel2)            # 寻找轮廓            Img, contours, hierarchy = cv2.findContours(                closed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)            for i in range(0, len(contours)):                x, y, w, h = cv2.boundingRect(contours[i])                print(w * h)                if w * h > 400 and w * h < 10000:                    cv2.rectangle(frame, (x, y), (x + w, y + h),                                  (0, 255, 0), 2)            cv2.imshow("frame", frame)            cv2.imwrite("./result/" + str(c) + ".jpg", frame,                        [int(cv2.IMWRITE_PNG_STRATEGY)])            k = cv2.waitKey(1)            if k == 27:                vc.release()                cv2.destroyAllWindows()        c = c + 1main()

 

转载地址:http://syhws.baihongyu.com/

你可能感兴趣的文章
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>
8、JavaWEB学习之基础篇—文件上传&下载
查看>>
reRender属性的使用
查看>>
href="javascript:void(0)"
查看>>
h:panelGrid、h:panelGroup标签学习
查看>>
f:facet标签 的用法
查看>>
<h:panelgroup>相当于span元素
查看>>
java中append()的方法
查看>>
必学高级SQL语句
查看>>
经典SQL语句大全
查看>>
Eclipse快捷键 10个最有用的快捷键
查看>>
log日志记录是什么
查看>>
<rich:modelPanel>标签的使用
查看>>
<h:commandLink>和<h:inputLink>的区别
查看>>
<a4j:keeyAlive>的英文介绍
查看>>
关于list对象的转化问题
查看>>
VOPO对象介绍
查看>>