Java and OpenCv
Posted: 13 November 2008 01:00 PM   [ Ignore ]
Rank
Joined  2008-11-13
Total Posts:  3
New Member

Hi, i’ve a problem to running open cv in java.
i downloaded all necessary following this guide: http://ubaa.net/shared/processing/opencv/
and under processing it works (but installing opencv 1.0 instead the latest version).
Now i want to use it in pure java environment, so i created a project under netbeans, i took the processing.core library, copied opencv.dll in OpenCv/bin directory and run the two examples with no error.
The problem is that i obtain a frame with black screen (webcam led is however on) and nothing is showed.

do you have any idea?

thanks in advance

Profile
 
 
Posted: 21 November 2008 11:33 PM   [ Ignore ]   [ # 1 ]
Rank
Joined  2007-02-15
Total Posts:  3
New Member

I ended up do a little poking around with the same stuff and ran into some of the same issues. I got it working by creating a separate class that implements java.awt.Panel then added that to the frame.

import hypermedia.video.OpenCV;
import java.awt.*;
import java.awt.image.MemoryImageSource;
import hypermedia.video.*;

public class 
ViewPanel extends Panel implements Runnable{
    
private OpenCV cv null;
    private 
Image img null;
    private 
Blob[] blobs null;
    private 
Thread t null;
    final 
float THRESHOLD 160f//Adjust this as needed
        
    
public ViewPanel() {
            super
();
            
= new Threadthis );
    
}

    
@Override
    
public void paint(Graphics g){
        
        
if (this.img != null){
            g
.drawImage(img00this);
            
            for( 
Blob b blobs {
                
// define blob's contour
                
Polygon shape = new Polygon();
                for( 
Point p b.points shape.addPointp.xp.);
                
                
g.setColorb.isHole Color.RED Color.BLUE );
                
g.fillPolygonshape );
            
}
        }
    }

    
@Override
    
public void run() {
        cv 
= new OpenCV();
        
cv.capture320240 );
        
cv.cascadeOpenCV.CASCADE_FRONTALFACE_ALT );
        
        while( 
t!=null && cv!=null {
            
            cv
.read();
            
cv.flipOpenCV.FLIP_HORIZONTAL );
            
            
MemoryImageSource mis = new MemoryImageSourcecv.widthcv.heightcv.pixels(), 0cv.width );
            
img createImagemis );
            
            
cv.absDiff();
            
cv.thresholdTHRESHOLD );
            
            
blobs cv.blobs30cv.width*(cv.height/2), 100trueOpenCV.MAX_VERTICES*);
            
            
repaint();
        
}
        
    }
    
    
public void stop() {
        t 
null;
        
cv.dispose();
    
}
    
public void start() {
        this
.t.start();
    
}
    
}

At this point you can treat it like any other awt component ...

import java.awt.*;
public class 
Test extends Frame {
    ViewPanel vp 
null;
    
Test(){
        super
("TEST FRAME");
        
        
this.vp = new ViewPanel();
        
this.vp.setBackground(Color.BLACK );
        
        
this.setBounds100100320240 );
        
this.add(this.vp);
        
this.setVisible(true);
        
        
this.vp.start();    
    
}
    
    
public static void main(String[] args{
        Test j 
= new Test();
    
}

}

Hope it helps.

Cheers
DosAge

Profile
 
 
Posted: 22 November 2008 08:36 AM   [ Ignore ]   [ # 2 ]
Rank
Joined  2008-11-13
Total Posts:  3
New Member

Hi and thanks for the answer.
In your way it works, but in the meantime i’ve founded the problem that isn’t in the type of Component used (Panel or Frame), but it’s in this line code:
cv.capture( 320, 240 );
that must be called inside the run() method and not in the constructor as the examples says.

Another problem that i have is a non perfect image representation with alternate black blocks similar to flickering problem, but if i exended JFrame instead Frame it’s ok.

Profile
 
 
Posted: 22 November 2008 02:47 PM   [ Ignore ]   [ # 3 ]
Rank
Joined  2007-02-15
Total Posts:  3
New Member

I have always attributed the flickering I get to attempting to run my camera at resolutions it doesn’t support.

As for the call to cv.capture(int,int) ,it works for me if called from the run method or from the constructor as shown in the examples.

Could you post a bit of code that didn’t work for you? It would be more out of curiosity now as we both seem to have a working solution.

Profile
 
 
Posted: 28 November 2008 03:51 PM   [ Ignore ]   [ # 4 ]
Rank
Joined  2008-11-13
Total Posts:  3
New Member

try to extends Component instead Panel… if we talk about the same flickering problem, you’ll must solve it.

problem about cv.capture(int, int) it’s simple: inside the run method it’s all ok, while in the constructor method didn’t refresh the image if i don’t move the mouse (i think it’s maybe a stupid thread problem, but i lost curiosity and left all code as it working rasberry)

Profile
 
 
Posted: 04 December 2008 08:35 AM   [ Ignore ]   [ # 5 ]
Avatar
Rank
Joined  2008-07-15
Total Posts:  63
New Member
Sliver - 28 November 2008 03:51 PM

try to extends Component instead Panel… if we talk about the same flickering problem, you’ll must solve it.

problem about cv.capture(int, int) it’s simple: inside the run method it’s all ok, while in the constructor method didn’t refresh the image if i don’t move the mouse (i think it’s maybe a stupid thread problem, but i lost curiosity and left all code as it working rasberry)

i might be wrong, but the flickering could be because frame is not double buffered by default.
You might use a frame, overload the paintcomponent method and paint it double-buffered.

Profile
 
 
Posted: 13 October 2009 12:22 PM   [ Ignore ]   [ # 6 ]
Rank
Joined  2009-10-13
Total Posts:  2
New Member

Hi,

I also see black frame when I use opencv in java although I did same way with you. please help.
Thanks.

Profile
 
 
Posted: 19 March 2010 09:24 AM   [ Ignore ]   [ # 7 ]
Rank
Joined  2010-03-19
Total Posts:  1
New Member

Black Screen Problem!!!

Hi all, im using the java wrapper for openCV under windows. I have successfully installed openCV v1.0 (i tested the newest versions and they dont work properly), i have set the variable system Path properly pointing to the “bin” directory of my openCV installation software, i have also pasted the openCV.dll into the bin directory, i have placed properly the “hypermedia” and “processing” folders to the appropriate locations, i have attached all the appropriate jar files needed by openCV, i have tested all these in XP and Vista..

Despite all these steps i did, all the examples provided by the openCV for the java version (face detection, BlobDetection, Aplets, etc) work properly without linking or runtime exceptions BUT I HAVE BLACK SCREEN TO ALL OF THEM!!!

What should i do more???
Even the face detection works, i see the bounding box tracking the movement of my head but i dont have image, just blank background colour.

Any suggestions? Please Help..

Profile