February 2016

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
  • Let me comment if you want some specific future Opencv tutorial.. 


    Future Opencv tutorials in progress

    I have prepared some ML tutorials.

    There is not to much complex tutorials like this on image samples.

    • Rtrees random trees methods in opencv 3.1
    • Boosting
    • SVM 
    • Testing of all cascades detect multiscales
    Remarks: If cascade.detectMultiscale faild in opencv. Return 2 much values in vector<Rect> try use release of your sample instead of debug.. Works for me and I dont know why.. 


    Also i would like to introduced

    • Some basic KLT tracking
    • KLT with feedback correction by NCC
    • Basic MeanShift and CamShift tracking method
    • Maybe some parts of Tracking learning detection method.. I will see. Maybe in many steps.
    Video editing
    •  Some drawing into video 
    • Games with face detection 


    Follow me and let me know if you want something soon 
    Thanks for comments and likes


    Farneback Optical flow

    Opencv simple C++ tutorial and code to achieve optical flow and farneback optical flow of moving object in opencv video. Lets checkt the  video example and the achieved result on my blog. Working and well describe code is included. 

    optical flow Farneback

    Optical Flow Farneback parameters remarks

    You need 2 images at least to calculate optical flow, the previous image (prevgray) and current image (img). 
    !! The previous image must be initialized first !! 
    Both images have to be gray scale. 

    Result is computer in flowUmat which has same size as inputs but format is CV_32FC2

    0.4- image pyramid or simple image scale
    1 is number of pyramid layers. 1 mean that flow is calculated only from previous image. 
    12 is win size.. Flow is computed over the window larger value is more robust to the noise. 
    2 mean number of iteration of algorithm
    8 is polynomial degree expansion recommended value are 5 - 7
    1.2 standard deviation used to smooth used derivatives recommended values from 1.1 - 1,5

    calcOpticalFlowFarneback(prevgray, img, flowUmat, 0.4, 1, 12, 2, 8, 1.2, 0);


    calcOpticalFlowFarneback Example

    Mine gopro video is captured at Old Town Square in Prague.. 






    Opencv Tutorial C++ code


    #include "opencv2\highgui.hpp"
    #include "opencv2\imgproc.hpp"
    #include "opencv2\objdetect\objdetect.hpp"
    #include "opencv2/video/tracking.hpp"
    #include <vector>
    #include <stdio.h>
    #include <Windows.h>
    #include <iostream>


    using namespace cv;
    using namespace std;



    int main(int argc, const char** argv)
    {

    // add your file name
    VideoCapture cap("yourFile.mp4");

    Mat flow, frame;
             // some faster than mat image container
    UMat  flowUmat, prevgray;

    for (;;)
    {

    bool Is = cap.grab();
    if (Is == false) {
                             // if video capture failed
    cout << "Video Capture Fail" << endl;
    break;
    }
    else {

    Mat img;
    Mat original;

    // capture frame from video file
    cap.retrieve(img, CV_CAP_OPENNI_BGR_IMAGE);
    resize(img, img, Size(640, 480));

    // save original for later
    img.copyTo(original);

    // just make current frame gray
    cvtColor(img, img, COLOR_BGR2GRAY);

    // For all optical flow you need a sequence of images.. Or at least 2 of them. Previous                           //and current frame
    //if there is no current frame
    // go to this part and fill previous frame
    //else {
    // img.copyTo(prevgray);
    //   }
    // if previous frame is not empty.. There is a picture of previous frame. Do some                                  //optical flow alg. 

    if (prevgray.empty() == false ) {

    // calculate optical flow
    calcOpticalFlowFarneback(prevgray, img, flowUmat, 0.4, 1, 12, 2, 8, 1.2, 0);
    // copy Umat container to standard Mat
    flowUmat.copyTo(flow);

                               
    // By y += 5, x += 5 you can specify the grid 
    for (int y = 0; y < original.rows; y += 5) {
    for (int x = 0; x < original.cols; x += 5)
    {
    // get the flow from y, x position * 10 for better visibility
            const Point2f flowatxy = flow.at<Point2f>(y, x) * 10;
    // draw line at flow direction
    line(original, Point(x, y), Point(cvRound(x + flowatxy.x), cvRound(y + flowatxy.y)), Scalar(255,0,0));
                                                             // draw initial point
    circle(original, Point(x, y), 1, Scalar(0, 0, 0), -1);

    }

    }
                                     // draw the results
    namedWindow("prew", WINDOW_AUTOSIZE);
    imshow("prew", original);
                                     // fill previous image again
    img.copyTo(prevgray);

    }
    else {

                                     // fill previous image in case prevgray.empty() == true
    img.copyTo(prevgray);
    }

    int key1 = waitKey(20);

    }
    }
    }

    Research AI and Computer vision

    oxford computer vision
    There is some demo. Actually it works. I am younger and good thing is that gender is right.

    Project oxford

    This is great project from Microsoft research.. The computer vision SDK and API of a state-of-the-art image algorithms. Gender classification optical letter Recognition and many more.


    Check all the demos of this great project

    Face DetectionFace VerificationEmotion RecognitionFace TrackingMotion DetectionStabilizationSpeech to TextText to SpeechSpeaker IdentificationSpeaker VerificationSpell CheckWord Breaking

    More information about microsoft computer vision research you can find here research microsoft
    There are project for images understanding like  microsoft Coco i mentioned in some  pervious  articles  (More about Coco). Another projects like Human behavior and video understanding, also 3D modelling and machine learning optimization and many more articles, source code and ideas. The lots of informations are related to Kinect and RGBD. 


    Opencv VideoCapture playback, fast frame replay

    Opencv C++ tutorial how to playback video frames and video loaded into memory, Fast video replay by slider. Simple described and working tutorial working in Visual studio 2015 with simple installation by Nugets Here or also with classical one.. 

    opencv videocapture playback

    This is not something special, but useful. Few days earlier, I post in memory video access by vector<Mat>. Great, fast approach but the short video fills RAM memory and crash the program even if you have 8 GB available. Maybe It is to danger and i decided to cancel this article. 

    In this example, i want to access GOPRO HD faster and skipped frames video and in memory is pure fantasy over minute video length. 

    Opencv VideoCapture property CV_CAP_PROP_POS_FRAMES


    I don't add extra comments. The code is pretty simple. I only highlights the important parts by RED font color. 

    This is only important part. By the slider_position value set the CV_CAP_PROP_POS_FRAMES value.
    cap.set(CV_CAP_PROP_POS_FRAMES, slider_position);

    The retrieved frame is the one on the slider position.
    cap.retrieve

    That's it.



    opencv videocapture playback fast frame access




    #include "opencv2\highgui.hpp"
    #include "opencv2\imgproc.hpp"
    #include <vector>
    #include <stdio.h>
    #include <Windows.h>
    #include <iostream>


    using namespace cv;
    using namespace std;


    int frame_max = 100;
    int slider_position;
    int slider_val;


    void on_trackbar(int, void*)
    {
    // I lost something? no
    }

    int main(int argc, const char** argv)
    {


    VideoCapture cap("GO.MP4");
    int counter = 10;
    frame_max = cap.get(CAP_PROP_FRAME_COUNT);


    for (;;)
    {

    bool Is = cap.grab();
    if (Is == false) {

    cout << "Video Capture Fail" << endl;
    break;
    }
    else {

    cap.set(CV_CAP_PROP_POS_FRAMES, slider_position);

    Mat img;
    Mat original;
    cap.retrieve(img, CV_CAP_OPENNI_BGR_IMAGE);

    img.copyTo(original);
    resize(img, img, Size(640, 480));

    namedWindow("prew", WINDOW_AUTOSIZE);
    imshow("prew", img);
    createTrackbar("rew", "prew", &slider_position, frame_max, on_trackbar);
    on_trackbar(slider_position, 0);

    int key1 = waitKey(20);

    }
    }
    }

    Computer vision mobile apps


    The biggest piece of computer vision apps are related to fun. I am not talking about image processing but about computer vision. Look at these apps.

    Fun Morph apps 

    Fun applications are mainly focus on augmented reality and Face Substitution or replacement. This 10 minutes of fun applications are great instead of youtube. You can create your child looks with your gf with 2 photos and one click.




    • This is great valuable tool in movie and TV insdustry. Smart film effects in future mobile phones will be scary tool. Imagine 2000 video of Leonardo Dicaprio at the same time on difference place on the earth. 
    • This apps are for fun and definitelly for short period


    Sport computer vision apps

    The profesional footbal player uses smart tracking belts. Or some kind of computer vision tracking apps. All this apps in Hockey, Ragby and American fotbal focus on strategy. This is professional usage of computer vision and system like this for his price cannot find in Appstore.





    There is some potential in individual improvements of your sport skills. There will be some smart apps which help you to do your exercise in right way. For example at the gym. Imagine that you are exercising on the machine an phone tracking your body parts. Phone could tell you information about your heard and look at you if you do movements correctly. Great.
    This tennis app is rly good one. Look at this.



    Tennis Camera




    Fashion and makeup computer vision apps

    Great and fun concept. In some stores have ipads with application for example Loreal where you can try different colors and style on face. I think that this kind of apps have a secret potential in near future for whole body fashion.







    Automatic babysitter


    Maybe and maybe not. There is better some kind of sensors in build in  childrens pad. Monitoring heart frequency and breath. This informations are much more valuable than anythink you can obtain from the video. Still, All of us want to watch our babies visually. Improvements could be in smart notification together with some sensor fusin like camera, smart pad, sound notification. 


    Security computer vision Apps

    Is there space only for video prewiev? Maybe not in near future. Some devices has H264 processors but some of tham use whole its power to handle modern codecs.
     When you need instant monitoring of something. For exapmle car or house. You need to use something else than your mobile phone. Something from internet of things concepts cloud service that handle smart computer vision task. 





    FFMPEG Debian Jessie and Opencv instalation

    Share this for more tutorials and computer vision post from me.. Thanks best Vladimir
    Debian Opencv 
    +
    Debian Opencv


    // Just update and upgrade your system
    sudo apt-get update
    sudo apt-get -y upgrade
    
    
    
    //Install basic staff in any case you dont have this packages
    
    
    sudo apt-get -y install build-essential
    sudo apt-get -y install cmake
    sudo apt-get -y install pkg-config

    Install opencv dependencies

    sudo apt-get -y install libgtk2.0-dev python-dev python-numpy 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 libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libdc1394-utils libjpeg-dev libpng-dev libtiff-devlibjasper-dev libtiff5 libtiff5-dev libopenexr-dev libjasper-dev
    

    Install FFMPEG

    sudo apt-get -y install git make nasm pkg-config libx264-dev libxext-dev libxfixes-dev zlib1g-dev
    sudo wget -O ffmpeg-2.8.tar.bz2 "https://www.ffmpeg.org/releases/ffmpeg-2.8.tar.bz2"
    sudo tar -xvf ffmpeg-2.8.tar.bz2
    sudo rm -Rf ffmpeg-2.8.tar.bz2
    
    cd /home/nomce/libs/ffmpeg-2.8 
    
    ./configure --enable-nonfree --enable-gpl --enable-libx264 --enable-x11grab --enable-zlib
    make -j2
    sudo make install
    sudo ldconfig -v
    wget -O 3.0.0.zip https://github.com/Itseez/opencv/archive/3.0.0.zip
    # Unzip
    unzip 3.0.0.zip
    # Cleanup
    rm -Rf 3.0.0.zip
    

    Install opencv

    cd /home/nomce/libs/opencv-3.0.0
    
    mkdir bin
    cd bin
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_TBB=ON -D BUILD_EXAMPLES=ON -D CMAKE_INSTALL_PREFIX=/usr/local /home/nomce/libs/opencv-3.0.0/
    make -j2
    sudo make install
    sudo ldconfig -v

    Load all pictures inside the Folder into Opencv Mat

    Share this for more tutorials and computer vision post from me.. Thanks best Vladimir

    This is example how to load all images inside the folder on the Windows system.
    I use this to collect vector<Mat> to train classificator for image recognition.

    Collect positive and negative pictures

    Basically, I load all positive samples from one Folder and negative samples from another Folder into one vector<Mat>. Code show only reading from one folder.

    Label positive and negative data in Opencv

    Also It is easy to count files (images) in one folder and another folder and label the images in your vector in sense that the first part contains only positive samples and when Iterator is greater than Num of positive samples rest we can labeled as negative. 

    Mat of labels 1 for positive and - 1 for negative you can create like that

    This is vector  with POSITIVE | NEGATIVE image samples inside
    The number of positive images and negative images is count in code at the end of this article. 
    vector<Mat> samples;


    // Create empty Container with INT values 1 for positive and -1 for negative
    Mat_<int> labels(1, pos_count + neg_count);
    //Fill the first part by 1 for positive samples intil I < pos_count

    for (int i = 0; i < pos_count; ++i)
    labels(0, i) = 1;
    //Fill the rest as negative labels with -1

    for (int i = pos_count; i < pos_count + neg_count; ++i)

    labels(0, i) = -1;



    -----------------------------------------------------------------

    #include "opencv2\highgui.hpp"
    #include "opencv2\imgproc.hpp"
    #include <vector>
    #include <fstream>
    #include <stdio.h>
    #include <iostream>
    #include <Windows.h>
    #include <iomanip>


    vector<string> getNamesOfFile(string folderPath)
    {
        // vector of names of file within a folder 
        vector<string> names_ofFiles;

        char search[200];
        // convert your string search path into char array
        sprintf(search, "%s*.*", folderPath.c_str());

       //  WIN32_FIND_DATA is a structure
       // Contains information about the file that is found by the FindFirstFile
        WIN32_FIND_DATA Info; 
        // Find first file inside search path
        HANDLE Find = ::FindFirstFile(search, &fdInfo); 

        // If  FindFirstFile(search, &fd); fails return INVALID_HANDLE_VALUE,
        // If Find is not equal to INVALID_HANDLE_VALUE do the loop inside the
        // IF condition

        if(Find != INVALID_HANDLE_VALUE) { 
            do { 
                
                   // DWORD dwFileAttributes , FILE_ATTRIBUTE_DIRECTORY ident a directory
                   // If condition is fine lets push file mane into string vector
                if(! (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
                   // Fill vector of names
                   names_ofFiles.push_back(Info.cFileName);

                }

            // do - while there is next file
            //::FindNextFile the function succeeds, the return value is nonzero and the Info contain //information about next file

            }while(::FindNextFile(Find, &Info)); 
            ::FindClose(Find); 
        } 

         // Return result
        return names_ofFiles;
    }





    int main(int argc, const char** argv)

    {

    // your path where you can load all images String Path = "C://NegativeSample/";
    // Vector that contain all images file mames
    vector<string> vectorOfFileNameWithinPath =  getNamesOfFile(Path2);



    // Load all images and display
    // and store inside the another vector StoredImages

    int pos_count = 0;
     vector<Mat> StoredImages
    for (int i = 0; i <  vectorOfFileNameWithinPath.size() -1 ; ++i)

    {
                      // Path "C://NegativeSample/" + i th "Filename.jpg"

                     String cesta = Path + vectorOfFileNameWithinPath[i];
     // Read image into Mat container
                     Mat img = imread(cesta, IMREAD_COLOR);
                     // Store images in vector you can use later for example in own ML training
                      StoredImages.push_back(img)
                     // Display single image
                               namedWindow("Display",WINDOW_AUTOSIZE);
      imshow( "Display", img);
      int key1 = waitKey(10);
                               // Increase number of positive samples in StoredImages
                               // Later we push into same vector negative samples
                               // and we need to know, where the boundary is
                              pos_count = pos_count + 1;

                    }

    }

    ad

    Follow by Email

    Powered by Blogger.