We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
masking problem (Read 592 times)
masking problem
Nov 8th, 2009, 4:00pm
 
Hi all,

I'm trying to divide an image into a number of slices and apply a mask to each one.
In setup, I loop through the number of slices and create a PImage for each one using the get() method. I then create a new shape to mask each one (an instance of a class I created) and pass it the width and height of the new PImage. I then apply the shape as a mask to the slice.
for some reason I get the following error: "The PImage used with mask() must be the same size as the applet".
I printed both their sizes just before I apply the mask and they return the same values.
Any ideas?

Here's the code:


PImage img;
PImage shapeImg;
PImage slices[];
MaskShape[] mymasks;
int numofslices, sliceWidth, sliceHeight;


void setup(){
 size(300,225);
 smooth();
 img = loadImage("../../../LiverpoolPhotos/liverpool8.jpg");
int numofSlices = int(random(3, 10));
slices = new PImage[numofSlices];
sliceWidth = img.width / numofSlices;
sliceHeight = img.height / numofSlices;

 mymasks = new MaskShape[numofSlices];
 
 for(int i=0; i<numofSlices; i++){
   int xpos = int(random(width-50));
   int ypos = int(random(height-50));
 
  slices[i] = img.get(i*sliceWidth+1, i*sliceHeight+1, sliceWidth, sliceHeight);
   mymasks[i] = new MaskShape(sliceWidth, sliceHeight);
   
 slices[i].mask(shapeImg);
 println("slice: "+slices[i].width+", "+slices[i].height);
 println("shape: "+shapeImg.width+", "+shapeImg.height);
 image(slices[i], xpos, ypos);
 }

}

void draw(){

}


class MaskShape{
 float randx = 20+random(width-40);
 float randy = 20+random(height-40);
 int numofver = int(random(3, 5));
 float vercs[][] = new float[numofver][2];
 PGraphics pg;
 int shapeWidth, shapeHeight;


 MaskShape(int imgWidth, int imgHeight){
 shapeWidth = imgWidth;
 shapeHeight = imgHeight;
 println(shapeWidth+", "+shapeHeight);
 pg = createGraphics(shapeWidth, shapeHeight, JAVA2D);



   vercs[0][0] = randx;
   vercs[0][1] = randy;
   vercs[1][0] = randx + random(15, 40);
   vercs[1][1] = randy + random(-40, 40);
   vercs[2][0] = randx + random(15, 40);
   vercs[2][1] = vercs[1][1] + random(15, 40);
   for (int j=3; j<numofver; j++){
     vercs[j][0] =   vercs[j-1][0] - random(10, 40);
     vercs[j][1] = randy+random(15, 40);
   }

     pg.background(0);
     pg.fill(255);
     pg.beginDraw();
     pg.beginShape();
     vertex(randx, randy);
     for (int k=0; k<numofver; k++){
     pg.vertex(vercs[k][0], vercs[k][1]);
   }
   pg.endShape(CLOSE);
   pg.endDraw();

  shapeImg = pg.get(0, 0, pg.width, pg.height);

 }

}



Thanks,
Shira
Re: masking problem
Reply #1 - Nov 8th, 2009, 4:23pm
 
i couldnt figure it out, but ill tell you some things i found out. maybe that helps you to solve the problem.

instead of the random numOfSlices i hardcoded it. 1,2,4,5,8 is not working 3, 6,7,9 seems to work.

and if you run one of the not working numbers and move the print stuff above your  slices[i].mask(shapeImg); line. you will see that you get these resutls :
slice: 39, 99
shape: 40, 100

so thats probably where the problem is, you just have to find out what causes it.
Re: masking problem
Reply #2 - Nov 8th, 2009, 4:35pm
 
i could track down where the problem is. Its in this line of code :
 slices[i] = img.get(i*sliceWidth+1, i*sliceHeight+1, sliceWidth, sliceHeight);

remove the +1 and it is working, although i still dont see anything on the screen. but maybe you know what to do now.
Re: masking problem
Reply #3 - Nov 9th, 2009, 12:11am
 
You are right! I wouldn't have figured it out in a million years...
No idea why I added that 1 in the first place.
Thanks!
Page Index Toggle Pages: 1