The example recreates the VideoCapture sample from the OpenCV documentation. It uses the Imgproc module to convert the color image into single channel greyscale, apply a blur filter, and find the edges.
import processing.video.*; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgproc.Imgproc; import org.opencv.core.CvType; import org.opencv.core.Size; import java.nio.*; import java.util.List; Capture cap; PImage img; int cnt; void setup() { size(640, 480); background(0); println(Core.VERSION); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); cap = new Capture(this, width, height); cap.start(); cap.read(); cnt = cap.width*cap.height*4; img = createImage(cap.width, cap.height, ARGB); } void draw() { Mat tmp = new Mat(cap.height, cap.width, CvType.CV_8UC4); Mat mat = new Mat(cap.height, cap.width, CvType.CV_8UC3); Mat grey = new Mat(cap.height, cap.width, CvType.CV_8UC1); byte [] bArray = new byte[cnt]; ByteBuffer bb = ByteBuffer.allocate(cnt); bb.asIntBuffer().put(cap.pixels); bb.get(bArray); tmp.put(0, 0, bArray); ArrayList ch1 = new ArrayList(); ArrayList ch2 = new ArrayList(); ArrayList ch3 = new ArrayList(); ArrayList ch4 = new ArrayList(); Core.split(tmp, ch1); ch2.add(ch1.get(3)); ch2.add(ch1.get(2)); ch2.add(ch1.get(1)); Core.merge(ch2, mat); Imgproc.cvtColor(mat, grey, Imgproc.COLOR_BGR2GRAY); Imgproc.GaussianBlur(grey, grey, new Size(7, 7), 1.5, 1.5); Imgproc.Canny(grey, grey, 0, 30); Core.split(grey, ch3); ch4.add(ch1.get(0)); // alpha ch4.add(ch3.get(0)); ch4.add(ch3.get(0)); ch4.add(ch3.get(0)); Core.merge(ch4, tmp); tmp.get(0, 0, bArray); ByteBuffer.wrap(bArray).asIntBuffer().get(img.pixels); img.updatePixels(); image(img, 0, 0); text("Frame rate: " + nf(round(frameRate), 2), 10, 20); grey.release(); tmp.release(); mat.release(); ch1.clear(); ch2.clear(); ch3.clear(); ch4.clear(); } void captureEvent(Capture c) { c.read(); } |
The Imgproc module handles basic and advanced image processing in OpenCV. The cvtColor converts the color matrix, mat to greyscale, grey with the parameter COLOR_BGR2GRAY. The GaussianBlur performs the Gaussian Blur filter to the greyscale image. The Canny searches for edges in the blurred greyscale image using the Canny Edge Detector.
Imgproc.cvtColor(mat, grey, Imgproc.COLOR_BGR2GRAY); Imgproc.GaussianBlur(grey, grey, new Size(7, 7), 1.5, 1.5); Imgproc.Canny(grey, grey, 0, 30); |