The example explores the Hough Circle detection in the Imgproc module. It starts with a greyscale copy of the live webcam image with an application of a blur filter, in this case, a medianBlur.
It also makes use of the CVImage class for the conversion between PImage and Mat in OpenCV.
import processing.video.*; import org.opencv.video.Video; import org.opencv.core.Mat; import org.opencv.imgproc.Imgproc; Capture cap; CVImage img; void setup() { size(640, 480, P3D); background(0); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); cap = new Capture(this, width, height); cap.start(); cap.read(); img = new CVImage(width, height); smooth(); } void draw() { img.copy(cap, 0, 0, cap.width, cap.height, 0, 0, img.width, img.height); img.toCV(); Mat grey = img.getGrey(); Mat dst = new Mat(grey.size(), grey.type()); Imgproc.medianBlur(grey, dst, 5); Mat circles = new Mat(); Imgproc.HoughCircles(dst, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 10, 100, 30, 10, 40); background(0); image(img, 0, 0); fill(255, 200, 0); if (circles.rows()>0) { for (int i=0; i<circles.cols(); i++) { double [] v = circles.get(0, i); float x = (float)v[0]; float y = (float)v[1]; float r = (float)v[2]; ellipse(x, y, r*2, r*2); } } fill(0); noStroke(); rect(0, 0, 110, 30); fill(255); text("Frame rate: " + nf(round(frameRate), 2), 10, 20, 0); grey.release(); dst.release(); circles.release(); } void captureEvent(Capture _c) { _c.read(); } |