I am doing canny edge detection with opencv in processing (source, webcam source) add coloring edge pixels a certain color. However, what I would like to do is draw curved lines through edges that have neighboring pixels, and if there is a gap of a large enough size, stop the line and move on to the next. I want the lines to be different colors, and be able to specify a certain density (i.e. no more than X number of lines for the overall image). What is the most efficient way to do this?
I stumbled on something for Hough Curve detection - is this available in opencv? Is that an appropriate way to try and solve this problem, or is there something easier that I should be focusing on?
Here is my current code:
import gab.opencv.*;
import processing.video.Capture;
OpenCV opencv;
Capture cam;
int zoom = 1;
int size = 3;
boolean invert = true;
// input resolution
int w = 160 * size, h = 120 * size;
//int w = 1080, h = 720;
void setup() {
size(480, 360);
// init cam
cam = new Capture(this, w, h);
cam.start();
// init opencv
opencv = new OpenCV(this, w, h);
}
color c1 = #ffff00;
color c2 = #ff00ff;
color c3 = #00ffcc;
color[] colorsArr = {c1,c2,c3 };
void draw() {
opencv.loadImage(cam);
scale(zoom);
//webcam image
PImage src = snapshot("original", 0, 0);
//edges
opencv.findCannyEdges(120, 75);
if(invert) opencv.invert();
snapshot("canny", 0, 0);
}
// read a new frame when it's available
void captureEvent(Capture c) {
c.read();
}
// create a snapshot and display it
PImage snapshot(String label, int px, int py) {
// show image
PImage img = opencv.getSnapshot();
if (label == "canny"){
img.loadPixels();
int dimension = img.width * img.height;
for (int i = 0; i < dimension; i += 1) {
if (img.pixels[i] == -1){ //if the contrasted image color is black(?) make it black transparent
img.pixels[i] = color(0, 0, 0, 0);
} else {
int randColor = int(random(0,2));
//img.pixels[i] = colorsArr[randColor]; //random color //MH - how to get connected lines?
img.pixels[i] = c2; //solid color
}
}
image(img, 0,0);
} else {
image(img, 0,0);
}
// image outline
noFill();
//stroke(invert ? 0 : 255);
//rect(px * w, py * h, w, h);
// return the snapshot so we can reuse it
return img;
}
void keyPressed() {
invert = !invert;
}