OpenCV 背景减法学习率不能改变 [英] OpenCV background subtraction learning rate cannot change
问题描述
我希望用 50 帧训练一个背景区域,并使用这个预先训练的模型进行背景减法.模型在训练后停止更新.
I wish to train a background region with 50 frames and use this pre-trained model for background subtraction. Model stops updating after training.
这是我的代码
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()"中.
The learning rate parameter is in the function "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;"
但是,我在这里尝试了几种学习率:
However, I tried several learning rate here:
fgmask = fgbg.apply(Img, -1) or
fgmask = fgbg.apply(Img, 0) or
fgmask = fgbg.apply(Img, 1) or
fgmask = fgbg.apply(Img, 0.00001)
训练背景结果没有改变.这意味着我不能在测试时保持预训练模型不变!
The training Background result does not change. This means I CANNOT keep pre-trained model unchanged while testing!
我的代码有问题吗?有没有办法改变学习率?
Is there anything wrong with my code? Is there any way to change the learning rate?
这是一些结果
从上面的结果可以明显看出,虽然我将学习率设置为 0,但训练后的背景图像在测试时会发生变化.
From the result above, it is clear that the trained background image changes while testing, although I set learning rate as 0.
fgmask1 = fgbg.apply(Img, 0)
推荐答案
所以使用python实现的正确方法是
So the correct way to use the python implementation is
fgbg = cv2.createBackgroundSubtractorMOG2(50, 16, False)
fgbg.apply(input, output, learning_rate)
与在 C++ 实现中完全一样.学习率必须是第三个参数.
Exactly as in the c++ implementation. The learning rate must be the third parameter.
这篇关于OpenCV 背景减法学习率不能改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!