Open jorditost opened 10 years ago
I think I know what the issue is here. Can you post your full sketch so I can run it locally?
Hi Greg,
Sorry for the slow reply, I got your notification emails into the spam folder. Here's the complete sketch. The line that makes the sketch crash is the 40, where I set the color mode back to RGB. I tested it with the last lib version (0.45).
import gab.opencv.*;
import processing.video.*;
import java.awt.Rectangle;
Capture video;
OpenCV opencv;
PImage src, processedImage, cannyImage, contoursImage;
ArrayList<Contour> contours;
void setup() {
video = new Capture(this, 640, 480);
opencv = new OpenCV(this, video.width, video.height);
size(video.width, video.height, P2D);
contours = new ArrayList<Contour>();
video.start();
}
void draw() {
// Load current frame
opencv.useColor();
opencv.loadImage(video);
src = opencv.getSnapshot();
opencv.useColor(HSB); // Change to HSB color space
opencv.setGray(opencv.getS().clone());
opencv.threshold(95);
opencv.erode();
processedImage = opencv.getSnapshot();
// Contours
contours = opencv.findContours(true, true);
contoursImage = opencv.getSnapshot();
// Canny Edges
opencv.loadImage(src);
opencv.useColor(RGB); // Change back to RGB color space
opencv.findCannyEdges(20,75);
opencv.dilate();
opencv.erode();
cannyImage = opencv.getSnapshot();
displayImages();
}
void displayImages() {
pushMatrix();
scale(0.5);
image(src, 0, 0);
image(processedImage, src.width, 0);
image(cannyImage, 0, src.height);
image(src, src.width, src.height);
popMatrix();
text("Source", 10, 25);
text("Pre-processed Image", src.width/2 + 10, 25);
text("Canny Edges", 10, src.height/2 + 25);
text("Contours", src.width/2 + 10, src.height/2 + 25);
displayContours();
}
void displayContours() {
pushMatrix();
scale(0.5);
translate(src.width, src.height);
noFill();
strokeWeight(3);
for (Contour contour : contours) {
Rectangle r = contour.getBoundingBox();
if ((contour.area() > 0.9 * src.width * src.height) ||
(r.width < 30 || r.height < 30))
continue;
stroke(255, 0, 0);
fill(255, 0, 0, 150);
strokeWeight(2);
rect(r.x, r.y, r.width, r.height);
}
popMatrix();
}
void captureEvent(Capture c) {
c.read();
}
In my draw() function I'm doing some preprocessing before I get the contours. For it I tell OpenCV to work in HSV color space. After it, I want to load the source image again and use the RGB color space, because it seems to work better to find contours. Here's the code:
It works fine when working with a source image, but throws an exception when using the video from the webcam (Capture class):
With a source image the sketch output looks like:
Here the rest of the error: