OpenCV-2.4.4 + javacv-0.4 +eclipse

来源:互联网 发布:淘宝店二维码怎么生成 编辑:程序博客网 时间:2024/06/05 06:08

由于最近实验室需要处理图像,本人对java相对来说比较熟悉,所以配置了Java的图像开发环境。如下:

1. 首先下载OpenCV-2.4.4和javacv-0.4。

OpenCV-2.4.4下载地址:http://www.opencv.org.cn/index.php/Download

javacv-0.4 下载地址:https://code.google.com/p/javacv/downloads/detail?name=javacv-0.4-bin.zip

2. 安装OpenCV-2.4.4到某一目录下(我安装到了D:\Program Files\opencv),配置环境变量,计算机->属性->高级系统设置->环境变量,在系统变量里找到path然后在里面添加

;D:\Program Files\opencv\build\x86\vc10\bin,新建系统变量OpenCV添加D:\Program Files\opencv\build。

3. 打开eclipse,新建java工程文件,例如:名字为myOpenCV,然后finish。

4. 右击工程文件myOpenCV,new->folder名字为libs,然后将解压缩的javacv-0.4中的所有jar包都复制到libs里面.

5. 右击工程myOpenCV,选择Build Path->Configuer Build Path,选中Libraries选项卡,单击Add JARs...,添加刚才复制到libs里面的jar包。

6. 新建Class,名字为Test.java,粘贴如下代码:

import java.awt.Graphics2D;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.Timer;import com.googlecode.javacv.CanvasFrame;import com.googlecode.javacv.OpenCVFrameGrabber;import com.googlecode.javacv.cpp.opencv_core.IplImage;import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;/** *  * Use JavaCV/OpenCV to capture camera images *  * There are two functions in this demo: * 1) show real-time camera images  * 2) capture camera images by mouse-clicking anywhere in the JFrame,  * the jpg file is saved in a hard-coded path.  *  * @author ljs * 2011-08-19 * */public class Test {public static String savedImageFile = "c:\\tmp\\my.jpg";//timer for image capture animationstatic class TimerAction implements ActionListener {private Graphics2D g;private CanvasFrame canvasFrame;private int width,height;private int delta=10;private int count = 0;private Timer timer;public void setTimer(Timer timer){this.timer = timer;} public TimerAction(CanvasFrame canvasFrame){this.g = (Graphics2D)canvasFrame.getCanvas().getGraphics();this.canvasFrame = canvasFrame;this.width = canvasFrame.getCanvas().getWidth();this.height = canvasFrame.getCanvas().getHeight();}        public void actionPerformed(ActionEvent e) {        int offset = delta*count;        if(width-offset>=offset && height-offset >= offset) {                g.drawRect(offset, offset, width-2*offset, height-2*offset);        canvasFrame.repaint();                count++;        }else{        //when animation is done, reset count and stop timer.        timer.stop();        count = 0;        }        }    }public static void main(String[] args) throws Exception {//open camera sourceOpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);grabber.start();//create a frame for real-time image displayCanvasFrame canvasFrame = new CanvasFrame("Camera");IplImage image = grabber.grab();int width = image.width();int height = image.height();        canvasFrame.setCanvasSize(width, height);                //onscreen buffer for image capture        final BufferedImage bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        Graphics2D bGraphics = bImage.createGraphics();                     //animation timer        TimerAction timerAction = new TimerAction(canvasFrame);final Timer timer=new Timer(10, timerAction);timerAction.setTimer(timer); //click the frame to capture an image        canvasFrame.getCanvas().addMouseListener(new MouseAdapter(){        public void mouseClicked(MouseEvent e){             timer.start(); //start animation        try {ImageIO.write(bImage, "jpg", new File(savedImageFile));} catch (IOException e1) {e1.printStackTrace();}                    }                     });                //real-time image display        while(canvasFrame.isVisible() && (image=grabber.grab()) != null){        if(!timer.isRunning()) { //when animation is on, pause real-time display        canvasFrame.showImage(image);        //draw the onscreen image simutaneously        bGraphics.drawImage(image.getBufferedImage(),null,0,0);          }        }                //release resources        cvReleaseImage(image);         grabber.stop();        canvasFrame.dispose();}}
7. 会打开摄像头,预览图像。

8. 在C盘下建立tmp文件夹,单击屏幕,保存图片到tmp里。

另外:如果系统是64位的,出现opencv_core244.dll: %1 不是有效的 Win32 应用程序错误的原因是:

没有安装运行时环境,Microsoft Visual C++ 2010 Redistributable Package (x64)