# Background subtraction image

**URL:** <https://forum.opencv.org/t/background-subtraction-image/18613>\
**Category:** Python\
**Created:** [August 29, 2024, 3:00pm UTC](https://forum.opencv.org/t/background-subtraction-image/18613 "2024-08-29T15:00:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taka](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@taka](https://forum.opencv.org/u/taka)\
**Post date:** [August 29, 2024, 3:00pm UTC](https://forum.opencv.org/t/background-subtraction-image/18613/1 "2024-08-29T15:00:23Z")

</div>

I’m new for OpenCV and have a question for background subtraction.  
Is there a way to display how background model looks like?  
The 3rd line ends up with an error because the model isn’t an image object.  
However, the 1st line creates a static image like stationary objects (e.g. streets and buildings) without cars, I assume. Your feedback would be really helpful.

backSub = cv2.createBackgroundSubtractorMOG2 # create background model  
print(type(backSub)) # it returns \<class ‘cv2.BackgroundSubtractorMOG2’\>  
cv2.imshow(‘BG Mask’, backSub) # end up an error

---

<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:** [August 30, 2024, 4:59am UTC](https://forum.opencv.org/t/background-subtraction-image/18613/2 "2024-08-30T04:59:05Z")

</div>

> [@taka](#):
>
> Is there a way to display how background model looks like?

indeed, if you take a look at the docs 🙂 you’ll find the  
[getBackgroundImage()](https://docs.opencv.org/4.x/d7/df6/classcv_1_1BackgroundSubtractor.html#a98cb8e292a6cbcb32436dad62a82f974)  
method.  
however, this is sythesized at runtime, thus quite expensive !

---

<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:** [August 30, 2024, 9:32am UTC](https://forum.opencv.org/t/background-subtraction-image/18613/3 "2024-08-30T09:32:53Z")

</div>

note that’s a visualization and likely represents just part of the model’s state.

if you need to persist the entire model state, you should be able to use `read`, `write`, `save` methods.

---

<div class="post-metadata">

**Author:** ![taka](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@taka](https://forum.opencv.org/u/taka)\
**Post date:** [August 30, 2024, 11:08am UTC](https://forum.opencv.org/t/background-subtraction-image/18613/4 "2024-08-30T11:08:28Z")

</div>

The method returns what I wanted to see. Thanks!

---

<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:** [August 30, 2024, 12:46pm UTC](https://forum.opencv.org/t/background-subtraction-image/18613/5 "2024-08-30T12:46:42Z")

</div>

> [@crackwitz](#):
>
> if you need to persist the entire model state, you should be able to use `read`, `write`, `save` methods.

not really (unfortunately).  
i’ll save the parameters, but not the model state
