# Why does use local variable in Mat::resize()?

**URL:** <https://forum.opencv.org/t/why-does-use-local-variable-in-mat-resize/15988>\
**Category:** C++\
**Tags:** mat, core\
**Created:** [December 28, 2023, 8:55am UTC](https://forum.opencv.org/t/why-does-use-local-variable-in-mat-resize/15988 "2023-12-28T08:55:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![whiplash](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@whiplash](https://forum.opencv.org/u/whiplash)\
**Post date:** [December 28, 2023, 8:55am UTC](https://forum.opencv.org/t/why-does-use-local-variable-in-mat-resize/15988/1 "2023-12-28T08:55:44Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/aed815b302a5e69eba8cfd54da5a614baa4c9dc5.png)

in the function of Mat::resize(), line 1048 & 1049, where dose use local variable ‘part’, is it valid that assign ‘s’ to ‘part’? or ‘part’ here means the ‘Mat’ object itself ?

---

<div class="post-metadata">

**Author:** ![matti.vuori](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@matti.vuori](https://forum.opencv.org/u/matti.vuori)\
**Post date:** [December 29, 2023, 7:51am UTC](https://forum.opencv.org/t/why-does-use-local-variable-in-mat-resize/15988/2 "2023-12-29T07:51:42Z")

</div>

Read the function’s documentation and think based on that what the role of s is and what part represents.

---

<div class="post-metadata">

**Author:** ![whiplash](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@whiplash](https://forum.opencv.org/u/whiplash)\
**Post date:** [January 1, 2024, 10:51am UTC](https://forum.opencv.org/t/why-does-use-local-variable-in-mat-resize/15988/3 "2024-01-01T10:51:12Z")

</div>

I’m new for opencv, I’ve thought ‘part’ is just a local variable. so here the function rowRange return the address of the Mat itself, its function is similar to reference, assign s to part could modify the value of part, so that the value in rowRange of Mat will be modified.

---

<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:** [January 1, 2024, 12:55pm UTC](https://forum.opencv.org/t/why-does-use-local-variable-in-mat-resize/15988/4 "2024-01-01T12:55:26Z")

</div>

the first line creates a slice/view over the new part of the matrix (it was resized). that new part hasn’t been initialized yet.

the second line has the form `Mat = Scalar`, which has special semantics. it assigns the scalar to every element of that view.

---

<div class="post-metadata">

**Author:** ![whiplash](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@whiplash](https://forum.opencv.org/u/whiplash)\
**Post date:** [January 2, 2024, 12:37am UTC](https://forum.opencv.org/t/why-does-use-local-variable-in-mat-resize/15988/5 "2024-01-02T00:37:51Z")

</div>

thank you. at the begin, i’ve thought the ‘part’ js just a local variable, after leaving it’s scope, it will destruct. now i think it’s just same as reference, it’s address is pointint at the part of Mat.
