Problem capturing webcam OpenCV
in
Contributed Library Questions
•
2 years ago
Ola I'm using OpenCV 1.0 for Java on Windows 7. I'm having trouble capturing videofrom webcam, put on the line where opencv.capture (640.480), he returns the following error "Error while starting capture: 0 device." I have already installed all the files .dll, and put the jar in my bibleiotecas eclipse. Do you have any idea what might be happening? Note I'm using the example file for java OpenCV Blob Detectiton sample
<code>
/**
* (./) BlobDetection.java, 03/05/08
* (by) cousot stephane @
http://www.ubaa.net/
* (cc) some right reserved
*
* Sample file for the "OpenCV" project.
* Use ESC key to close the program properly.
* Press SPACE BAR to record background image.
*
* This sample is released under a Creative Commons Attribution 3.0 License
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.MemoryImageSource;
import hypermedia.video.*;
public class test 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).
*/
test() {
super( "Blob Detection Sample" );
// OpenCV setup
cv = new OpenCV();
System.out.println("Antes do capture 640 480");
cv.capture( 640, 480 );
System.out.println("Passou pela 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 ) {System.out.println("Erro mortal");}
}
}
/**
* 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 test();
}
}
</code>
1