JAI Image smoothing

I have been working on this project for Image smoothing of 2D grainy Images. Here is a code snippet of Image smoothing using JAI median filter algorithm. The amount of smoothing is controlled by the masksize variable mentioned in the static create method of MedianFilterDescriptor class. This class also provides 4 different types of MedianFilterShape for different masking patterns on the Image : MEDIAN_MASK_SQUARE, MEDIAN_MASK_PLUS, MEDIAN_MASK_X and MEDIAN_MASK_SQUARE_SEPARABLE.

Code snippet :

File file = new File("grainy-image.jpg");
InputStream in = null;
long filelength = 0L;
ByteArrayInputStream bais = null;
try {
in = new FileInputStream(file);
filelength = file.length();
byte[] bytes = new byte[(int)filelength];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0)
offset += numRead;
if (offset < bytes.length)
throw new IOException("Could not completely read file "+file.getName());
} bais = new ByteArrayInputStream(bytes);
RenderedImage src = null;
src = JAI.create("stream", SeekableStream.wrapInputStream(bais, true));
RenderedImage renderedImage = PlanarImage.wrapRenderedImage(src);
RenderedOp renderedOp = MedianFilterDescriptor.create(renderedImage,
MedianFilterDescriptor.MEDIAN_MASK_SQUARE , 5, null);
BufferedImage image = renderedOp.getAsBufferedImage();
ImageIO.write(image, "jpeg", new File("smooth-image.jpg"));
} catch (IOException x) {

Here are the before and after Images

Before :

After :

You could check the JAI apis to sharpen the Image too. Here are some JAI tutorials pdf


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s