We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone, I'm using this code:
https://github.com/atduskgreg/opencv-processing/blob/master/examples/FindContours/FindContours.pde
I wish by pressing the button B the contour which found, will reset and becomes ready for a new image.
import g4p_controls.*;
import java.awt.Font;
import gab.opencv.*;
PImage src, dst;
OpenCV opencv;
ArrayList<Contour> contours;
ArrayList<Contour> polygons;
GButton btnFilterA;
boolean filterA=false;
GButton btnFilterB;
boolean filterB=false;
void setup() {
src = loadImage("1.jpg");
size(1024, 768);
opencv = new OpenCV(this, src);
btnFilterA = new GButton(this, 600, 20, 140, 20);
btnFilterA.setText("Go");
btnFilterA.setLocalColorScheme(GCScheme.GREEN_SCHEME);
btnFilterB = new GButton(this, 750, 20, 140, 20);
btnFilterB.setText("Stop");
btnFilterB.setLocalColorScheme(GCScheme.GREEN_SCHEME);
//opencv.gray();
//opencv.threshold(70);
//dst = opencv.getOutput();
contours = opencv.findContours();
println("found " + contours.size() + " contours");
}
void draw() {
//scale(0.5);
image(src, 0, 0);
//image(dst, src.width, 0);
noFill();
strokeWeight(3);
for (Contour contour : contours) {
beginShape();
for (PVector point : contour.getPolygonApproximation().getPoints()) {
if (filterA==true){
stroke(0, 255, 0);
contour.draw();
stroke(255, 0, 0);
vertex(point.x, point.y);
}
endShape();
}
}
}
public void handleButtonEvents(GButton button, GEvent event) {
if (button == btnFilterA) {
filterA=true;
}
if (button == btnFilterB) {
contour.clear();
}
}
I tried with my code but even if off the contour remains in "memory" of the contour, how I can fix?
thanks
Answers
Notice your endShape is not in the same scope as your beginShape. Move line 57 one curly bracket out.
To test if the array is being cleared out, try this:
In your code, line 68, you should be calling contours and not contour. This is what I disagree naming variables and containers following classes names. Very easy to create a bug and hard to track them. I will suggest spicing the names of your objects. It will make them more maintainable at the end as well.
Kf
Thanks kfrajer for your help, I have corrected the error (contours) but I have not understand where to enter endShape();
new code is:
every time I open a new file, it do not see the new contour, how can I fix?
thanks
Right. If you post the code in the Processing IDE (aka. not here), select all your code and hit ctrl+T. This will autoformat your code. Then if you check the scope of beginShape and endShape, they are not under the same block aka. under the same curling bracket block.
Kf
Ok, I also would recommend changing the way you are defining your size as you first image defines the size of your canvas. Instead, start with a pre-defined size and then when you load the image, you resize it. As it is right now for example, when I open a second image, if it is not the same size as the first image, it will throw an error.
Also I do not think it is a good approach to call your selectImage() recursively.
I will do instead (pseudo-code and not tested)
Kf
and it draw():
Now I try, thank you!
I have tried all your advice and this is the code:
but when I open the new image does not work detention of the contour, it works only the first time, I have this error:
ConcurrentModificationException for (Contour contour : contours) {
maybe I wrong something?
thanks
This works.
Kf
Improved by using stages.
Kf
Thanks kfrajer for your great help, everything works well, thanks again!