Pergunta

I'm trying to set up face detection with JavaCV. I've got working code with cvLoadImage but when I try to load an image via Highgui.imread there's an error: 'Highgui cannot be resolved' and 'Highgui' has red wavy underlining. For some reason Eclipse cannot deal properly with imported com.googlecode.javacv.cpp.opencv_highgui or ...?

Problem here:

CvMat myImg = Highgui.imread(myFileName);

Full code:

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);  

    }   

}

External JARs: http://pasteboard.co/jwqNHC9.png

Full project in ZIP

I've also followed all steps from here: http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html

Any help will be appreciated.

Foi útil?

Solução

Mat m = Highgui.imread(myFileName); is from the builtin opencv java wrappers , not from javacv ( which is a independant, 3rd party wrapper ).

unfortunately, both concurrent apis are pretty incompatible, as javacv is wrapping the outdated c-api, and the opencv ones are wrapping the more modern c++ api.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top