OpenCV背景减法学习率不能改变

时间:2017-02-21 03:32:59

标签: python c++ opencv

我希望训练一个50帧的背景区域,并使用这个预先训练的模型进行背景扣除。模型在训练后停止更新。

这是我的代码

import cv2
print "This program is for background subtraction with pre-trained model\n"

Training_Floder = "/Users/yuyang/Desktop/img1/"
Start_Frame_Num = 1
End_Frame_Num = 51

cv2.namedWindow("BG_IMAGE")

fgbg = cv2.createBackgroundSubtractorMOG2(50, 16, False)
font = cv2.FONT_HERSHEY_SIMPLEX



for index in range(Start_Frame_Num, End_Frame_Num):
    Img_File_Name = Training_Floder + str(index) + ".jpg"
    Img = cv2.imread(Img_File_Name)
    fgmask = fgbg.apply(Img, -1)
    BG_IMG = fgbg.getBackgroundImage()
    #######
    cv2.putText(BG_IMG,str(index),(10,500), font, 1,(255,255,255),2)
    cv2.imshow("BG_IMAGE", BG_IMG)
    #######
    cv2.waitKey(0)

Testing_Floder = "/Users/yuyang/Desktop/New/"
Test_Start = 1
Test_End = 100

for index in range(Test_Start, Test_End):
    Img_File_Name = Testing_Floder + str(index) + ".jpg"
    Img = cv2.imread(Img_File_Name)
    fgmask1 = fgbg.apply(Img, 0)
    BG_IMG1 = fgbg.getBackgroundImage()
    cv2.putText(BG_IMG1,str(index),(10,500), font, 1,(255,255,255),2)
    cv2.imshow("BG_IMAGE", BG_IMG1)
    cv2.waitKey(0)

基于评论

学习率参数在函数" apply()"。

@param learningRate 
The value between 0 and 1 that indicates how fast the background 
model is learnt. Negative parameter value makes the algorithm to 
use some automatically chosen learning rate. 0 means that the 
background model is not updated at all, 1 means that the background 
model is completely reinitialized from the last frame.

CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) = 0;"

但是,我在这里尝试了几种学习率:

fgmask = fgbg.apply(Img, -1) or
fgmask = fgbg.apply(Img, 0) or
fgmask = fgbg.apply(Img, 1) or
fgmask = fgbg.apply(Img, 0.00001)

训练背景结果不会改变。 这意味着我无法在测试时保持预先训练好的模型不变!

我的代码有什么问题吗? 有没有办法改变学习率?

以下是一些结果

Background subtraction result of Testing image #1

Background subtraction result of Testing image #40

从上面的结果可以清楚地看到,经过训练的背景图像在测试时会发生变化,尽管我将学习率设置为0。

fgmask1 = fgbg.apply(Img, 0)

1 个答案:

答案 0 :(得分:0)

所以使用python实现的正确方法是

fgbg = cv2.createBackgroundSubtractorMOG2(50, 16, False)
fgbg.apply(input, output, learning_rate)

与c ++实现中的完全相同。 学习率必须是第三个参数。