2014-01-30 140 views
0

我試圖用JavaCV設置人臉檢測。我有cvLoadImage但當我嘗試通過Highgui.imread加載圖像時出現錯誤:'Highgui無法解析'和'Highgui'具有紅色波浪下劃線。由於某些原因,Eclipse無法正確處理導入的com.googlecode.javacv.cpp.opencv_highgui或...?'Highgui can not be resolved',JavaCV,Eclipse

問題就在這裏:

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

任何幫助將不勝感激。

+0

同樣的問題的解決方案可以在另一個[stackoverflow線程]中找到(http://stackoverflow.com/questions/30296626/opencv-import-highgui-in-android-studio-is-showing-error-cannot-resolvei -添加) – snAtchnAren

回答

1

Mat m = Highgui.imread(myFileName);來自builtin opencv java wrappers,而不是來自javacv(這是一個獨立的第三方包裝器)。

不幸的是,兩個併發apis都非常不兼容,因爲javacv正在包裝過時的c-api,而opencv正在包裝更現代的C++ api。

相關問題