我希望训练一个 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)
训练背景结果不变。
这意味着我在测试时不能保持预训练模型不变!
我的代码有什么问题吗?
有什么办法可以改变学习率吗?
这是一些结果
测试图像 #1 的背景扣除结果 https://i.stack.imgur.com/Oeiy7.jpg
测试图像#40的背景扣除结果 https://i.stack.imgur.com/r3erO.jpg
从上面的结果可以明显看出,尽管我将学习率设置为0,但训练的背景图像在测试时发生了变化。
fgmask1 = fgbg.apply(Img, 0)