# OpenCV C++ Sliding Window Object Detection [2025]

> Opencv C++ tutorial about object detection with sliding window. A sliding window is easy to implement on a single scale and also not to much harder to…

- Canonical URL: https://www.funvisiontutorials.com/blog/opencv-cpp-sliding-window-object-detection-2025/
- Author: Vladimir Kucera
- Published: 2015-12-08
- Updated: 2026-02-20
- Topics: Installing & building OpenCV, Object detection, Image processing

Opencv C++ tutorial about object detection with sliding window. A sliding window is easy to implement on a single scale and also not to much harder to implement in a multi-scale for example detection inside the bigger mat. I would like to visualize all the steps during the code and describe in a natural C++ way. As a // comments. Enjoy the coding.

The first tutorial about mat resizing is [Mat Resize](/blog/mastering-opencv-cv-resize-quality-aspect-ratios/)
Second tutorial mat roi [Roi](/blog/opencv-roi-guide-extract-regions-interest-cpp/)

## Opencv installation for the tutorial

You can simply prepare the project inside the Visual Studio 2015 by Nuget Packages. This approach is easy for beginners and better than standard installation with all the environmental variables problems. Just follow the installation steps [here](/blog/opencv-31-opencv-2413-instalation-visual-studio/)

I am using Visual Studio 2015,  How to use Opencv 3.0.0 with Visual Studio can be found here [install opencv visual studio 2015](/blog/install-opencv-visual-studio-2015/).

## Sliding window for detection openCV code

**#include &lt;Windows.h>**
**#include "opencv2\\highgui.hpp"**
**#include "opencv2\\imgproc.hpp"****#include "opencv2/video/background\_segm.hpp"**
**#include "opencv2/video/tracking.hpp"**
**using namespace cv;**
**using namespace std;**
**int main(int argc, const char\*\* argv)**
**{**
_//_
_//  Load the image from file_
_//_
**Mat LoadedImage;**
_// Just loaded image Lenna.png from project dir to LoadedImage Mat_
**LoadedImage = imread("Lenna.png", IMREAD\_COLOR);**
_//I would like to visualize Mat step by step to see the result immediately._
_// Show what is in the Mat after load_
**namedWindow("Step 1 image loaded", WINDOW\_AUTOSIZE);**
**imshow("Step 1 image loaded", LoadedImage);**
**waitKey(1000);**

[Image: Opencv Tutorial Sliding Window]

_// Same the result from LoadedImage to Step1.JPG_
**imwrite("Step1.JPG", LoadedImage);**

_// Parameters of your slideing window_

**int windows\_n\_rows = 60;**
**int windows\_n\_cols = 60;**
         _// Step of each window_
        **int StepSlide = 30;**

_// Just copy of Loaded image_
_// Note that Mat img = LoadedImage; This syntax only put reference on LoadedImage_
_// Whot does it mean ? if you change img, LoadeImage is changed 2._
_// IF you want to make a copy, and do not change the source image- Use clone();_
**Mat DrawResultGrid= LoadedImage.clone();**

                _// Cycle row step_
**for (int row = 0; row &lt;= LoadedImage.rows - windows\_n\_rows; row += StepSlide)**
**{**
     _// Cycle col step_
**for (int col = 0; col &lt;= LoadedImage.cols - windows\_n\_cols; col += StepSlide)**
**{**
_// There could be feature evaluator  over Windows_
_// resulting window_
**Rect windows(col, row, windows\_n\_rows, windows\_n\_cols);**

**Mat DrawResultHere = LoadedImage.clone();**

_// Draw only rectangle_
**rectangle(DrawResultHere, windows, Scalar(255), 1, 8, 0);**
_// Draw grid_
**rectangle(DrawResultGrid, windows, Scalar(255), 1, 8, 0);**

_// Show  rectangle_
**namedWindow("Step 2 draw Rectangle", WINDOW\_AUTOSIZE);**
**imshow("Step 2 draw Rectangle", DrawResultHere);**
**waitKey(100);**
**imwrite("Step2.JPG", DrawResultHere);**

**[Image: Opencv Tutorial Sliding Window]**

_// Show grid_
**namedWindow("Step 3 Show Grid", WINDOW\_AUTOSIZE);**
**imshow("Step 3 Show Grid", DrawResultGrid);**
**waitKey(100);**
**imwrite("Step3.JPG", DrawResultGrid);**

[Image: Opencv Tutorial Sliding Window]

_// Select windows roi_
**Mat Roi = LoadedImage(windows);**

_//Show ROI_
**namedWindow("Step 4 Draw selected Roi", WINDOW\_AUTOSIZE);**
**imshow("Step 4 Draw selected Roi", Roi);**
**waitKey(100);**
**imwrite("Step4.JPG", Roi);**

**[Image: Opencv Tutorial Sliding Window]**

**}**
**}****}**
