Moving pixels a row at a time from multiple images to a single new image

edited January 2016 in How To...

Not sure if the title describes this properly. I want to take say 100 images and extract a single row of pixels from each image and then reconstitute the images.

load initialPix1-100

get row 1 from each image and make finalpix_one from these rows

get row 2 from each image and make finalpix_two from these rows

get row 3 from each image and make finalpix_three from these rows

##########

void setup(){

size(400,400); img_one = loadImage("initial_one.jpg"); img_two = loadImage("initial_two.jpg"); img_three = loadImage("initial_three.jpg");

finalpix_one.set(0,0, initial_one.get(0,1,initial_one.width,1) ); finalpix_one.set(0,1, initial_two.get(0,1,initial_two.width,1) ); finalpix_one.set(0,2, initial_three.get(0,1,initial_three.width,1) );

finalpix_two.set(0,0, initial_one.get(0,2,initial_one.width,1) ); finalpix_two.set(0,1, initial_two.get(0,2,initial_two.width,1) ); finalpix_two.set(0,2, initial_three.get(0,2,initial_three.width,1) );

finalpix_three.set(0,0, initial_one.get(0,3,initial_one.width,1) ); finalpix_three.set(0,1, initial_two.get(0,3,initial_two.width,1) ); finalpix_three.set(0,2, initial_three.get(0,3,initial_three.width,1) );

My question is -- does this make sense or would it be easier to do this with pixels[] can anyone see an easy way to pick if this works with taking 2 rows at a time rather than a single row? If I keep spitting out to the final pix will it overwrite it or append like I want-- there has to be a way to do this that I am not seeing.

Answers

  • edited January 2016 Answer ✓

    You want to do a same task with lots of pictures, so you need an algorithm to do it. First, have an array of images: PImage[] images = new PImage[numImages];, if you rename all your pictures to be 0.jpg, 1.jpg, 2.jpg and so on, you can load them with a loop:

     for (int i = 0; i < numImages; i++) {
      images[i] = loadImage(i + ".jpg");
     }
    

    now, you can loop through all of the images and extract needed row, fortunately, each row of pixels corresponds with the number of picture!

       PImage finalImg = createImage(w, h, RGB);
       for (int i = 0; i < numImages; i++) {
        finalImg.loadPixels();
        for (int j = 0; j <= w; j++) {
         finalImg.pixels[j + w*i] = images(i).get(j, i);
        }
        finalImg.updatePixels();
       }
    

    This is untested, but you get an idea, and I hope can debug if it does not work.

    Also mind that it is assumed that all pictures have the same size.

Sign In or Register to comment.