Hello everyone,
I've written a small program that processes black and white images pixel by pixel to determine how how many of the neighbouring pixels are black or white. Then based on this value it changes the colour of the pixel in question.
My problem is that the quality of the output images seems to be significantly lower than the input image. I'm not sure if this is an error with my code or what.
The original images were created using Adobe Illustrator and have a resolution of 1280 x 800 and are 72 DPI.
Here is my code:
- //Define PImage variables
- PImage source;
- PImage destination;
- String baseName = "File_";
- boolean more;
- int z=0;
- int t=0;
- color white = color(255);
- color black = color(0);
- void setup()
- {
- size (1280,800);
- do
- {
- for (z=1; z<9999; z++)
- {
- //Load source image
- more = new File(baseName+nf(z,4)+".png").exists();
- source = loadImage(baseName+nf(z,4)+".png");
- //Create destination image
- destination = createImage(source.width, source.height, ALPHA);
- //Look at both images pixels
- source.loadPixels();
- destination.loadPixels();
- //Set the size of the scanning matrix
- int matrixsize = 27;
- //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];
- destination.pixels[loc] = black;
- //If current pixel colour is white examine if the colour needs changing the colour
- if ( pix == white)
- {
- //Pass the x y coordinates and matrix info to the perimeterFeature metho
- //perimeterFeature method then scans the surronding pixels and returns a new colour
- color newPix = perimeterFeature(x,y,matrixsize,source);
- //Set pixel colour to new value
- destination.pixels[loc] = newPix;
- }
- /*else
- {
- destination.pixels[loc] = black;
- }*/
- }
- }
- //Update pixels in destination
- destination.updatePixels();
- //Display the destination image
- image(destination,0,0);
- //Counter to save the files with even numbers
- t = t+2;
- //Save image
- save(baseName+nf(t,4)+".png");
- }
- }while(more);
- }
- void draw()
- {
- }
- color perimeterFeature(int x, int y, int matrixsize, PImage img)
- {
- int countWhitePixels = 0;
- int offset = matrixsize / 2;
- // Loop through convolution 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 count it
- if (pix == white)
- {
- countWhitePixels = countWhitePixels + 1;
- }
- else
- {
- countWhitePixels = countWhitePixels + 0;
- }
- }
- }
- if(countWhitePixels > 0)
- {
- if (countWhitePixels < 729)
- {
- if (countWhitePixels < 625)
- {
- if (countWhitePixels < 529)
- {
- countWhitePixels = 255;
- }
- else
- {
- countWhitePixels =0;
- }
- }
- else
- {
- countWhitePixels =0;
- }
- }
- else
- {
- countWhitePixels = 0;
- }
- }
- else
- {
- countWhitePixels = 0;
- }
- // Make sure colour is within range
- countWhitePixels = constrain(countWhitePixels,0,255);
- // Return the resulting color
- return color(countWhitePixels);
- }
Output image:
You can see the loss in definition around the edges of the circle, which should have small teeth but dont in the second image.
Can anyone offer any advice as to why there is such a difference between the image quality?
1