# How to save/load the whole internal states (not the parameters) of the BackgroundSubtractors, OpenCVSharp?

**URL:** <https://forum.opencv.org/t/how-to-save-load-the-whole-internal-states-not-the-parameters-of-the-backgroundsubtractors-opencvsharp/15447>\
**Category:** C++\
**Tags:** csharp\
**Created:** [November 8, 2023, 2:22pm UTC](https://forum.opencv.org/t/how-to-save-load-the-whole-internal-states-not-the-parameters-of-the-backgroundsubtractors-opencvsharp/15447 "2023-11-08T14:22:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aram\_Nasser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/aram_nasser/32/9031_2.png) [@Aram\_Nasser](https://forum.opencv.org/u/Aram_Nasser)\
**Post date:** [November 8, 2023, 2:22pm UTC](https://forum.opencv.org/t/how-to-save-load-the-whole-internal-states-not-the-parameters-of-the-backgroundsubtractors-opencvsharp/15447/1 "2023-11-08T14:22:43Z")

</div>

I have successfully used OpenCVSharp, specifically the BackgroundSubtractorGMG, BackgroundSubtractorKNN, and BackgroundSubtractorMOG2, to perform background subtraction on images. However, when attempting to save the trained model, I have found that I can only save and load the parameters used to configure it!

Do you have any suggestions on how I can save the entire model with its learned state for future use? If there isn’t a direct way to achieve this, is it feasible to make modifications to the library to enable such functionality?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [November 8, 2023, 2:38pm UTC](https://forum.opencv.org/t/how-to-save-load-the-whole-internal-states-not-the-parameters-of-the-backgroundsubtractors-opencvsharp/15447/2 "2023-11-08T14:38:26Z")

</div>

you cant save the state (or learned model)  
(it’s not a csharp problem)

what is your actual use-case, which would require this ?

> [@Aram\_Nasser](#):
>
> to perform background subtraction on images.

it’s meant to be used on continuous video streams to find moving objects (and assumes you have a few hundred frames to spare on startup to ‘train’ it.)

can it be, you’re looking for some more general _object segmentation_ on _single images_ ?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 8, 2023, 5:36pm UTC](https://forum.opencv.org/t/how-to-save-load-the-whole-internal-states-not-the-parameters-of-the-backgroundsubtractors-opencvsharp/15447/3 "2023-11-08T17:36:03Z")

</div>

`cv::algorithm` has read(), write(), save() instance methods and load(), read() static methods

BackgroundSubtractor, being a subclass of cv::algorithm, _should_ mean that further subclasses implement those… hopefully with sensible semantics.

[crosspost](https://meta.stackoverflow.com/a/266159):

> <https://stackoverflow.com/questions/77445147/how-to-save-load-the-whole-internal-states-not-the-parameters-of-the-backgroun>

---

<div class="post-metadata">

**Author:** ![Aram\_Nasser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/aram_nasser/32/9031_2.png) [@Aram\_Nasser](https://forum.opencv.org/u/Aram_Nasser)\
**Post date:** [November 9, 2023, 1:14pm UTC](https://forum.opencv.org/t/how-to-save-load-the-whole-internal-states-not-the-parameters-of-the-backgroundsubtractors-opencvsharp/15447/4 "2023-11-09T13:14:00Z")

</div>

Thanks for your response, Berak,

I aim to kick off the inference right from the get-go, skipping the training process every time the system starts. Consequently, I aim to store the most recent model state and start using it promptly upon startup.

As far as I am concerned, there is no direct way to do that. I want to know where can I modify the implementation or, maybe, add a new method for that functionality!

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [November 9, 2023, 2:53pm UTC](https://forum.opencv.org/t/how-to-save-load-the-whole-internal-states-not-the-parameters-of-the-backgroundsubtractors-opencvsharp/15447/5 "2023-11-09T14:53:11Z")

</div>

> [@Aram\_Nasser](#):
>
> As far as I am concerned, there is no direct way to do that.

it’s not about you, there _IS_ no way to do that builtin.

> [@Aram\_Nasser](#):
>
> I want to know where can I modify the implementation or, maybe, add a new method for that functionality!

you’d have to dig into the resp. [cpp source here](https://github.com/opencv/opencv/blob/4.x/modules/video/src/bgfg_gaussmix2.cpp#L289-L321)

again, there _are_ read/write methods, so it might even go without modifying interfaces
