1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
import java.awt.*;
import java.awt.event.*;
import java.awt.image.MemoryImageSource;
import hypermedia.video.*;
public class BlobDetection extends Frame implements Runnable {
// program execution frame rate (millisecond)
final int FRAME_RATE = 1000/30;
// threshold value for removing noises
final float THRESHOLD = 80f;
OpenCV cv = null; // OpenCV Object
Thread t = null; // the program thread
// the input video stream image
Image frame = null;
Blob[] blobs = null;
/**
* Setup Frame and Object(s).
*/
BlobDetection() {
super( "Blob Detection Sample" );
// OpenCV setup
cv = new OpenCV();
cv.capture( 640, 480 );
cv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
// frame setup
this.setBounds( 100, 100, cv.width, cv.height );
this.setBackground( Color.BLACK );
this.setVisible( true );
this.addKeyListener(
new KeyAdapter() {
public void keyReleased( KeyEvent e ) {
if ( e.getKeyCode()==KeyEvent.VK_ESCAPE ) { // ESC : release OpenCV resources
t = null;
cv.dispose();
System.exit(0);
}
if ( e.getKeyCode()==KeyEvent.VK_SPACE ) // SPACE : record background
cv.remember();
}
}
);
// start running program
t = new Thread( this );
t.start();
}
/**
* Release OpenCV resources.
*/
public void stop() {
t = null;
cv.dispose();
}
/**
* Draw video frame and each detected faces area.
*/
public void paint( Graphics g ) {
if ( frame==null || blobs==null ) return;
// draw image
//g.drawImage( frame, 0, 0, null );
// draw blobs
for( Blob b : blobs ) {
// define blob's contour
Polygon shape = new Polygon();
for( Point p : b.points ) shape.addPoint( p.x, p.y );
// fill blob
g.setColor( b.isHole ? Color.RED : Color.BLUE );
g.fillPolygon( shape );
}
}
/**
* Execute this sample.
*/
public void run() {
while( t!=null && cv!=null ) {
try {
t.sleep( FRAME_RATE );
// grab image from video stream
cv.read();
//cv.flip( OpenCV.FLIP_HORIZONTAL );
// create a new image from cv pixels data
MemoryImageSource mis = new MemoryImageSource( cv.width, cv.height, cv.pixels(), 0, cv.width );
frame = createImage( mis );
// prepare image for detection
cv.absDiff();
cv.threshold( THRESHOLD );
// detect blobs
blobs = cv.blobs( 30, cv.width*(cv.height/2), 100, true, OpenCV.MAX_VERTICES*4 );
// of course, repaint
repaint();
}
catch( InterruptedException e ) {;}
}
}
/**
* Main method.
* @param String[] list of arguments's user passed from the console to this program
*/
public static void main( String[] args ) {
System.out.println( "\nOpenCV blob detection sample" );
System.out.println( "PRESS SPACE BAR TO RECORD BACKGROUND IMAGE\n" );
new BlobDetection();
}
} |
Partager