import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.util.Arrays; import blobDetection.*; /* * blobview.java is intended to demonstrate the use of blobDetection.java using a simple Java program. * This program takes one argument, which is a .jpg file. It displays on the screen the .jpg file * with the detected blobs outlined in RED. If your image does not produce blobs in the desired * locations, then tweak the value passed to the setThreshold() method. * * You can also tweak the self documenting values hard coded in the newBlobDetectedEvent() method. * * This code is provided free and without warranty. You are free to use it anyway you like. The * author, michael@potter.name, asks you to submit bugs and suggestions. * */ public class blobview extends Frame { /** * */ private static final long serialVersionUID = 1L; /** * @param args */ private Image image; private BufferedImage Bimage; public blobview(String fileName) { BlobDetection bd; int h; int w; Toolkit toolkit = Toolkit.getDefaultToolkit(); image = toolkit.getImage(fileName); MediaTracker mediaTracker = new MediaTracker(this); mediaTracker.addImage(image, 0); try { mediaTracker.waitForID(0); } catch (InterruptedException ie) { System.err.println(ie); System.exit(1); } h = image.getHeight(null); w = image.getWidth(null); Bimage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D G2d = Bimage.createGraphics(); G2d.drawImage(image, 0, 0, w, h, null); bd = new BlobDetection(w, h); bd.activeCustomFilter(this); bd.setThreshold(0.5f); bd.setPosDiscrimination(false); bd.computeBlobs(Bimage.getRGB(0, 0, w, h, null, 0, w)); for (int i=0; i < bd.getBlobNb(); i++) { Blob b; b = bd.getBlob(i); drawBlob(G2d, b, h, w); } addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setSize(w, h); setTitle(fileName); setVisible(true); } public boolean newBlobDetectedEvent(Blob b) { int sizePix; int h = b.parent.imgHeight; int w = b.parent.imgWidth; sizePix = calculateSizePix(b, h, w); if (b.getEdgeNb() > 400) { System.out.println("Too many edges: n=" + b.getEdgeNb() + " x=" + b.x + " y=" + b.y + " v=" + sizePix); return(false); } if (sizePix < 50) { System.out.println("Too small: n=" + b.getEdgeNb() + " x=" + b.x + " y=" + b.y + " v=" + sizePix); return(false); } if (sizePix > 500) { System.out.println("Too large: n=" + b.getEdgeNb() + " x=" + b.x + " y=" + b.y + " v=" + sizePix); return(false); } if (b.w*w > 30) { System.out.println("Too wide: n=" + b.getEdgeNb() + " x=" + b.x + " y=" + b.y + " v=" + sizePix); return(false); } if (b.w*w < 10) { System.out.println("Too narrow: n=" + b.getEdgeNb() + " x=" + b.x + " y=" + b.y + " v=" + sizePix); return(false); } if (b.h*h > 30) { System.out.println("Too tall: n=" + b.getEdgeNb() + " x=" + b.x + " y=" + b.y + " v=" + sizePix); return(false); } if (b.h*h < 10) { System.out.println("Too short: n=" + b.getEdgeNb() + " x=" + b.x + " y=" + b.y + " v=" + sizePix); return(false); } if (b.h/b.w < 0.5f || b.h/b.w > 2.0f) { System.out.println("Too oblong: n=" + b.getEdgeNb() + " x=" + b.x + " y=" + b.y + " v=" + sizePix); return(false); } System.out.println("Blob: n=" + b.getEdgeNb() + " x=" + b.x + " y=" + b.y + " v=" + sizePix); return(true); } private int calculateSizePix(Blob b, int h, int w) { EdgeVertex eA, eB; int sizePix; float sizePercent; sizePercent = 0.0f; /* This loop implements Green's Theorem. It ignores the B vertex and just uses the previous A vertex * as the starting point. I found that to be a better algorithm because the B of one edge does not * always equal the A of the next edge. Specifically the last vertex. */ eA = b.getEdgeVertexA(0); for (int i=1; i