pixels[] and get()

edited February 2015 in Questions about Code

when i use pixels[] or get() the program gets stuck there... am i doing something wrong? this is a cut down version of my code : the console shows "Starting..." and then nothing happens...

PImage img,eye;

void setup(){

img=loadImage("C:\Users\MY\Desktop\P\face.png");

eye=loadImage("C:\Users\MY\Desktop\P\eye.png");

size(img.width,img.height); }

void draw(){

image(img,0,0);

image(eye,0,0);

}

void mousePressed(){

loadPixels();

int match=0;

println("Starting...");

for(int i=0;i<=40;++i)

for(int j=0;i<=20;++j)

pixels[j*i]=0;

match++;

println(match/8);

updatePixels();

}

Answers

  • edited February 2015

    I believe that loadPixels() and updatePixels() should be img.loadPixes() and img.updatePixels() so the system knows which image to work on. See the loadPixels() example.

  • edited February 2015

    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

    • Always place size() as setup()'s 1st statement in order to avoid bugs!
    • If you just wanna fill an array w/ a chosen value, just use Arrays.fill() method:

    java.util.Arrays.fill(pixels, 0);

    https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#fill-int:A-int-

  • edited February 2015

    Alternatively we can use a for () loop in place of Arrays.fill():

    for (int i = pixels.length; i-- != 0; pixels[i] = 0);

    And as @bilmor's pointed out, there's a big diff. between the canvas' pixels[] and a PImage's pixels[]: #-o

    1. http://processing.org/reference/pixels.html
    2. http://processing.org/reference/PImage_pixels.html
  • edited February 2015

    i didnt exactly want to just change the color... its a face detection basic... i think its more of an error from the processing(3.0a5 [alphas always suck]) side... but i cant understand the problem... its completely illogical! when i copied the code i pasted here(and added a few stuff(size) as GoToLoop suggested) i get array outofbound exception but then i wrote the program again in a new file i get the desired output... how does processing know i am trying to...?? okay yeah its sounds crazy but i think i am missing something... could some find the diff btw the 2 codes?!(have a look at the console)... the very first code of this didnt even give an error... just got stuck after "Starting" and never gave the result...

    forum

  • heres a video of what i meant :

  • edited February 2015

    :-O

  • Your code change the pixels of the sketch, in mousePressed, but in the next frame (some 1/60th of second later), you overwrite these changes in draw()! That's why you see nothing.

    Note that I don't see the point of your match variable which is incremented once, and when you divide an integer with another, you get an integer, so the result of the division is zero.

    And if you use pixel[x*y], you don't iterate over all the pixels of your square... See the numerous examples doing this to see how to do it properly.

  • edited February 2015

    PhiLho : the line "pixels[j* i]=0;" was just used for debugging... this is not the program... the 2 comments of mine above yours show the actual problem... the two identical code give 2 diff results... one gets stuck at pixels.. and the other runs perfectly... (have a look @ the video).. and just FYI its a face feature detection primitive code and match is the no: of pixels that exactly match and is supposed to be inside the if statement now replaced by pixels[j* i]=0; for debugging..

  • Your images are unreadable, too small. And I don't see the point of asking help on code which is not the one you use.

    Also see How to format code and text... (Looks like you were using single backslashes where you have double ones. BTW, even in Windows, you can just use forward slashes there...)

  • edited March 2015

    PhiLho FYI \ \ are needed cuz only \ is taken as an escape seq!

  • edited March 2015

    Read my (rejected!) message again:

    1. Format your code. Edit your message(s) and follow the given instructions. People will be more willing to help if they can read your code and copy & paste it as is.
    2. You can use forward slashes / : img=loadImage("C:/Users/MY\Desktop/P/face.png");
      Simpler, and easier to read.
  • Why isnt anyone answering my q! ua doing everything other than ansing my q PhiLho... my code is just fine the way it is... there is no prob with the imgs... watch the video please! and tell me whats diff between the two codes... this video

  • Your inner for loop is checking the wrong variable. I'd tell you a line number except you haven't formatted your code...

  • edited May 2015

    `

  • Your video is gone, can't see it

    I downloaded Processing 3.0a5 to try this out and used the code below based on your picture (matching code to a tiny picture is a pain, you shouldn't ignore PhiLho's suggestion). I ran it on OS X 10.10 without any errors however I commented out the img code because I don't have your images and it has no effect on the loop logic. Maybe upload the images somewhere online, so that others can run the sketch exactly as you did

    PImage img, eye;
    
    void setup() {
      size(500, 500);
      background(0);
      //img = loadImage("C:\Users\MY\Desktop\P\face.png");
      //eye = loadImage("C:\Users\MY\Desktop\P\eye.png");
    }
    
    void draw() {
      //image(img, 0, 0);
      //image(eye, 0, 0);
    }
    
    void mousePressed() {
      loadPixels();
      println("Starting...");
      for (int i = 0; i <= 40; ++i)
        for (int j = 0; i <= 20; ++j)
          pixels[(j*width)*i]=0;
      println("Ending");
      updatePixels();
    }
    
  • edited May 2015

    Just to clarify, I mentioned uploading the images so that other could run under the same circumstances in case it actually is a problem with Processing 3.0a5 (and grabbing the images from "C:\Users\MY\Desktop\P\"). It should not affect the sketch since the loop is not based on the image dimensions but there are no obvious problems with the sketch

  • @asimes ignore this post... The same code when runned in a new window works perfectly! mostly it was a one time bug with Processing! I get the results properly now!

Sign In or Register to comment.