程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Meanshift image segmentation and video background separation in opencv (implemented in Python)

編輯:Python

List of articles

    • 1.MeanShift principle
    • 2. Video foreground and background separation
      • (1)MOG2 Remove the background

1.MeanShift principle

(1) Strictly speaking, this method is not used to segment the image , It's a smooth filter at the color level ;
(2) It will neutralize colors with similar color distribution , Smooth color details , Erode away smaller areas of color ;
(3) It takes any point in the image P For the center of a circle , The radius is sp, The color amplitude is sr Keep iterating ;

pyrMeanShiftFiltering(src, sp, sr, dst=None, maxLevel=None, termcrit=None):

Src: Input the original image ;
Sp: Double precision radius , The bigger the value is. , The more fuzzy ;
Sr: The range of amplitude variation of color , The greater the range of variation , The larger the area becomes .
Dst: Output image ;
maxLevel: The default value is 1;
Termcrit: Termination criteria : When to stop meanshift iteration .

import os
import cv2
import numpy as np
img=cv2.imread('images/lenna.png')
img=cv2.resize(src=img,dsize=(450,450))
# Image segmentation
dst=cv2.pyrMeanShiftFiltering(src=img,sp=20,sr=30)
# Image segmentation ( Edge treatment )
canny=cv2.Canny(image=dst,threshold1=30,threshold2=100)
# Find the outline
conturs,hierarchy=cv2.findContours(image=canny,mode=cv2.RETR_EXTERNAL,method=cv2.CHAIN_APPROX_SIMPLE)
# Draw the outline
cv2.drawContours(image=img,contours=conturs,contourIdx=-1,color=(0,255,0),thickness=3)
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.imshow('canny',canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')


Canny Edge detection algorithm :
https://mydreamambitious.blog.csdn.net/article/details/125116318
Image search findHomography:
https://mydreamambitious.blog.csdn.net/article/details/125385752


2. Video foreground and background separation

(1)MOG2 Remove the background

stay createBackgroundSubtractorMOG On the basis of that, we improved ;

Gaussian mixture model based foreground or background segmentation algorithm

createBackgroundSubtractorMOG2(history=None, varThreshold=None, detectShadows=None):

History: How long reference frame is required for modeling , The default value is 200;
varThreshold: Judge whether the background model can describe the pixels well .
detectShadows: Shadow detection ;

import os
import cv2
import numpy as np
# Turn on the camera
cap=cv2.VideoCapture('video/University_Traffic.mp4')
# Create foreground separation object
bgsegment=cv2.createBackgroundSubtractorMOG2()
while cap.isOpened():
OK,frame=cap.read()
if OK==False:
break
frame=cv2.resize(src=frame,dsize=(500,500))
fgmask=bgsegment.apply(frame)
cv2.imshow('img',fgmask)
if cv2.waitKey(1)&0xFF==27:
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')

You can see from the video frame MOG2 It produces a lot of noise , Therefore, an improved method is proposed :
GMG The method of removing the background :
Static background image estimation and Bayesian segmentation of each pixel are more robust to noise ;


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved