 |
Author |
Topic: BImage in a video question (Read 262 times) |
|
kensuguro
|
BImage in a video question
« on: Oct 26th, 2004, 10:53am » |
|
I'm just beginning to learn Processing, and came by to see if I can find any help. Sorry if the questions' been asked before. (I did try searching) Anyhow, I'm having trouble understanding where exactly should a image() line be to be able to draw something on the screen. Here's an example: Code: BImage test; BImage old; int threshold; boolean newFrame = false; color now; color before; int diff; int count=0; void setup() { size(320*3, 240); beginVideo(320, 240, 2); framerate(3); threshold=20; test=video; old=video; } void videoEvent() { newFrame = true; } void loop() { colorMode(RGB, 100); if(newFrame == true){ for(int i=0; i<320*240; i++){ test.pixels[i]=color(0,0,brightness(old.pixels[i])); } image(video, 0, 0); newFrame = false; // println("O"); } else{ println("X"); } } |
| Here, I have the image(video,0,0) line after I've drawn into the test.pixels[i] array. But, for some strange reason, althought I expected to see the unaltered "video" instead, I get the "test" image instead. Now if I move the image() line above the for() loop, then I do get the unaltered video feed. But I still get the "test' image, inbetween the screen refreshes. (notice my capture framerate is 2, and the project framerate is 3) Well, my question is, what's going on? I don't understand why there's become some sort of link between "test" and "video". Or am I going about this the wrong way? Any help appreciated.
|
« Last Edit: Oct 26th, 2004, 12:03pm by kensuguro » |
|
|
|
|
TomC
|
Re: BImage in a video question
« Reply #1 on: Oct 26th, 2004, 11:02am » |
|
This is to do with how = (the assignment operator) works on objects. Basically, if test and video are Objects, when you say test=video; you're saying "test refers to the same object as video". This can be confusing, because when you do the same with ints or floats, a=b; means "set the value of a to the value of b". To fix it, you need to make test be a copy of video instead. Example... Code: BImage img1 = new BImage(); // make img2 refer to the same data that img1 refers to BImage img2 = img1; // img1 and img2 are the same object, so both are modified: img2.pixels[0] = color(255,0,0); |
| Code: BImage img1 = new BImage(); // use a copy of img1, and call it img2 BImage img2 = img1.copy(); // img1 remains untouched, img2 is modified: img2.pixels[0] = color(255,0,0); |
| More... In C++, you might have heard about the difference between "pass by reference" and "pass by value" for functions. In Java there's no distinction, primitives like int and float are always passed by value, and Objects are always passed by reference. You can think of assignment as a special kind of function (in C++ you can write your own if you want, called an operator). In the case of assignment, pass by value makes a copy of the data and assigns it to the new variable name - so the original data isn't changed when the new variable is changed. However, pass by reference means the new variable refers to the old one, so both will be affected by changes to each other. I hope I got that the right way around!
|
« Last Edit: Oct 26th, 2004, 11:14am by TomC » |
|
|
|
|
kensuguro
|
Re: BImage in a video question
« Reply #2 on: Oct 26th, 2004, 12:06pm » |
|
whoa, thanks for the quick reply. I did go back and changed my question, but your answer would solve both my questions. That's what was causing the confusion. So instead of creating a variable of type BImage, I need to make an instance of class BImage.. Or well, maybe that's not quite right but I get the picture. But anyway, technicalities aside, I will try your advice and check back. I'm still new to both C++ and java, so it's going to take a while to get my programming terminology straight.. hehe.
|
« Last Edit: Oct 26th, 2004, 12:11pm by kensuguro » |
|
|
|
|
kensuguro
|
Re: BImage in a video question
« Reply #3 on: Oct 26th, 2004, 1:39pm » |
|
and walla, it did work. thanx again!
|
|
|
|
|