(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
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 ;