*banner
 
 

Notes on Using OpenCV and Reactivision in Ptolemy II

Installing OpenCV

See http://ubaa.net/shared/processing/opencv/ for details.

Installing OpenCV under Mac OS X

  1. Download http://ubaa.net/shared/processing/opencv/download/opencv-framework.1.1.dmg
  2. Install opencv-framework.1.1.dmg, which will create /Library/Frameworks/OpenCV.framework/
  3. Download and unziphttp://ubaa.net/shared/processin g/opencv/download/opencv_01.zip
  4. Copy the .jar and .jnilib to /System/Library/Java/Extensions:
        sudo cp ~/Downloads/OpenCV/library/OpenCV.jar /System/Library/Java/Extensions/
        sudo cp ~/Downloads/OpenCV/library/libOpenCV.jnilib /System/Library/Java/Extensions/
     

Note that under Eclipse, you may need to add OpenCV.jar to your classpath.

OpenCV and Ptolemy

See http://chess.eecs.berkeley.edu/ptexternal for information about getting started with Ptolemy.

A version of the actor below has been installed as $PTII/ptolemy/actor/lib/opencv/OpenCVReader.java

There is a demo at $PTII/ptolemy/actor/lib/opencv/demo/OpenCVReader/OpenCVReader.xml

See $PTII/ptolemy/actor/lib/opencv/package.html for detailed instructions about how to set up.

If you install and you add the jar file to your build path in Eclipse, then the following actor, put (inappropriately) in $PTII will run in this model.

import hypermedia.video.OpenCV;

import java.awt.Frame;
import java.awt.Image;
import java.awt.image.MemoryImageSource;

import ptolemy.actor.TypedAtomicActor;
import ptolemy.actor.TypedIOPort;
import ptolemy.data.AWTImageToken;
import ptolemy.data.type.BaseType;
import ptolemy.kernel.CompositeEntity;
import ptolemy.kernel.util.IllegalActionException;
import ptolemy.kernel.util.NameDuplicationException;


public class OpenCVHelloWorld extends TypedAtomicActor {

  public OpenCVHelloWorld(CompositeEntity container, String name)
          throws IllegalActionException, NameDuplicationException {
      super(container, name);
      output = new TypedIOPort(this, "output", false, true);
      output.setTypeEquals(BaseType.OBJECT);
      _dummyFrame = new Frame();
  }

  public TypedIOPort output;
  
  public void fire() throws IllegalActionException {
      _openCV.read();
      // create a new image from cv pixels data
      MemoryImageSource mis = new MemoryImageSource( _openCV.width, _openCV.height, _openCV.pixels(), 0, _openCV.width );
      Image image = _dummyFrame.createImage( mis );

      output.send(0, new AWTImageToken(image));
  }
  
  public void initialize() throws IllegalActionException {
      super.initialize();
      _openCV = new OpenCV();
      _openCV.capture( 640, 480 );
      _openCV.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
  }
  
  private OpenCV _openCV;
  private Frame _dummyFrame;
}

Dai's Interface to OpenCV

Dai's OpenCV interface to the C API is illustrated in $PTII/ptolemy/codegen/demo/OpenCV. However, using the Java API may be far more convenient in the long run.
©2002-2018 Chess