Dealing with border pixels
in
Programming Questions
•
2 years ago
Hello,
I am working on a program which will apply a median filter to an image then save and load the result, then do this again repeatedly. My program is functional though the results are less than ideal because I am applying a 3x3 matrix to collect the values for one pixel's nearest neighbors and in order to keep the array from going out of bounds I am neglecting the first and last columns and rows. The results for this leave those pixels black which then alters the next iteration to where eventually I am left with a solid black frame.
To sum up, I would like to know how to handle these border pixels so that the returned image is a consistent alteration of the original.
Any help, advice, or direction is much appreciated.
Thank you,
Matthew
I am working on a program which will apply a median filter to an image then save and load the result, then do this again repeatedly. My program is functional though the results are less than ideal because I am applying a 3x3 matrix to collect the values for one pixel's nearest neighbors and in order to keep the array from going out of bounds I am neglecting the first and last columns and rows. The results for this leave those pixels black which then alters the next iteration to where eventually I am left with a solid black frame.
To sum up, I would like to know how to handle these border pixels so that the returned image is a consistent alteration of the original.
Any help, advice, or direction is much appreciated.
Thank you,
Matthew
- /**
* Matthew Lord : 2010 / 11 . 30
*
* using a variety of examples from:
* "Processing" by Fry & Reas
*
* This code will process an image
*
*/
import java.util.Arrays;
PImage img;
int matrixSize = 9;
int rgbListMedian = 4;
int offset = 1;
// SETUP
void setup( ) {
size( 320, 180 );
}
// DRAW
void draw( ) {
// to render the first "n" images
if( frameCount <= 620 ) {
// select an image
if( frameCount <= 1 ) {
// load the seed image
img = loadImage( "testimage.jpg" );
} else if( frameCount > 1 ) {
// assign the new image to the "img" variable
img = loadImage( "processedimgs/testimage." + nf( frameCount - 1, 4 ) + ".tif" );
}
// load the pixels of the image
img.loadPixels( );
// Create a new image to store the new data into
// Create an opaque image of the same size as the original
PImage medianImg = createImage( img.width, img.height, RGB );
// cycle through all pixels
// Loop through every pixel in the image.
for( int y = offset; y < img.height - offset; y++ ) {
for( int x = offset; x < img.width - offset; x++ ) {
int z = 0;
int[ ] AList = new int[ matrixSize ];
int[ ] RList = new int[ matrixSize ];
int[ ] GList = new int[ matrixSize ];
int[ ] BList = new int[ matrixSize ];
for( int ky = -offset; ky <= offset; ky++ ) {
for( int kx = -offset; kx <= offset; kx++ ) {
int pos = ( y + ky ) * width + ( x + kx );
int alphaVal = int( alpha( img.pixels[ pos ] ) );
int redVal = int( red( img.pixels[ pos ] ) );
int greenVal = int( green( img.pixels[ pos ] ) );
int blueVal = int( blue( img.pixels[ pos ] ) );
// place the RGB values into their respective arrays
AList[ z ] = int( alphaVal );
RList[ z ] = int( redVal );
GList[ z ] = int( greenVal );
BList[ z ] = int( blueVal );
z++;
}
}
// sort the Arrays
Arrays.sort( AList );
Arrays.sort( RList );
Arrays.sort( GList );
Arrays.sort( BList );
// select the median value for each array
color medianColor = color( RList[ rgbListMedian - 1 ], GList[ rgbListMedian - 1 ], BList[ rgbListMedian - 1 ], AList[ rgbListMedian - 1 ] );
// assign the RGB values to the new image pixel in the same location as the original
medianImg.pixels[ y * img.width + x ] = color( medianColor );
}
}
medianImg.updatePixels( );
image( medianImg, 0, 0 );
saveFrame( "processedimgs/testimage.####.tif" );
}
}
1