# How to do window sliding operation in a fast way?

**URL:** <https://forum.opencv.org/t/how-to-do-window-sliding-operation-in-a-fast-way/8452>\
**Category:** C++\
**Created:** [April 19, 2022, 2:05am UTC](https://forum.opencv.org/t/how-to-do-window-sliding-operation-in-a-fast-way/8452 "2022-04-19T02:05:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MakeItGreatAgain](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MakeItGreatAgain](https://forum.opencv.org/u/MakeItGreatAgain)\
**Post date:** [April 19, 2022, 2:05am UTC](https://forum.opencv.org/t/how-to-do-window-sliding-operation-in-a-fast-way/8452/1 "2022-04-19T02:05:59Z")

</div>

Hello,

As told by the title, I want to do a filter operation in pixel-wise.  
Suppose, for example, the filter size is M-by-M and it would be applied onto the image column-by-column for each line. That means the filter slides alongside the first row and moves to the next row and do it iteratively.

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [April 21, 2022, 9:59am UTC](https://forum.opencv.org/t/how-to-do-window-sliding-operation-in-a-fast-way/8452/2 "2022-04-21T09:59:06Z")

</div>

There are several things that you can do, depending on the application:

- avoid using Python. Four `for` loops will take forever to run.
- check if the filter is separable. It could reduce the MxM window to a M line and M column filter.
- check if you can do the operation using a pyramidal approach. This is more the case of a detection operation. First start with a reduced image size (let’s say by 4) and run a M/4xM/4 filter. Then, continue the operation on the interesting areas on higher resolutions.
- Sliding windows are quite easy to parallelize. Check [this answer](https://answers.opencv.org/question/90333/parallel-implementation-of-per-pixel-calculations/#90350) on how to do it simply using the TBB library.
- Going a step further, the **best solution** would be a massive parallelization on GPU. This kind of operation is really where GPUs shine.  
Fortunately OpenCV has already implemented this feature on CUDA (Nvidia graphics cards). Check [this tutorial](https://docs.opencv.org/3.4/d4/dbd/tutorial_filter_2d.html) on how to do it.

---

<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:** [April 21, 2022, 8:04pm UTC](https://forum.opencv.org/t/how-to-do-window-sliding-operation-in-a-fast-way/8452/3 "2022-04-21T20:04:39Z")

</div>

> [@kbarni](#):
>
> - avoid using Python. Four `for` loops will take forever to run.

you can do it, but use `numba` and `@njit` everything. add `parallel=True` and a `prange()` to use all cores.

generally, use `cv.filter2D()` and stop worrying.

---

<div class="post-metadata">

**Author:** ![MakeItGreatAgain](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MakeItGreatAgain](https://forum.opencv.org/u/MakeItGreatAgain)\
**Post date:** [April 22, 2022, 3:52am UTC](https://forum.opencv.org/t/how-to-do-window-sliding-operation-in-a-fast-way/8452/4 "2022-04-22T03:52:15Z")

</div>

```
  Merci beaucoup. 20 bytes.
```

---

<div class="post-metadata">

**Author:** ![MakeItGreatAgain](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MakeItGreatAgain](https://forum.opencv.org/u/MakeItGreatAgain)\
**Post date:** [April 22, 2022, 3:52am UTC](https://forum.opencv.org/t/how-to-do-window-sliding-operation-in-a-fast-way/8452/5 "2022-04-22T03:52:32Z")

</div>

Merci beaucoup. et 20 bytes.
