RLPark 1.0.0
Reinforcement Learning Framework in Java

OpenCVImageProvider.java

Go to the documentation of this file.
00001 package rlpark.plugin.opencv.zephyr;
00002 
00003 import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
00004 
00005 import java.awt.image.BufferedImage;
00006 
00007 import rlpark.plugin.opencv.ImageBuffer;
00008 import rlpark.plugin.opencv.OpenCVUtils;
00009 import zephyr.plugin.core.api.viewable.ImageProvider;
00010 
00011 import com.googlecode.javacv.cpp.opencv_core;
00012 import com.googlecode.javacv.cpp.opencv_core.IplImage;
00013 import com.googlecode.javacv.cpp.opencv_imgproc;
00014 
00015 public class OpenCVImageProvider implements ImageProvider {
00016   private IplImage frame;
00017   private IplImage depthBuffer;
00018   private IplImage channelBuffer;
00019 
00020   @Override
00021   public BufferedImage image() {
00022     if (frame == null)
00023       return null;
00024     IplImage depthConverted = convertDepthIFN(frame);
00025     IplImage channelConverted = convertChannelIFN(depthConverted);
00026     return channelConverted.getBufferedImage();
00027   }
00028 
00029   private IplImage convertChannelIFN(IplImage src) {
00030     if (src.nChannels() == 3)
00031       return src;
00032     if (channelBuffer == null)
00033       channelBuffer = IplImage.create(src.width(), src.height(), src.depth(), 3);
00034     opencv_imgproc.cvCvtColor(src, channelBuffer, opencv_imgproc.CV_GRAY2RGB);
00035     return channelBuffer;
00036   }
00037 
00038   private IplImage convertDepthIFN(IplImage src) {
00039     if (src.depth() == opencv_core.IPL_DEPTH_8U)
00040       return src;
00041     if (depthBuffer == null)
00042       depthBuffer = IplImage.create(src.width(), src.height(), IPL_DEPTH_8U, src.nChannels());
00043     OpenCVUtils.convertScale(src, depthBuffer);
00044     return depthBuffer;
00045   }
00046 
00047   public void update(IplImage currentFrame) {
00048     this.frame = currentFrame;
00049   }
00050 
00051   public void dispose() {
00052     if (depthBuffer != null)
00053       depthBuffer.release();
00054     if (channelBuffer != null)
00055       depthBuffer.release();
00056   }
00057 
00058   public void update(ImageBuffer currentFrame) {
00059     update(currentFrame.im());
00060   }
00061 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark