5/27/2018

Video frame difference (frame subtraction), opencv example source code

video frame subtraction example source code using opencv



...
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/cudaarithm.hpp"
#include <iostream>
#include <vector>

#ifdef _DEBUG               
#pragma comment(lib, "opencv_core331d.lib")       
#pragma comment(lib, "opencv_highgui331d.lib")    
#pragma comment(lib, "opencv_imgcodecs331d.lib")  
#pragma comment(lib, "opencv_objdetect331d.lib")  
#pragma comment(lib, "opencv_imgproc331d.lib")  
#pragma comment(lib, "opencv_videoio331d.lib")  
#pragma comment(lib, "opencv_cudaarithm331d.lib")  
#else       
#pragma comment(lib, "opencv_core331.lib")       
#pragma comment(lib, "opencv_highgui331.lib")    
#pragma comment(lib, "opencv_imgcodecs331.lib")    
#pragma comment(lib, "opencv_objdetect331.lib")  
#pragma comment(lib, "opencv_imgproc331.lib")  
#pragma comment(lib, "opencv_videoio331.lib")
#pragma comment(lib, "opencv_cudaarithm331.lib")
#endif        


using namespace std;
using namespace cv;

int main(int, char)
{

    Mat frame;
    Mat old_frame;
    Mat sub_frame;
    //Mat absdiff_frame;
    VideoCapture stream1(0);

    if (!stream1.isOpened()) { //check if video device has been initialised
        cout << "cannot open camera 1";
        return 0;
    }

    while (1)
    {
        if (!(stream1.read(frame))) //get one frame form video   
            break;

        if (old_frame.empty())
        {
            old_frame = frame.clone();
            continue;
        }
        

        subtract(old_frame, frame, sub_frame);
        //absdiff(old_frame, frame, absdiff_frame);

        imshow("frame", frame);
        imshow("sub_frame", sub_frame);
        //imshow("absdiff_frame", absdiff_frame);

        //subtraction every frame
        old_frame = frame.clone(); 
        if (waitKey(5) >= 0)
            break;
    }

    return 0;
}
...

5/26/2018

Find contour example source code


Find Contour example source code


...
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <vector>

#ifdef _DEBUG               
#pragma comment(lib, "opencv_core331d.lib")       
#pragma comment(lib, "opencv_highgui331d.lib")    
#pragma comment(lib, "opencv_imgcodecs331d.lib")  
#pragma comment(lib, "opencv_objdetect331d.lib")  
#pragma comment(lib, "opencv_imgproc331d.lib")  
#pragma comment(lib, "opencv_videoio331d.lib")    
#else       
#pragma comment(lib, "opencv_core331.lib")       
#pragma comment(lib, "opencv_highgui331.lib")    
#pragma comment(lib, "opencv_imgcodecs331.lib")    
#pragma comment(lib, "opencv_objdetect331.lib")  
#pragma comment(lib, "opencv_imgproc331.lib")  
#pragma comment(lib, "opencv_videoio331.lib")    
#endif        


using namespace std;
using namespace cv;

int main()
{
    Mat img(500, 500, CV_8UC3);
    img.setTo(255);

    cvtColor(img, img, CV_RGB2GRAY);
    img.setTo(0);
    rectangle(img, Rect(10, 10, 200, 200), CV_RGB(255, 255, 255), CV_FILLED);
    imshow("show1", img);

    Mat dst = Mat::zeros(img.rows, img.cols, CV_8UC3);
    vector< vector< Point> > contours;
    vector< Vec4i> hierarchy;

    findContours(img.clone(), contours, hierarchy,
        RETR_CCOMP, CHAIN_APPROX_SIMPLE);

    // iterate through all the top-level contours,
    // draw each connected component with its own random color
    int idx = 0;
    for (; idx >= 0; idx = hierarchy[idx][0])
    {
        Scalar color(rand() & 255, rand() & 255, rand() & 255);
        //drawContours(dst, contours, idx, color, FILLED, 8, hierarchy);
        drawContours(dst, contours, idx, color, 1, 8, hierarchy);
    }

    imshow("show2", dst);

    waitKey(0);

    return 0;
}
...

5/25/2018

example for applyColorMap usage

example for applyColorMap usage

...
#include "opencv2/opencv.hpp"
#include "opencv2/cuda.hpp"
#include "opencv2\cudaarithm.hpp"
#include <iostream>
#include <time.h>

using namespace std;
using namespace cv;

int main()
{
    
    //image show.
    Mat img = imread("AbyssCGI.jpg");
    
    namedWindow("img", 0);
    imshow("img", img);

    /////////////////applyColorMap
    //basic usage
    Mat im_color;
    applyColorMap(img, im_color, COLORMAP_COOL);//COLORMAP_AUTUMN);
    namedWindow("im_color", 0);
    imshow("im_color", im_color);

    //what about gray?
    Mat img_gray;
    cvtColor(img, img_gray, CV_RGB2GRAY);
    Mat im_color2;
    applyColorMap(img_gray, im_color2, COLORMAP_AUTUMN);
    namedWindow("im_color2", 0);
    imshow("im_color2", im_color2);
    /////////////////////////////////////////

    //////////////////// gray & color test!!
    Mat img2(100, 100, CV_8UC3);
    img2.setTo(Scalar(0, 128, 255));
    Mat im_color3;
    applyColorMap(img2, im_color3, COLORMAP_JET);

    namedWindow("im_color3", 0);
    imshow("im_color3", im_color3);

    //same with resutl of color
    Mat img2_gray;
    cvtColor(img2, img2_gray, CV_RGB2GRAY);
    Mat im_color4;
    applyColorMap(img2, im_color4, COLORMAP_JET);
    namedWindow("im_color4", 0);
    imshow("im_color4", im_color4);
    //////////////////////

    waitKey(0);

    return 0;
}
...


Loop & LUT processing time compare

 Loop & LUT processing time compare


...
#include "opencv2/opencv.hpp"
#include "opencv2/cuda.hpp"
#include "opencv2\cudaarithm.hpp"
#include <iostream>
#include <time.h>

using namespace std;
using namespace cv;

int main(int, char)
{
    unsigned long AAtime = 0, BBtime = 0;
    unsigned char O[1000][1000] = { 1, 2, };

    //very slow;
    AAtime = getTickCount();
    for (int i = 0; i < 1000; ++i)
    {
        for (int j = 0; j < 1000; ++j)
        {
            int t = O[i][j] * 1.14;
            t = (t > 255) ? 255 : t;
            O[i][j] = t;
        }
    }
    BBtime = getTickCount();
    printf("just loop %.5lf sec \n", (BBtime - AAtime) / getTickFrequency()); //chec
    
    //fast
    unsigned char LUT[256];
    AAtime = getTickCount();
    for (int i = 0; i < 256; ++i)
    {
        int t = i * 1.14;
        t = (t > 255) ? 255 : t;
        LUT[i] = t;
    }

    for (int i = 0; i < 1000; ++i)
        for (int j = 0; j < 1000; ++j)
            O[i][j] = LUT[O[i][j]];
    
    BBtime = getTickCount();
    printf("using lut %.5lf sec \n", (BBtime - AAtime) / getTickFrequency()); //chec

    return 0;
}
...


print each character in string and some useful functions

This is example source code for how to print and access each character in string.
And using this function, I made some useful functions.
Thank you.


...
void printAndCountAllCh(string str)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        std::cout << count << ": " << *it << endl;
        count++;
    }
}

string changeAtoB(string str, char A, char B)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) == string(1, A))
        {
            *it = B;
        }
    }

    return str;
}

string deleteA(string str, char A)
{
    string rstr;
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) != string(1, A))
        {
            rstr.push_back(*it);
        }
    }

    return rstr;
}


int countNotCH(string str, char CH)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) != string(1, CH))
            count++;
    }
    return count;
}

int main()
{

    string str = "A_B CD_EF GH_I";
    int count;

    printAndCountAllCh(str); //print each char and count
    std::cout << endl;

    string abc = changeAtoB(str, ' ', '_'); //A_B CD_EF GH_I -> A_B_CD_EF_GH_I
    std::cout << abc << endl << endl;

    string bbb = deleteA(str, ' '); //A_B CD_EF GH_I -> A_BCD_EFGH_I
    std::cout << bbb << endl << endl;

    count = countNotCH(str, '_'); //A_B CD_EF GH_I -> 11
    std::cout << count << endl << endl;

    return 0;
}
...




5/22/2018

single color value convert from RGB to YUV and YUV to RGB

Simple color convert code for a pixel value

example for :
RGB to YUV ,YUV to RGB


void rgbToyuv(int r, int g, int b, int& y, int& u, int& v)
{
    Mat img_in(1, 1, CV_8UC3);
    img_in.at<Vec3b>(0, 0)[0] = uint(b);
    img_in.at<Vec3b>(0, 0)[1] = uint(g);
    img_in.at<Vec3b>(0, 0)[2] = uint(r);


    Mat img_out;
    cvtColor(img_in, img_out, CV_BGR2YUV);

    y = uint(img_out.at< cv::Vec3b>(0, 0)[0]);
    u = uint(img_out.at< cv::Vec3b>(0, 0)[1]);
    v = uint(img_out.at< cv::Vec3b>(0, 0)[2]);
}

void yuvTorgb(int y, int u, int v, int& r, int& g, int& b)
{
    Mat img_in(1, 1, CV_8UC3);
    img_in.at<Vec3b>(0, 0)[0] = uint(y);
    img_in.at<Vec3b>(0, 0)[1] = uint(u);
    img_in.at<Vec3b>(0, 0)[2] = uint(v);


    Mat img_out;
    cvtColor(img_in, img_out, CV_YUV2BGR);

    b = uint(img_out.at< cv::Vec3b>(0, 0)[0]);
    g = uint(img_out.at< cv::Vec3b>(0, 0)[1]);
    r = uint(img_out.at< cv::Vec3b>(0, 0)[2]);
}


int main()
{

    int ored = 255;
    int ogreen = 0;
    int oblue = 0;
    int y2, u2, v2;
    printf("origin %d %d %d \n", oblue, ogreen, ored);

    rgbToyuv(ored, ogreen, oblue, y2, u2, v2);
    printf("origin -> yuv: %d %d %d \n", y2, u2, v2);

    int r3, g3, b3;
    yuvTorgb(y2, u2, v2, r3, g3, b3);
    printf("yuv -> rgb: %d %d %d \n", r3, g3, b3);
}




5/12/2018

Github command tips

*Init Github
$ git init
Initialized empty Git repository in ~/djangogirls/.git/
$ git config --global user.name "yourname"
$ git config --global user.emai yourname@mail.com

*Making ignore file -> .ignore
*.pyc
*~
__pycache__
myvenv
db.sqlite3
/static
.DS_Store

*Status check
$ git status
On branch master
Initial commit
Untracked files:  (use "git add <file>..." to include in what will be committed)
        .gitignore        blog/        manage.py        mysite/
nothing added to commit but untracked files present (use "git add" to track)

*add and commit
$ git add --all . #git add .
$ git commit -m "My Django Girls app, first commit"
 [...] 13 files changed, 200 insertions(+) create mode 100644 .gitignore [...] create mode 100644 mysite/wsgi.py

*Set remote repository
$ git remote add origin https://github.com/MareArts/CVLecture_djangocms.git
$ git push -u origin master

*Command arrangement
Working computer
$ git status
$ git add --all .
$ git status
$ git commit -m "Changed the HTML for the site."
$ git push

Deploy computer
$ git pull

*tip force pull
https://stackoverflow.com/questions/1125968/how-do-i-force-git-pull-to-overwrite-local-files
$git fetch --all
$git reset --hard origin/master
or
$git reset --hard origin/<branch_name>

*tip to set user/pass don't ask every time
save username / pass
$git config credential.helper store
$git pull

change information
$git config credential.helper store 
$git pull

*tip when .igore file not working
https://stackoverflow.com/questions/11451535/gitignore-is-not-working
git rm -r --cached .
git add .
git commit -m "fixed untracked files"

*delete git
rm -rf .git

*delete specific file ex) some problem file which can't do push because of over than 100mb.
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch path_more_than_100mb.file' \
--prune-empty --tag-name-filter cat -- --all

ex)
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch checkpoints_/model.ckpt-23000.data-00000-of-00001' \
--prune-empty --tag-name-filter cat -- --all