how to loadImage over drawing and video?

edited July 2014 in How To...

Hi all,

I'm playing with webcam and drawing geometrics, but I want to load 2 images over it. But I don't want to redraw the image all the time. Is there a way to load only once the image and set it over all the sketch?

I try with PGraphics but didn't figure it out how to use it for loadImage.

Thanks!

Tagged:

Answers

  • Answer ✓

    This is a bit unclear...

    "I don't want to redraw the image all the time"
    Why? Actually, you have no choice, this is the way Processing works.

    "Is there a way to load only once the image and set it over all the sketch?"
    Er, yes, that's also the way most Processing sketches work.

    Are you mixing up loadImage() with image()?

    Perhaps see if the second article (and more!) of the Technical FAQ can help you...

  • edited July 2014

    Thanks PhiLho! and sorry about my poor english lvl.

    "Why? Actually, you have no choice, this is the way Processing works." Yes! you are right, but... What happen if you want to display an image over your interactive drawing, or over your webcam, something like a watermark. A layer may be.

    Here is an example of what I'm looking for:

    layer

    Those are 3 captures shown the image over the drawing. In my script I'm displaying the image at the end of the void draw() just to get it on top. But the image is the same, and in the same place, and I'm wasting precious CPU cycles.

    Is not a problem on this particular sketch, but I'm working on one with webcam interactions what's consuming lots of resources.

    Thanks again for your time!

  • Answer ✓

    Perhaps you meant display when you say load? :-/

  • GoToLoop, you are right! I've edited my previous post. Thanks

  • Answer ✓

    "I'm wasting precious CPU cycles"
    Well, again, there is no magic, for an image to be over others, it has to be drawn above them, and thus, redrawn on each frame.

    Now, if you are looking for performance, you might do your sketch in OpenGL, load the image on the GPU (actually the video memory, as a texture probably), and let some shader (?) to do this drawing, thus consuming no CPU.

  • You quoting me about my precious CPU cycles, make me think about maybe I'm wasting more time than cycles...

    I've test the performance of my computer, displaying the image every time, and not displaying the image at all, and there is no change in the performance.

    Sorry, I'm silly newbie

  • At least, you took the steps to test (finally!), which is the right approach.

    A classical say about optimization of code: "don't do it". :-) There is more about it, but basically it says not to try if you don't know what you do. Modern computers are quite fast, so doing optimizations like GoToLoop like(d) to do, saving (perhaps) 1 microsecond over a frame time of 16 ms is a waste of time, and often leads to less readable code. While today, one of the most important thing about code is to be readable, to ease maintenance. It is more important in enterprise software than in Processing sketches, often quick hacks which we rarely re-open, but still...

    Mmm, I digress. My point is: do optimizations only if you find out a real speed issue. In other words, most of the time, if you feel you need an optimization, measure performance, before, and after the optimization.
    Ideally, use tools to find out the real bottleneck (not obvious in Processing, though). And optimize only this bottleneck: don't waste time on optimizing something that runs only on each keystroke, for example (unless it is taking several seconds!), because users are slow. But avoid obvious (or less obvious) waste of CPU, like loading the same image in draw() in each frame (a classical newbies' error) or creating thousand of objects in each frame.

Sign In or Register to comment.