Improve speed of image processing
in
Programming Questions
•
4 months ago
Hello,
I've written a program that grayscales pixels depending on the colour of the neighbouring pixels. I only want to grayscale the pixel if it is white so the first step is to scan through the image pixel by pixel to determine the colour of the pixel. If the pixel is white then the location of the pixel is passed to a method that scans the neighbouring pixels and counts the number of neighbouring white pixels. Based on how many neighbouring pixels are white the colour of the center pixel is then altered.
I want to use this program to process thousands of images (up to 9999), however as the images are processed the program becomes increasingly slow. Do you have any tips as to how I can improve the speed of the image processing?
Thanks
- //Define PImage variables
- PImage source;
- PImage destination;
- String baseName = "MRobot_";
- boolean more;
- int z=1;
- int even = 2 ;
- void setup()
- {
- size (1280,800);
- }
- void draw()
- {
- do
- {
- for (z=1; z<9999; z++)
- {
- //Load source image
- source = loadImage(baseName+nf(z,4)+".png");
- more = new File(baseName+nf(z,4)+".png").exists();
- destination = createImage(source.width, source.height, ALPHA);
- //Look at both image's pixels
- source.loadPixels();
- destination.loadPixels();
- //Set the size of the scanning matrix
- int matrixsize = 3;
- //Loop through every pixel column
- for ( int x = 0; x < source.width; x++)
- {
- //Loop through every pixel row
- for (int y = 0; y < source.height; y++)
- {
- //Calculate 1D pixel location
- int loc = x + y * source.width;
- //Determine colour of current pixel
- color pix = source.pixels[loc];
- //If current pixel colour is white examine if the colour needs changing the colour
- if ( pix == color(255))
- {
- //Pass the x y coordinates and matrix info to the greyscale method
- //greyscale method then scans the surronding pixels and returns a new colour
- color newPix = greyscale(x,y,matrixsize,source);
- //Set pixel colour to new value
- destination.pixels[loc] = newPix;
- }
- else
- {
- destination.pixels[loc] = color(0); //black
- }
- }
- }
- //Update pixels in destination
- destination.updatePixels();
- //Set background to black
- background(0);
- //Display the destination image
- image(destination,0,0);
- for (even=2; even<z*2; even = even +2)
- {
- //Save image with even number
- save(baseName+nf(even,4)+".png");
- }
- }
- }while(more);
- }
- //Method to greyscale a pixel based on the surronding pixel colours
- color greyscale(int x, int y, int matrixsize, PImage img)
- {
- int greyvalue = 0;
- int offset = matrixsize / 2;
- // Loop through scanning matrix
- for (int i = 0; i < matrixsize; i++)
- {
- for (int j= 0; j < matrixsize; j++)
- {
- // What pixel are we testing
- int xloc = x+i-offset;
- int yloc = y+j-offset;
- int loc = xloc + img.width*yloc;
- // Make sure we have not walked off the edge of the pixel array
- loc = constrain(loc,0,img.pixels.length-1);
- //Colour of the current pixel
- color pix = img.pixels[loc];
- //if the colour of the current pixel is white
- // add one to "greyvalue"
- if (pix == color(255))
- {
- greyvalue = greyvalue + 1;
- }
- else
- {
- greyvalue = greyvalue + 0;
- }
- }
- }
- //Means that there is a least one white pixel within the scanning matrix
- if(greyvalue > 0)
- {
- //Measn there is at least one black pixel within the scanning matrix
- if (greyvalue < 9)
- {
- greyvalue = 255; //white
- }
- else
- {
- greyvalue = 100; //grey
- }
- }
- else
- {
- greyvalue = 0; //black
- }
- // Make sure colour is within range
- greyvalue = constrain(greyvalue,0,255);
- // Return the resulting color
- return color(greyvalue);
- }
1