#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/
- Image Filters ( examples/ Filter imagesThis is the image that is being used in the example :
Code:
https://gist.github.com/TheVisualG/b8d718ea66e32f5bd67c7e3e35075339Output:
I learned:
Importing library – import gab.opencv.*;
OpenCV opencv;opencv = new OpenCV(this, img)
To print an image – = opencv.getSnapshot(); - Background Substraction
Video used:https://youtu.be/rSN_BNjPuKACode:https://gist.github.com/TheVisualG/39cfbdbdd1bbd056191c0bd8be3a0f9dOutput:
The code outputs a video where all the moving objects have a red stroke around them.
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)
- Brightest pointImage used:
Code:https://gist.github.com/TheVisualG/5c84313c5b41515ebc5b69a26c5675c1What I learned:PVector loc = opencv.max();
max() – get the x-y location of the maximum value in the current image.
min() – get the x-y location of the minimum value in the current image.
o Result.
- Image FilterCode:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); } Problem:
I had to change the file extension from .jpg to .JPG for some reason.Output:
- Background SubstractionInput File:
Code:https://gist.github.com/TheVisualG/f56c6348a25dc3b1eb584d10269d040c
Learning:
I received this error message:
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:
After that I recorded another one with the camera on a tripod and had way clearer results:
- Brightest PointInput Image:
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } Output:
o Sources.
- https://github.com/atduskgreg/opencv-processing
- Processing examples.