Results 1 to 3 of 3

Thread: Blob location in an image

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Blob location in an image

    Well i'm stuck... I'm working on a program that detects blobs in a image. It's a B/W image were there can be multiple small blobs (approx. 2-6 blobs, different locations in the image, and are about 10-30 pixels in dia. but can be irregular shaped). I have a JAVA script that does this, and i tried to read the code, but not really understanding it. It uses a "walk around" method. When it sees a white pixel, it will walk it's way around the blog, logging the x and y max and min along the way. I'm not sure how to write some logic to do this in VB.net. Anyone have an idea? Maybe some pseudo code that could help? I have the understanding, however i just dont know how to control the computer around the blob. Any ideas?

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Blob location in an image

    Can we see the java code? Someone else might be able to understand it.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Blob location in an image

    There are six JAVA files. Each window below is a different JAVA text file. Not sure what order they are called in. If your a JAVA expert, I guess you'll know!

    The program reads a from a webcam, then analyzes the image for white dots on a black background. Then it will locate the blob on an X and Y cord and use those cords as an X and Y input. The white dots will change in size, location and quantity with each frame. This is done in real time, so quickness is key.

    (Please note this is not my program. I'm not sure if any copy write laws or credit needs to be taken into consideration)


    Main.JAVA
    Code:
    /*
     *	Copyright Frank van Leeuwen. Free for personal and educational use.
     *	Please support me via PayPal.com (account [email protected]).
     *	You may alter the code if you leave this notice and share it on the Web.
     */
    
    class Main
    {
    	public static void main(String[] args) throws Exception
    	{
    		BlobDetection detector;
    		PaintBlobs painter;
    
    		new Camera(new ImageProxy(
    			detector = new BlobDetection(
    			painter = new PaintBlobs(null))));
    
    		painter.blobs = detector;
    	}
    }

    Blob.JAVA
    Code:
    /*
     *	Copyright Frank van Leeuwen. Free for personal and educational use.
     *	Please support me via PayPal.com (account [email protected]).
     *	You may alter the code if you leave this notice and share it on the Web.
     */
    
    import java.awt.*;
    import java.awt.geom.*;
    import java.util.*;
    
    class Blob
    {
    	interface Container
    	{
    		Vector<Blob> getBlobs();
    	}
    
    	int[][][]		shape;
    	Polygon			poly;
    	Area			area;
    	int				nrPixels
    	,				totalIntensity;
    	BlobDetection	imgSrc;
    	int				x
    	,				y
    	,				w
    	,				h
    	,				cx // Center coords
    	,				cy;
    
    	Blob(int[][][] shape, BlobDetection imgSrc)
    	{
    		this.shape = shape;
    		this.imgSrc = imgSrc;
    		determineBounds();	// You may remove this
    		getArea();			// and this
    	}
    
    	int getNrOfPixels()
    	{
    		if (nrPixels == 0)
    		{
    			//TODO
    		}
    		return nrPixels;
    	}
    
    	float getIntensity()
    	{
    		if (totalIntensity == 0)
    		{
    			//TODO (use imgSrc.video.data)
    		}
    		return totalIntensity / getNrOfPixels();
    	}
    
    	Polygon getPoly()
    	{
    		if (poly == null)
    		{
    			poly = new Polygon(shape[0][0], shape[0][1], shape[0][0].length);
    		}
    		return poly;
    	}
    
    	void determineBounds()
    	{
    		Rectangle r = getPoly().getBounds();
    		x = (int)r.getX();
    		y = (int)r.getY();
    		w = (int)r.getWidth();
    		h = (int)r.getHeight();
    		cx = x + w / 2;
    		cy = y + h / 2;
    	}
    
    	Area getArea()
    	{
    		if (area == null)
    		{
    			area = new Area(getPoly());
    
    			for (int i = 1; i < shape.length; i++)
    			{
    				area.subtract(new Area(
    					new Polygon(shape[i][0], shape[i][1], shape[i][0].length)));
    			}
    		}
    		return area;
    	}
    }

    BlobDetection.JAVA
    Code:
    /*
     *	Copyright Frank van Leeuwen. Free for personal and educational use.
     *	Please support me via PayPal.com (account [email protected]).
     *	You may alter the code if you leave this notice and share it on the Web.
     */
    
    import java.awt.*;
    import java.awt.geom.*;
    import java.util.*;
    
    class Blob
    {
    	interface Container
    	{
    		Vector<Blob> getBlobs();
    	}
    
    	int[][][]		shape;
    	Polygon			poly;
    	Area			area;
    	int				nrPixels
    	,				totalIntensity;
    	BlobDetection	imgSrc;
    	int				x
    	,				y
    	,				w
    	,				h
    	,				cx // Center coords
    	,				cy;
    
    	Blob(int[][][] shape, BlobDetection imgSrc)
    	{
    		this.shape = shape;
    		this.imgSrc = imgSrc;
    		determineBounds();	// You may remove this
    		getArea();			// and this
    	}
    
    	int getNrOfPixels()
    	{
    		if (nrPixels == 0)
    		{
    			//TODO
    		}
    		return nrPixels;
    	}
    
    	float getIntensity()
    	{
    		if (totalIntensity == 0)
    		{
    			//TODO (use imgSrc.video.data)
    		}
    		return totalIntensity / getNrOfPixels();
    	}
    
    	Polygon getPoly()
    	{
    		if (poly == null)
    		{
    			poly = new Polygon(shape[0][0], shape[0][1], shape[0][0].length);
    		}
    		return poly;
    	}
    
    	void determineBounds()
    	{
    		Rectangle r = getPoly().getBounds();
    		x = (int)r.getX();
    		y = (int)r.getY();
    		w = (int)r.getWidth();
    		h = (int)r.getHeight();
    		cx = x + w / 2;
    		cy = y + h / 2;
    	}
    
    	Area getArea()
    	{
    		if (area == null)
    		{
    			area = new Area(getPoly());
    
    			for (int i = 1; i < shape.length; i++)
    			{
    				area.subtract(new Area(
    					new Polygon(shape[i][0], shape[i][1], shape[i][0].length)));
    			}
    		}
    		return area;
    	}
    }

    Camera.JAVA
    Code:
    /*
     *	Copyright Frank van Leeuwen. Free for personal and educational use.
     *	Please support me via PayPal.com (account [email protected]).
     *	You may alter the code if you leave this notice and share it on the Web.
     */
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.media.*;
    import javax.media.format.*;
    import javax.media.protocol.*;
    
    /*
     * Connects to a webcam with the default settings @ 30 FPS and sends the data
     * as a byte array to a processing class, which can paint in a window
     * (or add components to it) that is also provided by this class.
     */
    class Camera implements BufferTransferHandler
    {
    	interface Processor
    	{
    		void process(Graphics2D g, Video video);
    	}
    
    	/*
    	 * The component that paints the webcam frames and/or processed data.
    	 */
    	class Video extends Panel
    	{
    		int[] pixels;
    		byte[] data, prev;
    		MemoryImageSource src;
    		Image off;
    		Dimension size;
    		int w, h;
    		Processor processor;
    		boolean busy;
    
    		Video(Dimension size, Processor processor)
    		{
    			this.size = size;
    			this.processor = processor;
    			w = (int)size.getWidth();
    			h = (int)size.getHeight();
    			src = new MemoryImageSource(w, h, pixels = new int[w * h], 0, w);
    		}
    
    		public Dimension getPreferredSize() { return size; }
    		public void update(Graphics g) { paint(g); }
    
    		public void paint(Graphics g)
    		{
    			if (off == null) off = createImage(w, h);
    			if (prev != data && data != null)
    			{
    				processor.process((Graphics2D)off.getGraphics(), this);
    				prev = data;
    				data = null;
    			}
    			((Graphics2D)g).scale(getWidth() / w, getHeight() / h);
    			((Graphics2D)g).drawRenderedImage((RenderedImage)off, null);
    		}
    	}
    
    	Video video;
    	PushBufferStream stream;
    	Buffer buf = new Buffer();
    
    	Camera(Processor processor) throws Exception
    	{
    		CaptureDeviceInfo info = null;
    		Format format = null;
    
    		for (Object device : CaptureDeviceManager.getDeviceList(null))
    		{
    			info = (CaptureDeviceInfo)device;
    			for (Format f : info.getFormats())
    			{
    				format = f;
    				if (f instanceof VideoFormat) break;
    			}
    		}
    
    		/*
    		 * Use the first video device found @ 30 FPS.
    		 */
    
    		final DataSource src = Manager.createDataSource(info.getLocator());
    		((CaptureDevice)src).getFormatControls()[0].setFormat(
    			format.intersects(new VideoFormat(null, null, -1, null, 30)));
    		((PushBufferDataSource)src).getStreams()[0].setTransferHandler(this);
    
    		/*
    		 * Create the window and start the camera.
    		 */
    
    		Frame f = new Frame("Multitouch");
    		f.add(video = new Video(((RGBFormat)format).getSize(), processor));
    		f.pack();
    		f.setLocation(800, 600);
    		f.addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				try
    				{
    					src.stop();
    					src.disconnect();
    				}
    				catch (Exception ex) { System.err.println(ex); }
    				System.exit(0);
    			}
    		});
    		f.setVisible(true);
    		src.start();
    	}
    
    	/*
    	 * Called when a frame arrives.
    	 */
    	public void transferData(PushBufferStream stream)
    	{
    		try
    		{
    			video.busy = true;
    			stream.read(buf);
    			if (video.data == null)
    			{
    				video.data = (byte[])buf.getData();
    				if (buf.isDiscard())
    				{
    					video.data = null;
    				}
    				else
    				{
    					video.repaint();
    				}
    			}
    			else buf.setDiscard(true);
    		}
    		catch (Exception e) { System.err.println(e); }
    	}
    }

    ImageProxy.JAVA
    Code:
    /*
     *	Copyright Frank van Leeuwen. Free for personal and educational use.
     *	Please support me via PayPal.com (account [email protected]).
     *	You may alter the code if you leave this notice and share it on the Web.
     */
    
    import java.awt.*;
    
    class ImageProxy implements Camera.Processor
    {
    	Camera.Processor next;
    
    	ImageProxy(Camera.Processor next)
    	{
    		this.next = next;
    	}
    
    	public void process(Graphics2D g, Camera.Video video)
    	{
    		int j = 0;
    		int alpha = 255 << 24;
    
    		for (int i = 0; i < video.pixels.length; i++)
    		{
    			video.pixels[i] = alpha
    			+	( (int)video.data[j++] & 255)			// blue
    			+	(((int)video.data[j++] & 255) << 8)		// green
    			+	(((int)video.data[j++] & 255) << 16);	// red
    		}
    
    		g.drawImage(video.createImage(video.src), 0, 0, null);
    
    		if (next != null) next.process(g, video);
    	}
    }



    PaintBlobs.JAVA
    Code:
    /*
     *	Copyright Frank van Leeuwen. Free for personal and educational use.
     *	Please support me via PayPal.com (account [email protected]).
     *	You may alter the code if you leave this notice and share it on the Web.
     */
    
    import java.awt.*;
    import java.util.*;
    
    class PaintBlobs implements Camera.Processor
    {
    	Camera.Processor next;
    	Blob.Container blobs;
    
    	PaintBlobs(Camera.Processor next)
    	{
    		this.next = next;
    	}
    
    	public void process(Graphics2D g, Camera.Video video)
    	{
    		if (this.blobs == null) return;
    		Vector<Blob> blobs = this.blobs.getBlobs();
    		if (blobs == null) return;
    
    		g.setColor(new Color(0, 0, 255, 128));
    
    		for (Blob b : blobs) g.draw(b.getPoly());
    
    		g.setColor(new Color(0, 255, 0, 128));
    
    		for (Blob b : blobs) for (int j = 1; j < b.shape.length; j++)
    		{
    			g.draw(new Polygon(b.shape[j][0], b.shape[j][1], b.shape[j][0].length));
    		}
    
    		g.setColor(new Color(255, 255, 0, 128));
    
    		for (Blob b : blobs) g.draw(b.getPoly().getBounds());
    
    		if (next != null) next.process(g, video);
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width