1.2 Dev. Step #1 / How OpenCV works? What is the syntax of the library for Processing?

#1 How OpenCV works? What is the syntax of the library for Processing?

o Approach.

Installing the library and experimenting with the different examples that come with it. Main goal is tracking the X and Y position of an object.

o Learning Experience.

I decided to use the most up to date OpenCV library from the official website ( https://opencv.org/ ). That was a a big mistake, since it didn’t work out and I lost quite some time trying to make it work.

I ended up installing Processing 3 and installing the library directly through the application itself – OpenCV for Processing ( 0.5.4 ) https://github.com/atduskgreg/opencv-processing

The library is based on Open CV 2.4.5, which means that I should be looking at the java documentation for that version. After a short search I found the page: https://docs.opencv.org/java/2.4.5/ . Soon I realized that the creator of the processing library has changed the syntax and I need to look for a different library documentation.

After a short while I found the final OpenCV for Processing documentation!
http://atduskgreg.github.io/opencv-processing/reference/

Capture.JPG

I learned:
startBackgroundSubtraction(int history,  int nMixtures, double backgroundRatio)

Setup background subtraction. After calling this function, updateBackground() must be called with each new frame you want to add to the running background subtraction calculation. For details on the arguments, see: http://docs.opencv.org/java/org/opencv/video/BackgroundSubtractorMOG.html#BackgroundSubtractorMOG(int, int, double)

 

o Result.

  • Image FilterCode:


    import gab.opencv.*;
    OpenCV opencv;
    PImage img, thresh, blur, adaptive;
    void setup() {
    img = loadImage("profile_test.jpg");
    size(1080, 1080);
    opencv = new OpenCV(this, img);
    PImage gray = opencv.getSnapshot();
    opencv.threshold(80);
    thresh = opencv.getSnapshot();
    opencv.loadImage(gray);
    opencv.blur(12);
    blur = opencv.getSnapshot();
    opencv.loadImage(gray);
    opencv.adaptiveThreshold(591, 1);
    adaptive = opencv.getSnapshot();
    }
    void draw() {
    pushMatrix();
    scale(0.5);
    image(img, 0, 0);
    image(thresh, img.width, 0);
    image(blur, 0, img.height);
    image(adaptive, img.width, img.height);
    popMatrix();
    fill(0);
    text("source", img.width/2 – 100, 20 );
    text("threshold", img.width – 100, 20 );
    text("blur", img.width/2 – 100, img.height/2 + 20 );
    text("adaptive threshold", img.width – 150, img.height/2 + 20 );
    }

    view raw

    filter_test

    hosted with ❤ by GitHub

    Problem:
    I had to change the file extension from .jpg to .JPG for some reason.

    Output:
    Capture.JPG

  • Background SubstractionInput File: Capture2.JPGCode:https://gist.github.com/TheVisualG/f56c6348a25dc3b1eb584d10269d040c

    Learning:
    I received this error message:
    Capture.JPG
    I converted the video to .mov and disabled the audio. The code was not working with .mp4 file extension.

    The first video was handheld and it resulted in:
    Capture.JPG

    After that I recorded another one with the camera on a tripod and had way clearer results:

    Capture.JPG

  • Brightest PointInput Image:
    test2.JPG

    Code:


    import gab.opencv.*;
    OpenCV opencv;
    void setup() {
    PImage src = loadImage("test2.JPG");
    src.resize(800, 0);
    size(800, 533);
    opencv = new OpenCV(this, src);
    }
    void draw() {
    image(opencv.getOutput(), 0, 0);
    PVector loc = opencv.max();
    stroke(255, 0, 0);
    strokeWeight(4);
    noFill();
    ellipse(loc.x, loc.y, 30, 30);
    }

    view raw

    bright_test

    hosted with ❤ by GitHub

    Output:
    Capture.JPG

o Sources.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s