November 2015


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
  • Computer vision and brands taging

    This year, I have conducted a number of meetings with many people about same and same topics. Idea is simple but what about solution and realization. Is it simple to? 
    Let me comment or message pls. 

    What is going on?

    Brand recognition, automatic tags and statistics from the users picture. 

    If you know some kind of service like this pls let me know. I really want to discuss limitation and realization of these kind of projects. 

    We are all working on image segmentation and scene understanding using random trees, forest, ferns and deep learning methods. We are trying to find horses in images and tennis racket. . . . etc. I am really glad that, I am mainly working with detection up to 10 category. What about analysis and recognition of brands like on picture. And worse, Imagine case of analyze Instagram images and your output look like this.

    user: Vladimir, where: noLocated, brands: 2 [starbucks coffee, juice] 

    In advance the output know that Vladimir has juice and coffee probably at Starbucks. 

    Computer vision Brands analysis

    People around the world share tons of images from the pubs, restaurant and public places. Doing lots of thing and eat and drink more than million of products around the world. 

    We can find Coca-Cola logo multiscale, in different rotation and transformation over a lots of images, if I correct.  
    Can we do this with 1000 of brands over large images sets?  

    I have an meeting on Friday with potential customer. I can offer only 10 brand on really small sample of images per second. Sure If I have infinite amount of computer resources. Blab la bla J something like this is possible. Problem is price reasonable for the customer. Anyone has some experiences ?

    Position and vibration control

    This is really nice control example on experimental tool developed in our group at Czech Technical University in Prague. The experiment on video deal with oscillation of suspended load during helicopter maneuver. The goal is control the Helicopter position with standard control loops which are conventional in real cases. The second important goal is control vibration of suspended load. 

    Signal Shaper 

    In this examples is part of our research publicated in such prestigious journals[1] [2].

    [1]Vyhlidal T., Hromcik M., Kucera V. Anderle M., On feedback architectures with zero vibration signal, Accepted IEEE Transaction on Automatic Control as regular paper

    [2]Vyhlidal T., Kucera V., Hromcik M, Signal shapers with distribute
    delays: spectral analysis and design,  Automatica, Vol 49, Issue 11, November 2013, pp 3484-3489

    Basic Opencv Face Detection Tutorial

    Basic Opencv C++ tutorial how to detect the face from video image and any source you can achieve.
    If you have little bit skills with programing you can just achieve the result under 10 minutes. I would like to reccomend also the installation by NUGET package. It is the fastest approach in Visual Studio how to start coding with opencv.. 

    Problem with CascadeClassifier detectMultiScale Opencv 3.0.0. Windows prebuild libs

    Problem is that in many cases with 32 bit Opencv 3.0.0 Windows version in debug and in release mode. I thing any of haarcascade_xxx return vector<Rect> Faces (testing number of cascades is 7). The length of the vector is incomprehensibly bigger than I can expect with my parameters and experiences with previous Opencv version..

    vector<Rect> of detection is to big 

    This is some kind of bug that I try to find in opencv source. What is surprising is fact that 64 bit version of Opencv works fine in my case. 

     I am using Visual Studio 2015 with 64 bit Opencv 3.0.0

     Here is my tutorial how to install Opencv 3.0.0 libs for Visual Studio 2015. 
    Install Opencv for Visual Studio 2015
    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 

    Face detection basic Opencv C++ code example

    There is nothing special, but you can also find the example how to replace the face with transparent mask or any pictures. This is at least fun. 
    #include <opencv2/core.hpp>
    #include <opencv2/highgui.hpp>
    #include <opencv2/videoio.hpp>
    #include <opencv2/objdetect.hpp>
    #include <opencv2/imgproc.hpp>
    #include <iostream>
    using namespace cv;
    using namespace std;
    int main(int argc, char** argv)
     // capture from web camera init
     VideoCapture cap(0);;
     Mat img;
    // Load cascate classifier placed in sulution folder
     CascadeClassifier detector;
     string cascadeName = "haarcascade_frontalface_alt.xml";
     bool loaded = detector.load(cascadeName);
    // Parameters of detectMultiscale Cascade Classifier
     int groundThreshold = 2;
     double scaleStep = 1.1;
     Size minimalObjectSize(80, 80);
     Size maximalObjectSize(200, 200);
    // Vector of returned faces
     vector found;
     for (;;)
          // Image from camera to Mat
          cap >> img;
         // Convert input to greyscale 
          Mat image_grey;
          cvtColor(img, image_grey, CV_BGR2GRAY);
          // why not
          // Detect faces
          detector.detectMultiScale(image_greyHead, found, scaleStep, groundThreshold, 0         | 2, minimalObjectSize,maximalObjectSize);
          // Draw the results into mat retrieved from webcam
          if (found.size() > 0) {
                for (int i = 0; i <= found.size() - 1; i++) {
                 rectangle(img, found[i].br(), found[i].tl(), Scalar(0, 0, 0), 1, 8, 0);
        //Show the results
         imshow("wooohooo", img);
         int key2 = waitKey(20);
     return 0;

    Debian FFMPEG instalation 

    One day, my server Debian is upgraded to Jessie. Nice name but FFMPEG packages and Media repos are missing. This is not something special but i will be enthusiastic to have this script few days ago. Here is a script to download and make, install FFMPEG on linux Debian system from source.

    Debian FFMPEG instalation 

    You need to install some prerequisites.

    sudo apt-get -y install git make nasm pkg-config libx264-dev libxext-dev libxfixes-dev zlib1g-dev

    Download FFMPEG from the website

     sudo wget -O ffmpeg-2.8.tar.bz2 ""

    Unzip and remove if you like

    sudo tar -xvf ffmpeg-2.8.tar.bz2
    sudo rm -Rf ffmpeg-2.8.tar.bz2 
    Run configuretion in source Folder 

    ./configure --enable-nonfree --enable-gpl --enable-libx264 --enable-x11grab --enable-zlib

    Note here. -J2 depends on your number of processor cores. If you have 20 cores intel machine. Lets Run make -j20 

    make -j2

    Install and configure FFMPEG sudo make install sudo ldconfig -v

    Install opencv for Visual Studio 2015 

    Opencv tutorial how to build opencv from source in Visual Studio 2015. This is usefull when the new version just release and there is no prebuild library awailable..  If you download prebuild libs for windows Visual studio some times agou there is problem the newest VS just mussing. Lets checkt the version of libraries and VS.
    Prebuild libs are only for version VC11 and VC12. This mean Visual Studio 2012 and 2013.

    This step helps you compile your own opencv libs for VC14  - Visual Studio 2015 Community edition.

    Important !!
     Now a days just use NUGET packages in Visual studio and you can code under 1 minutes. here.

    Prepare third party libs for opencv 

    This step depends on your requirements. If you want python lets install python. But i can reccomend to install following parts. 

    Build opencv 3.0.0 

    Download Opencv 3.0.0 gold
    • Extract files folder into C:/opencv-3.0.0
    • Prepare Empty folder C:/opencv-build

    Build your own OPENCV by CMAKE and VISUAL STUDIO.

    Prepare Solution

    • install CMAKE
    CMake is an extensible, open-source system that manages the build process in an operating system.
    By this program you generate visual studio Opencv.sln solution file.

    Choose windows installer 

    Windows (Win32 Installer)

    Run Cmake


    cmake opencv

    Set field-

    •  Where is the source code.  C:/opencv-3.0.0   (downloaded from git)
    •  Where to build the binaries C:/opencv-build   (empty folder you create)
    Select configure button
    • Choose Visual Studio 14 2015    Win64 bits of 32 as you like.
    • I am using default native compilers options. 
    • Hit a Finish button
    In table name- Choose components you want to install. Just try default first,

    Now press Generate button an waith, 
    This process take some time and copy files and generate Visual Studio OpenCV.sln file in C:/opencv-build folder.

    Build and release Opencv 3.0.0 in Visual Studio 2015

    • Open Visual Studio 2015 
    • By file-open-project choose OpenCV.sln in C:/opencv-build
    • Visual studio open this projects structure.
    Build opencv Visual Studio 2015

    • Choose Debug and green start. 
    • After that choose Release and press green start. 

    Opencv 3 visual studio 2015

    This build and release process can generate some errors.
    Look at the details. All this errors in my case are in performance test parts.

    • Ok  Some errors but in test parts. I dont care. Let install opencv.

    Install opencv Visual Studio 2015

    Install Opencv, Windows 7, Windows 10, Visual Studio 2015

    Visual Studio 2015 64 bit version

    In case you open your build folder before this step and you cant find Opencv Libs and Headers. 
    You need to do following.
    • Choose INSTALL project in visual studio like on picture.
    • Build Release only this install project
    Install opencv Visual Studio 2015
    Install your project

    The result is 1 succesfully build solution and 0 failded as on picture.

    Your installation of opencv is c:/opencv-build/install

    There is your header and libs for Visual studio 2015.
    i have got only x64 version.

    Test Opencv Visual Studio 2015

    Include Project in Visul Studio.

    Choose Property pages of the project.
             Additional Include Directory as C:\opencv-build\install\include
             Additional #using Directory as C:\opencv-build\install\include
             Additional Library Directories as C:\opencv-build\install\x64\vc14\lib
              Additional Dependencies add some libs like

    And you can build your Opencv program. You can copy mine from the image but choose your picture.

    Opencv 3.0.0 Visual Studio 2015

    This is simple tracking example of moving object.

     Basic concept is simple as following steps 

    1. Use background subtraction   Code Here
    2. Draw Rectangle over object  (blue) (Use coordinates of center (X,Y))
    3. Init Kalman filter (red object) with detected (X,Y) coordinates
    4. If measured position of the object is available update kalman filter. 
    5. If measured position is not available just read predicated state of kalam filter

    Opencv VideoCapture File, Camera and stream

    Opencv tutorial simple code in C++ to capture video from File, Ip camera stream and also the web camera plug into the computer.. The key is to have installed the FFMPEG espetially in case of reading the strem of IP cameras. In windows just use Opencv Installation by Nugets packages Here. Simple easy under 2 minutes instalation. In Linux you need to follow the instruction bellow. If you are on Debian Like package system. Under Fedora Red hat dist just use different aproach. Code is simple and installation is the key.. 

    Windows use nugets packages
    Linux you have to install and build Opencv With FFMPEG. Also simple. 

    It is easy capture video in opencv

    Video capture
     in opencv is really easy task, but for little bit experienced user. 

    What is problem?
    Problem is instalation of Opencv without recommended dependencies.

    Just install all basic libs that is recommended on website.
    # Basic packages
    sudo apt-get -y install build-essential
    sudo apt-get -y install cmake
    sudo apt-get -y install pkg-config
    # Basic dependencies
    sudo apt-get -y install libgtk2.0-dev python-dev python-numpy
    # OpenCV dependencies part II.
    sudo apt-get -y install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils libv4l-0 libv4l-dev
    sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev
    sudo apt-get -y install libdc1394-22-dev libdc1394-utils
    sudo apt-get -y install libjpeg-dev libpng-dev libtiff-dev libjasper-dev
    sudo apt-get -y install libtiff5 libtiff5-dev
    sudo apt-get -y install libopenexr-dev
    sudo apt-get -y install libjasper-dev
    # Algebra
    sudo apt-get -y install libeigen3-dev

    Install ffmpeg .

    You need to download and build by own in case of Debian Jessie or some version of Ubuntu.
    Just download, extract tar archive ffmpeg.tar.bz2.

    1 configure instalation ./configure -- with Right Params.

    2 Make - make -j4

    3 install - make install

    4 write config ldconfig -v

    Compile opencv

    Check this table during the opencv instalation. if you have FFMPEG and all AV libs 
    AVCODEC AVFORMAT AVUTIL AVSWSCALE you can continue with opencv installation.
    opencv config ffmpef
    Opencv config

    Read video file in Opencv

    VideoCapture capture("input.mp4");

    Read rtsp stream in Opencv

    VideoCapture capture("rtsp://");
    Some of my next post will be about rtsp stream format. PLS subscribe.

    Read web camera in Opencv

    VideoCapture capture(0);

    Opencv 3 and higher c++ code

    using namespace std;
    using namespace cv;
    //Chose input
    //VideoCapture capture(0);
    VideoCapture capture("rtsp://");
    //VideoCapture capture("input.mp4");
    // create mat to fill by external source
    Mat frame;
           bool OK = capture.grab();
            if (OK == false){
            cout << "cannot grab" << endl;
              // retrieve a frame of your source
             // capture >> frame;

    The delayed resonator 

    is approach for controlling vibration of mechanical system or other flexible structures, where external forces is periodic signal which excited vibration of the main structure. 

    voice coil
    This setup using 2 voice coile motors. The smaller one is source of external periodical vibration and the longer one is resonator actuator drive by accelerometer attach on resonator body.

    If you want some mathematical description please let me a comment.

    About Martingate

    This really old betting strategy was popular in 18th century in France. Through the ages there are many modification and improvement to reduce risk. This strategy is still powerfull but you have to follow strict rulles to reduce risk to minimum. For humans it is the hardest thing to admit some losses and stop them before they lose so much money.  

    Betting strategy

    In basic this strategy is really simple. 
    Consider betting only on Red and Black numbers. Choose only one of them and bet your initial amount of the money. 

     Start betting with initial value 1$ to Red. 
    -If you win lets start again with initial value to same color. 
    -If you lose lets bet double at same color. 2$
    -If you lose agail bet double than in previous round. 4$
    In any case you win let start again with the initial value. 

    Imagine this series. 
    bet 1$ fail      -1$
    bet 2$ fail      -3$ total
    bet 4$ fail      -7$ total 
    bet 8$ win      +1$ in total

    This is dangerously great because in any case you lose you have your money back + 1$.


    •  Long fail series is more probable than you thought. It is very possible that you can encounter with 10 of more fail in the row. 

    1 2 4 8 16 32 64 128 256 512  = That means your total result is -1023$ total. 
    If you bet nex round double 1024 you have +1$ in total result. 
    if you lose again you are in deep trouble. 

    • Limits in many games you have maximal value of your bet. The first is  the amount of money you have available for the game. The second one are limits by the game rule as maximal amout value you can bet.  
    • If your limit is 50$. The number of fail round is reduced to 6 and after that you never back your money by this strategy. This is very dangerous factors. 


    Consider the simulation without the limits in game. 
    Start with 10 dollar tipical situation of good result. in 1000 round you end with 25 dollar. 
    Maybe is better get a job :) 

    Martingate simulation

    Roulette simulation martingate
    Another situation after 1000 rounds you lose about 5$.
    This simulation show many games in one graph. The blue simulation ends well. Your final amout of money ends in good numbers. The red simulation you lose your money. 

    Martingate simulation
    Positive turns in game. You can turn your game after few loses, but imagine the 10 fail in the row. In few rounds you end up with -1000$ and as you can see after 10000 rounds is more likely that you can win about 10 or 50 dolars. This is also without a limits for the max value of your bet. 

    Martingate simulation

    roulette Martingate simulation

    Face detection and Transparent image in opencv 

    Face detection Opencv C++ tutorial about how to replace the face with mask. Easy steps to achieve results like many popular applications that just enhance your face. Code is just part that we already have available video reader written in opencv and just apply the face detection part with mask over the detected face. 

    Pixel by pixel color value filtering

    Let me explain how to add a transparent mask over the ROI in a video sequence. 
    This image of the anonymous mask has a white background. Add this image to the selected area  pixel by pixel with following condition. Add mask to the background video if the color of the mask is black. It s really simple idea. Image could be added over the roi in different way, but in the case you want to filter by pixel color value. This is example for you.

    Transparent Mask Opnecv

    Code Opencv Tutorial

    Just remember you need to at least figure out how to load the image or video into the IMG source mat. 
    //Normal face detection by haar cascade in opencv
      CascadeClassifier faceDetector;
      bool loaded1 = faceDetector.load("facehaarcascade.xml");
      Mat original;  img.copyTo(original);
      std::vector faces;  
      cvtColor(img, img, CV_BGR2GRAY); 
      equalizeHist(img, img);
      detectorBody.detectMultiScale(img, faces, 1.1, 2, 0 | 1, Size(40, 40), Size(400,400 ));
    Mat Original; 
    //Read the anonymous mask 
    Mat imageMask = imread("MASK.png", 1);
    // Draw the mask over all rectangles
    for( size_t i = 0; i < faces.size(); i++ )
          int xx =0, yy =0;
      // Just iterate in face region pixel by pixel
         for(int x = faces[i].x; x < faces[i].x+faces[i].width; x++)
                  for (int y = faces[i].y; y < faces[i].y+faces[i].height; y++)
                         //This is core of the trick. 
                         // Copy Mask to Original image  If the 0 chan
                         // Proper condition is over all color channels
                         //,yy)[0] < 10
                         //,yy)[1] < 10
                         //,yy)[2] < 10
                                                   // Black Color(0,0,0)
                            if (,yy)[0] < 10)
                              // Copy to original image on (y,x) places  the pixel of xx,yy mask
                    ,x)[0] =  imageMask .at(xx,yy)[0];
                    ,x)[1] =  imageMask .at(xx,yy)[1];
                    ,x)[2] =  imageMask .at(xx,yy)[2]; 
                              // Iterate in mask x
                      xx =xx+1; 
          // iterate next row of imageMask
          xx = 0;
          yy =yy+1;

    Opencv face detection and mask over the face

    Face detection is just the basic think you can do with the Opencv for fun. You can place the picture over the face. You can just replace by transparent mask like on video down here. Its easy and fun staff that you can achieved in 1 hour if you havent got any opencv skills, but you know somethink about the C++. Check the video example here and go to the tutorial :) Enjoy Codiang 

    Is there anyone who wants to know how to do it ? Tutorial is Here

    My future tutorials will be about more complicated methods like tracking of moving objects using background segmentation (First post). This is one year is old video here. I show you how to do this much better than that. :) 

     I would like to write something about  face detection and background removing based on colors, see youtube video here. This is nice technique used in film postproduction and TV to remove blue, green background from the video and replace with image or something else. 

    I also have prepared articles related to control theory and computer vision examples like kalman filtering in theoretical point of view and nice application in vision. Also, i am working on theoretical introduction to basics about LQR control and LQG and related nice practical examples. 

    Opencv tutorial C++ Background substraction

    Simple Opencv C++ code example how to from video remote the foreground from the background. Code is well described and working under opencv 3 and higher without any problems. I would like to reccomend instalation using the NUGET packages in case of Windows Visual Studio Development. Just visit the example how to install here.

    The technique to remove foreground from the background is basic technique to capture evants in web cameras or track the moving objects. In next blog post, I will show you how to draw bounding rectangle over the moving objects. The third blog in line apply kalman filter to track moving objects. This is not a people detection based technique. In case of  shaking, moving and variable light conditions this technique  doesn’t  work properly. In that case. Use haar cascades of LBP or something much better than background.. Lets visit this topics if you are interested Here

    I hope that the code is clear and well described. You can leave me a comment here.

    Background sustraction C++ video

    Opencv C++ background substrack code

    #include "opencv2/highgui.hpp"
    #include "opencv2/imgproc.hpp"

    #include <vector>
    #include <fstream>
    #include <iostream>
    #include <math.h>
    #include <Windows.h>

    #include "opencv2/video/background_segm.hpp"
    #include "opencv2/highgui.hpp"
    #include "opencv2/imgproc.hpp"

    using namespace cv;
    using namespace std;

    int main(int argc, const char** argv)
    // Init background substractor
    Ptr<BackgroundSubtractor> bg_model = createBackgroundSubtractorMOG2().dynamicCast<BackgroundSubtractor>();

    // Create empy input img, foreground and background image and foreground mask.
    Mat img, foregroundMask, backgroundImage, foregroundImg;

    // capture video from source 0, which is web camera, If you want capture video from file just replace //by  VideoCapture cap("")
    VideoCapture cap(0);

    // main loop to grab sequence of input files

         bool ok = cap.grab();

      if (ok == false){

              std::cout << "Video Capture Fail" << std::endl ; 


                    // obtain input image from source
                   cap.retrieve(img, CV_CAP_OPENNI_BGR_IMAGE);
                   // Just resize input image if you want

                  // create foreground mask of proper size
                  if( foregroundMask.empty() ){
                      foregroundMask.create(img.size(), img.type());
                  // compute foreground mask 8 bit image
                  // -1 is parameter that chose automatically your learning rate
                  bg_model->apply(img, foregroundMask, true ? -1 : 0);
                   // smooth the mask to reduce noise in image
                  GaussianBlur(foregroundMask, foregroundMask, Size(11,11), 3.5,3.5);
                  // threshold mask to saturate at black and white values
                  threshold(foregroundMask, foregroundMask, 10,255,THRESH_BINARY);
    Background Subtractor

                    // create black foreground image
                   foregroundImg = Scalar::all(0);
                   // Copy source image to foreground image only in area with white mask
                   img.copyTo(foregroundImg, foregroundMask);
                   //Get background image

                    // Show the results
                   imshow("foreground mask", foregroundMask);
                  imshow("foreground image", foregroundImg);

                   int key6 = waitKey(40);
                    if(!backgroundImage .empty()){

                  imshow("mean background image", backgroundImage );
                    int key5 = waitKey(40);




        return EXIT_SUCCESS;


    Follow by Email

    Powered by Blogger.