简体   繁体   中英

Questions about JavaCV example

HI guys I am experimenting with the use of JavaCV as I want to get to know how it works in order to include it's functionality in a project I have in mind. I have downloaded and set up OpenCV just like what the instructions said and I also have downloaded form bytedeco the JavaCV 1.0 jars that I need to include in my project.

I have started with an example program I found online that basically grabs and save images from a webcam. The code I have written is the following:

import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.VideoInputFrameGrabber;

import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.*;

public class GrabberShow implements Runnable{

    IplImage image;
    CanvasFrame canvas = new CanvasFrame("Web Cam");

    public GrabberShow(){
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    }

    public void run(){
        FrameGrabber grabber = new VideoInputFrameGrabber(0);
        int i = 0;
        try{
            grabber.start();
            IplImage img;
            while(true){
                img = grabber.grab();
                if(img != null){
                    cvFlip(img, img, 1);
                    cvSaveImage((i++) + "-aa.img", img);
                    canvas.showImage(img);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args){
        GrabberShow gs = new GrabberShow();
        Thread th = new Thread(gs);
        th.start();
    }
}

This is a very straightforward and easy example. The problem I am experiencing can be found on the following lines:

img = grabber.grab();

and

canvas.showImage(img);

The problem I face is a Type Mismatch "Cannot convert from Frame to opencv_core.LplImage".

I have tried searching for this online but I was unable to locate a good answer about this. What I did found was the same example only. Does anyone have any ideas about this?

Need to stress out that this the first time I use openCV with Java. I have used it in the past to make and object tracking program but this was done with native openCV and using Python.

The code you have looks like it would probably work for javacv version 0.10. For 1.0 the FrameGrabber 's return Frame objects that then need to be converted to IplImage objects using a OpenCVFrameConverter.ToIplImage .

import org.bytedeco.javacpp.opencv_core.IplImage;
import static org.bytedeco.javacpp.opencv_core.cvFlip;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvSaveImage;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.VideoInputFrameGrabber;

...

public class GrabberShow implements Runnable {

    CanvasFrame canvas = new CanvasFrame("Web Cam");

    public GrabberShow() {
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    }

    public void run() {
        FrameGrabber grabber = new VideoInputFrameGrabber(0);
        OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();

        int i = 0;
        try {
            grabber.start();
            IplImage img;
            while (true) {
                Frame frame = grabber.grab();
                img = converter.convert(frame);
                if (img != null) {
                    cvFlip(img, img, 1);
                    cvSaveImage((i++) + "-aa.img", img);
                    canvas.showImage(frame);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        GrabberShow gs = new GrabberShow();
        Thread th = new Thread(gs);
        th.start();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM