gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:03:43 32 4
gpt4 key购买 nike

我希望用 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)

最佳答案

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

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

与在 c++ 实现中完全一样。
学习率必须是第三个参数。

关于python - OpenCV 背景减法学习率不能改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358155/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com