Opencv C++ Tutorial Sliding Window

Follow

My favourite

  • Opencv tutorial people detection
  • Head people cascade download
  • Opencv tutorial optical flow
  • Opencv Video stabilization
  • Opencv car dataset download
  • Opencv tutorial Transparent mask
  • Opencv videowriter
  • Opencv FFMPEG
  • Opencv Canny edge and hough lines
  • Sliding Window, search objects single scale

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

    First tutorial about mat resizeing is Mat Resize
    Second tutorial mat roi Roi

    Opencv instalation for the tutorial

    You can simple prepare the project inside the Visual Studio 2015 by Nuget Packages. This approach is easy for beginers and better than standard installation with all the environmental variables problems. Just follow the installation steps inside here
    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.


      Sliding window for detection opencv code


    #include <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);
            
    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 <= LoadedImage.rows - windows_n_rows; row += StepSlide)
    {
         // Cycle col step
    for (int col = 0; col <= 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);

                             
    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);

                          
    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);


    Opencv Tutorial Sliding Window


    }
    }

    }

    2 comments:

    ad

    Follow by Email

    Powered by Blogger.