'Highgui无法解决',JavaCV,Eclipse

时间:2014-01-30 19:18:20

标签: java eclipse opencv javacv face-detection

我正在尝试使用JavaCV设置面部检测。我已经使用 cvLoadImage 编写了代码但当我尝试通过 Highgui.imread 加载图像时出现错误:'Highgui无法解析'和'Highgui '有红色波浪下划线。由于某种原因,Eclipse无法正确处理导入的com.googlecode.javacv.cpp.opencv_highgui或......?

问题在这里:

CvMat myImg = Highgui.imread(myFileName);

完整代码:

import java.awt.EventQueue;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;
import java.awt.Button;
import java.io.File;

import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.googlecode.javacv.cpp.opencv_core.CvMat;
import com.googlecode.javacv.cpp.opencv_highgui;
//import opencv_highgui;


public class Form1 {

    static private final String newline = "\n";
    JButton openButton, saveButton;
    JTextArea log;
    JFileChooser fc;

    String myFileName = "";

    //Load haar classifier XML file
    public static final String XML_FILE = 
            "resources/!--master--haarcascade_frontalface_alt_tree.xml";    

    private JFrame frame;

    //Detect for face using classifier XML file 
    public static void detect(IplImage src){

        //Define classifier 
        CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad(XML_FILE));

        CvMemStorage storage = CvMemStorage.create();

        //Detect objects
        CvSeq sign = cvHaarDetectObjects(
                src,
                cascade,
                storage,
                1.1,
                3,
                0);

        cvClearMemStorage(storage);

        int total_Faces = sign.total();     

        //Draw rectangles around detected objects
        for(int i = 0; i < total_Faces; i++){
            CvRect r = new CvRect(cvGetSeqElem(sign, i));
            cvRectangle (
                    src,
                    cvPoint(r.x(), r.y()),
                    cvPoint(r.width() + r.x(), r.height() + r.y()),
                    CvScalar.RED,
                    2,
                    CV_AA,
                    0);

        }

        //Display result
        cvShowImage("Result", src);
        cvWaitKey(0);

    }               

    /**
     * Create the application.
     */
    public Form1() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */

    JLabel Label1 = new JLabel(" ");        

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 301, 222);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton btnDetect = new JButton("Detect");
        btnDetect.setVerticalAlignment(SwingConstants.TOP);
        btnDetect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //IplImage img = cvLoadImage("resources/lena.jpg");
                IplImage img = cvLoadImage(myFileName);
                CvMat myImg = Highgui.imread(myFileName);
                detect(img);                    
            }
        });
        frame.getContentPane().add(btnDetect, BorderLayout.SOUTH);


        Label1.setHorizontalAlignment(SwingConstants.CENTER);
        frame.getContentPane().add(Label1, BorderLayout.CENTER);        

        JButton btnNewButton = new JButton("Open");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fileopen = new JFileChooser();            
                int ret = fileopen.showDialog(null, "Открыть файл");
                if (ret == JFileChooser.APPROVE_OPTION) {
                    File file = fileopen.getSelectedFile();
                    myFileName = file.getAbsolutePath();
                    Label1.setText(myFileName);
                }
            }
        });
        frame.getContentPane().add(btnNewButton, BorderLayout.NORTH);



    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Form1 window = new Form1();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //Load image
        //IplImage img = cvLoadImage("resources/lena.jpg");     
        //detect(img);  

    }   

}

外部JAR:http://pasteboard.co/jwqNHC9.png

Full project in ZIP

我也遵循了这里的所有步骤:http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

Mat m = Highgui.imread(myFileName);来自builtin opencv java wrappers,而不是来自javacv(这是一个独立的第三方包装器)。

不幸的是,两个并发api都非常不兼容,因为javacv正在包装过时的c-api,而opencv正在包装更现代的c ++ api。