We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am wondering how to write this piece of code in normal syntax. I understand the version supplied here is shorthand.
I want to be able to tease apart the pieces so I can see what it is doing (still learning) and I am not sure how to devolve the syntax so-to-speak. How could I write this out in full?
beginShape();
for (PVector point : contour.getPolygonApproximation().getPoints()) {
vertex(point.x, point.y);
}
endShape();
From the full version of the FindContours example in OpenCV for Processing by Greg Borenstein
import gab.opencv.*;
PImage src, dst;
OpenCV opencv;
ArrayList<Contour> contours;
void setup() {
src = loadImage("test.jpg");
size(src.width, src.height/2, P2D);
opencv = new OpenCV(this, src);
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) {
stroke(0, 255, 0);
contour.draw();
stroke(255, 0, 0);
beginShape();
for (PVector point : contour.getPolygonApproximation().getPoints()) {
vertex(point.x, point.y);
}
endShape();
}
}
Answers
it depends from the methods of contour, but in general, something like this.
(it would be like this if it were an ArrayList)
this
just means "loop over all elements in the list contours and bring each element into the var (object) contour one after another.
var contour being of type (class) Contour."
;-)
When you use an "enhanced for" like that, you're actually using an Iterator behind the scenes. So this code:
Is actually doing this:
Addendum:
for ( : )
loop.for ( : )
loops.for (PVector dot : vecs) vertex(dot.x, dot.y);
if vecs happens to be a PVector[], it's internally transpiled into:
Otherwise, if it's a class which implements Iterable, like an ArrayList for example, it's as what was said by other answerers:
Of course never use Iterator. Just let Java do that behind the scenes w/ those nice
for ( : )
loops! ;-)Thank you very much for the thorough explanation everyone. I see that there are a few different ways and a few different layers going on here.
I am going to try a few of these in the code and see about finding which one works for the sketch and re-post that.
I think I'll start with the post by Chrisir and then dig deeper.
Cheers!
Take notice that this long condition expression within the
for ( ; ; )
loop:i < contour.getPolygonApproximation().count();
is re-tested at each iteration! =;For better performance I advise you to pre-cache that before the loop:
But don't forget that everything above can be shortened to 1-line statement via "enhanced"
for ( : )
: *-:)Read more about the diffs. between the 2
for ()
loop styles here: :-Bhttp://processing.org/reference/for.html
I started with the line of code above but frankly I think I have just confused myself more and it doesn't work within the context of the sketch.
I am not sure what's doing what. There are too many things named similar to "contour". I'll be working through it this afternoon.
Here is my current sketch:
Dunno if it's any help but here's your program using the "enhanced"
for ()
loop.Since I dunno the library nor have the image, couldn't test it though: :-??
I really appreciate you taking a moment to help me through this.
When I run the code you just posted I get the error of "The function getPolygonApproximation() does not exist" on the last line of code.
Is it calling it outside or inside of the scope of the function?
Yeah, I am just learning the library as well. Thank you for the help once again. I was hoping these misunderstandings would be common enough for others not into OpenCV to help with.
The last line gave me an error but still ran.
"No uv texture coordinates supplied with vertex() call"
I am thinking I'll just try out some other examples and see if I can work it out. I've started to tease apart the naming of the code. Some is arbitrary and some is not (especially with the three derivatives of "contour" present.)
I'm not versed w/ vertex() either. Better take a look at it for yourself then: 8-X
Seems like some beginShape() & endShape() are needed too...
If that's so, perhaps this might work now: :(|)
Yes, that one does work. Thank you.