How to scale pixels[] using scale() ?

edited April 2014 in How To...

I'm trying to scale this square using a scale(2); function to make the pink square fill the whole size (or at least twice the size).

Does anyone know how?

this is my current simple code

//CODE----------------------------

color pink = color(255, 102, 204);
int loc = 0;

void setup(){
  size(300,300);
}

void draw(){
  loadPixels();
  for (int i = 0; i < width/2; i++) {
    for(int j = 0; j < height/2; j++){
      loc = i + (j*width);
      pixels[loc] = pink;
    }
  }
  updatePixels();
}

Answers

  • is there a reason why you are not using the rect() method for your square and pushMatrix()/popMatrix() for matrix tranformations like scaling and skewing and translating??

  • boolean twoTimes = false;
    void setup(){
        size(300,300);
    }
    
    void draw(){
        fill(255, 102, 204);
        if(twoTimes)
           rect(0,0, width, height);
        else 
           rect(0,0, width/2, height/2)
    }
    
    //now you need a way to switch between the boolean, ex keyPressed, mousePressed, etc
    
  • The scale() function has no effect on pixels[], it is intended for shapes drawn in Processing like @Kobby wrote. For reference: http://www.processing.org/reference/scale_.html

    If you still want to accomplish this using pixels[] then change the conditions in the for loop.

  • I'm using a kinect, and a silhouette of the user function is drawn using userMap() from SimpleOpenNI. The userMap is using these pixels array. So I'd like to resize my silhouette to be bigger than 640 x 480 which is what its capturing in thats all. so i guess it isnt possible huh. thanks for the info.

  • _vk_vk
    edited April 2014 Answer ✓

    I think you could work the math of the loops as @asimes suggested kind of making a interpolation algorithm... I don't know how to do that. But, now that you explained the need, maybe a PGraphics approach can be enough. Some thing like:

    color pink = color(255, 102, 204);
    int loc = 0;
    PGraphics pg;
    
    void setup() {
      size(300, 300);
      pg = createGraphics(300, 300);
      pg.loadPixels();
      for (int i = 0; i < width/2; i++) {
        for (int j = 0; j < height/2; j++) {
          loc = i + (j*width);
          pg.pixels[loc] = pink;
        }
      }
      pg.updatePixels();
      // use (2, 2) to double the size (see scale reference)
      scale(1.8, 1.8);
      image(pg, 0, 0);
    }
    
  • cool. that PGraphics is just what I'm missing.

    Perfect thanks _vk i'll try it out in a bit

  • Yes it works, I found another example code from here. uses the same concept

    http://cs.marlboro.edu/courses/spring2013/dedhour/kinect.attachments/kinectBackgroundRemoval.pde

    import SimpleOpenNI.*;
    
    import processing.opengl.*;
    
    SimpleOpenNI kinect;
    
    PImage userImage = createImage(640, 480, RGB);
    int userID;
    int[] userMap;
    
    void setup(){
      size(1280, 800, OPENGL);
    
      kinect = new SimpleOpenNI(this);
      kinect.enableDepth();
      kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_NONE);
    }
    
    void draw(){
      kinect.update();
    
      if(kinect.getNumberOfUsers() > 0){
    
        userMap = kinect.getUsersPixels(SimpleOpenNI.USERS_ALL);
        userImage.loadPixels();
        userImage.resize(640, 480); 
        //scale up the image to our windown size
        for(int i=0; i<userMap.length; i++){
          if(userMap[i] != 0){
            userImage.pixels[i] = color(0, 255, 0);
          } else {
            userImage.pixels[i] = color(0, 0, 0);
          }
        }
    
        userImage.updatePixels();
        userImage.resize(width, height);
        image(userImage, 0, 0);
      }
    }
    
    void onNewUser(int uID){
      userID = uID;
      println("tracking");
    }
    
  • Thanks this worked for me! Here is my code using windows 7. Without using the opengl because that seemed to cause some lagg.

    import SimpleOpenNI.*;
    import processing.opengl.*;
    SimpleOpenNI context;
    PImage userImage=createImage(640,480,RGB);
    int[] userMap;
    PImage rgbImage;
    color pixelColor;
    
    void setup(){
    
      size(displayWidth, displayHeight, OPENGL);
      context=new SimpleOpenNI(this);
      context.enableRGB();
      context.enableDepth();
      context.enableUser();
    }
    void draw(){
    
      background(255);
      context.update();
      rgbImage=context.rgbImage();
    
      stroke(0);
      strokeWeight(10);
      if (mousePressed == true) {
        line(mouseX, mouseY, pmouseX, pmouseY);
      }
    
      userMap=context.userMap();
      userImage.loadPixels();
      userImage.resize(640, 480);
    
      for(int y=0;y<context.depthHeight();y++){
            for(int x=0;x<context.depthWidth();x++){
              int index=x+y*640;
              if(userMap[index]!=0){
                 pixelColor=rgbImage.pixels[index];
                userImage.pixels[index]=color(0,0,0);
              }else{
                userImage.pixels[index]=color(255);
              }
            }
          }
        userImage.updatePixels();
        context.update();
        rgbImage=context.rgbImage();
        userImage.resize(width, height);
        image(userImage,0,0);
    
    }
    boolean sketchFullScreen() {
      return true;
    }
    
Sign In or Register to comment.