Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS 

Java - Erode

// JAI Filter erode

public RenderedImage erode(BufferedImage img)
	{
		KernelJAI kernel = new KernelJAI(7, 7, new float[]{
															0, 0, 0, 0, 0, 0, 0,
															0, 1, 1, 1, 1, 1, 0,
															0, 1, 1, 1, 1, 1, 0,
															0, 1, 1, 1, 1, 1, 0,
															0, 1, 1, 1, 1, 1, 0,
															0, 1, 1, 1, 1, 1, 0,
															0, 0, 0, 0, 0, 0, 0
															});
		ParameterBlock pb = new ParameterBlock();
		pb.addSource(img);
		pb.add(kernel);
		
		return JAI.create("erode", pb);
	}

Java - subTraction

// Use JAI filter

public RenderedImage subTraction(BufferedImage img1, BufferedImage img2)
	{
		ParameterBlock pb = new ParameterBlock();
		pb.addSource(img1);
		pb.addSource(img2);
		
		return JAI.create("subtract", pb);
	}

Java - showBitPlanes

// the input it must be a b/w image

public BufferedImage showBitPlanes(BufferedImage bi, int lv)
	{
		int level = 0;
		
		switch(level)
		{
			case 0:
				level = 128;
				break;
			case 1:
				level = 64;
				break;
			case 2:
				level = 32;
				break;
			case 3:
				level = 16;
				break;
			case 4:
				level = 8;
				break;
			case 5:
				level = 4;
				break;
			case 6:
				level = 2;
				break;
			case 7:
				level = 1;
				break;
			default:
					return null;
		}
		
		int width = bi.getWidth();
		int height = bi.getHeight();
		
		BufferedImage img = new BufferedImage(width, height, bi.getType());
		
		for(int x=0; x<width; x++)
			for(int y=0; y<height; y++)
				img.setRGB(x, y, ((bi.getRGB(x, y) & level)/level)*255);
		
		return img;
	}

Java - Dilate3

// pixel before the filter
// |--|--|--|
// | | | |
// |--|--|--|
// | | *| |
// |--|--|--|
// | | | |
// |--|--|--|

// pixel after the filter
// |--|--|--|
// | *| *| *|
// |--|--|--|
// | *| *| *|
// |--|--|--|
// | *| *| *|
// |--|--|--|

public BufferedImage dilate3(BufferedImage bi)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(3, 3, new float[] {
														1f, 1f, 1f, 
														1f, 1f, 1f, 
														1f, 1f, 1f
													 });
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}

Java - EdgeW / EdgeH

public BufferedImage EdgeW(BufferedImage bi)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(3, 3, new float[] {
														-1f, 0f, 1f, 
														-2f, 0f, 2f, 
														-1f, 0f, 1f
													 });
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}


	public BufferedImage EdgeH(BufferedImage bi)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(3, 3, new float[] {
														-1f, -2f, -1f, 
														 0f,  0f,  0f, 
														 1f,  2f,   1f
													 });
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}

Java - Brightness

public BufferedImage brightness(BufferedImage bi, float value)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(1, 1, new float[] {value});
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}

Matlab - DoubleFlip Image

// Flip Width & Height

function flipIMG=DoubleFlip(img)

    tic

    flipIMG = img([1:end/2, end/2:-1:1], [1:end/2, end/2:-1:1], :);

    toc

Java - JMF Simple Filter

import java.awt.Dimension;

import javax.media.Buffer;
import javax.media.Effect;
import javax.media.Format;
import javax.media.ResourceUnavailableException;
import javax.media.format.RGBFormat;

public class SimpleFilter implements Effect
{
	protected Format inputFormat = null;
	protected Format outputFormat = null;
	
	protected Format[] inputFormats = null;
	protected Format[] outputFormats = null;
	
	public AngelMotionCodec()
	{
		inputFormats = new Format[]{ new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
		outputFormats = new Format[]{ new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
	}
	
	/****** Codec ******/
	public Format[] getSupportedInputFormats()
	{
		return inputFormats;
	}

	public Format[] getSupportedOutputFormats(Format input)
	{
		if(input != null)
		{
			if(matches(input, inputFormats) != null)
				return new Format[]{ outputFormats[0].intersects(input) };
			else
				return new Format[0];
		}
		
		return outputFormats;
	}

	public int process(Buffer input, Buffer output)
	{
		// Swap tra input & output
		Object tmp = input.getData();
		
		input.setData(output.getData());
		output.setData(tmp);
		
		return BUFFER_PROCESSED_OK;
	}

	public Format setInputFormat(Format input)
	{
		inputFormat = input;
		
		return input;
	}

	public Format setOutputFormat(Format output)
	{
		if(output != null || matches(output, outputFormats) != null)
		{
			RGBFormat inRGB = (RGBFormat) output;
			
			Dimension size = inRGB.getSize();
			int maxDataLength = inRGB.getMaxDataLength();
			int lineStride = inRGB.getLineStride();
			int flipped = inRGB.getFlipped();
			
			if(size == null)
				return null;
			
			if(maxDataLength < size.width*size.height*3)
				maxDataLength = size.width*size.height*3;
			
			if(lineStride < size.width*3)
				lineStride = size.width*3;
			
			if(flipped != Format.FALSE)
				flipped = Format.FALSE;
			
			outputFormat = outputFormats[0].intersects(new RGBFormat(size, maxDataLength, inRGB.getDataType(), inRGB.getFrameRate(), inRGB.getBitsPerPixel(), inRGB.getRedMask(), inRGB.getGreenMask(), inRGB.getBlueMask(), inRGB.getPixelStride(), lineStride, flipped, inRGB.getEndian()));
			
			return outputFormat;
		}
		
		return null;
	}
	/****** Codec ******/

	/****** PlugIn ******/
	public void close()
	{

	}

	public String getName()
	{
		return "Simple-Filter";
	}

	public void open() throws ResourceUnavailableException
	{

	}

	public void reset()
	{

	}
	/****** PlugIn ******/

	/****** Controls ******/
	public Object getControl(String controlType)
	{
		return null;
	}

	public Object[] getControls()
	{
		return null;
	}
	/****** Controls ******/
	
	/****** Utility ******/
	private Format matches(Format in, Format[] out)
	{
		if(in != null && out != null)
		{
			for(int i=0, cnt=out.length; i<cnt; i++)
			{
				if(in.matches(out[i]))
					return out[i];
			}
		}
		
		return null;
	}
	/****** Utility ******/
}

Java - ImageFilter Simple

// Filtro Embrossing
	public BufferedImage Embrossing(BufferedImage bi)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(3, 3, new float[] {
														-2f, 0f, 0f, 
														 0f, 1f, 0f, 
														 0f, 0f, 2f
													 });
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}


// Filtro Blurring
	public BufferedImage Blurring(BufferedImage bi)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(3, 3, new float[] {
														1f/9f, 1f/9f, 1f/9f, 
														1f/9f, 1f/9f, 1f/9f, 
														1f/9f, 1f/9f, 1f/9f
													 });
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}


// Filtro Sharpening
	public BufferedImage Sharpening(BufferedImage bi)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(3, 3, new float[] {
														-1f, -1f, -1f, 
														-1f,  9f, -1f, 
														-1f, -1f, -1f
													 });
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}
« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS