使用带有大小的Java OpenCV库时出错

时间:2015-04-10 02:57:14

标签: java opencv

我正在eclipse上使用java中的openCV,我正在使用眼动追踪软件,我正在使用其他人创建的基本代码并计划调整它,但是我遇到了几行代码的错误无法弄清楚为什么。这是整个班级

package have;

import java.awt.*;  

import java.awt.image.BufferedImage;  
import java.io.ByteArrayInputStream;  
import java.io.IOException;  
import javax.imageio.ImageIO;  
import javax.swing.*;  
import org.opencv.core.Core;  
import org.opencv.core.Mat;  
import org.opencv.core.MatOfByte;  
import org.opencv.core.MatOfRect;  
import org.opencv.core.Point;  
import org.opencv.core.Rect;  
import org.opencv.core.Scalar;  
import org.opencv.core.Size;  
import org.opencv.highgui.Highgui;  
import org.opencv.highgui.VideoCapture;  
import org.opencv.imgproc.Imgproc;  
import org.opencv.objdetect.CascadeClassifier;  

class FacePanel extends JPanel{  
     private static final long serialVersionUID = 1L;  
     private BufferedImage image;  
     // Create a constructor method  
     public FacePanel(){  
          super();   
     }  
     /*  
      * Converts/writes a Mat into a BufferedImage.  
       *   
      * @param matrix Mat of type CV_8UC3 or CV_8UC1  
      * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY  
      */       
     public boolean matToBufferedImage(Mat matrix) {  
          MatOfByte mb=new MatOfByte();  
          Highgui.imencode(".jpg", matrix, mb);  
          try {  
               this.image = ImageIO.read(new      ByteArrayInputStream(mb.toArray()));  
           } catch (IOException e) {  
               e.printStackTrace();  
               return false; // Error  
          }  
       return true; // Successful  
     }  
     public void paintComponent(Graphics g){  
          super.paintComponent(g);   
          if (this.image==null) return;         
            g.drawImage(this.image,10,10,this.image.getWidth(),this.image.getHeight(),     null);
      }

}  
class FaceDetector {  
     private CascadeClassifier face_cascade;  
     // Create a constructor method  
     public FaceDetector(){  
         // face_cascade=new      CascadeClassifier("./cascades/lbpcascade_frontalface_alt.xml");  
         //..didn't have not much luck with the lbp

        face_cascade=new      CascadeClassifier("./cascades/haarcascade_frontalface_alt.xml"); 
          if(face_cascade.empty())  
          {  
               System.out.println("--(!)Error loading A\n");  
                return;  
          }  
          else  
          {  
                     System.out.println("Face classifier loooaaaaaded up");  
          }  
     }  
     public Mat detect(Mat inputframe){  
           Mat mRgba=new Mat();  
           Mat mGrey=new Mat();  
      MatOfRect faces = new MatOfRect();  
      inputframe.copyTo(mRgba);  
      inputframe.copyTo(mGrey);  
      Imgproc.cvtColor( mRgba, mGrey, Imgproc.COLOR_BGR2GRAY);  
      Imgproc.equalizeHist( mGrey, mGrey );  
      face_cascade.detectMultiScale(mGrey, faces);  
      System.out.println(String.format("Detected %s faces", faces.toArray().length));  
      for(Rect rect:faces.toArray())  
      {  
          Point center= new Point(rect.x + rect.width*0.5, rect.y + rect.height*0.5 );  
          //draw a blue eclipse around face

错误从错误代码开始:"构造函数Size(double,double,int,int,int,Scalar)未定义"

          Size s = new Size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new Scalar( 0, 0, 255 )

然后在椭圆处我得到一个错误:" Core类型中的方法ellipse(Mat,RotatedRect,Scalar,int,int)不适用于参数(Mat,Point,Size,int, int,int)

           Core.ellipse( mRgba, center,s , 4, 8, 0 );  
      }  
      return mRgba;  
 }  

}
    公共课眼睛{

public static void main(String arg[]) throws InterruptedException{  
  // Load the native library.  
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 
  //or ...     System.loadLibrary("opencv_java244");       

  //make the JFrame
  JFrame frame = new JFrame("WebCam Capture - Face detection");  
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

  FaceDetector faceDetector=new FaceDetector();  
  FacePanel facePanel = new FacePanel();  
  frame.setSize(400,400); //give the frame some arbitrary size 
  frame.setBackground(Color.BLUE);
  frame.add(facePanel,BorderLayout.CENTER);       
  frame.setVisible(true);       

  //Open and Read from the video stream  
   Mat webcam_image=new Mat();  
   VideoCapture webCam =new VideoCapture(0);   

    if( webCam.isOpened())  
      {  
       Thread.sleep(500); /// This one-time delay allows the Webcam to initialize itself  
       while( true )  
       {  
         webCam.read(webcam_image);  
         if( !webcam_image.empty() )  
          {   
              Thread.sleep(200); /// This delay eases the computational load .. with little performance leakage
               frame.setSize(webcam_image.width()+40,webcam_image.height()+60);  
               //Apply the classifier to the captured image  
               webcam_image=faceDetector.detect(webcam_image);  
              //Display the image  
               facePanel.matToBufferedImage(webcam_image);  
               facePanel.repaint();   
          }  
          else  
          {   
               System.out.println(" --(!) No captured frame from webcam !");   
               break;   
          }  
         }  
        }
       webCam.release(); //release the webcam

  } //end main 

}

任何和所有帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

如果您查看OpenCV Java API,您可以看到为Size定义的构造函数以及所需的参数:

http://docs.opencv.org/java/org/opencv/core/Size.html

大小包含两个值,高度和宽度。

所以你的代码:

 Size s = new Size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new Scalar( 0, 0, 255 )

应该是:

Size s = new Size( rect.width*0.5, rect.height*0.5);

这会创建一个大小,保持矩形宽度的一半和矩形高度的一半。


再次使用Ellipse方法:

http://docs.opencv.org/java/org/opencv/core/Core.html

有许多变化,但看起来您希望使用以下内容:

public static void ellipse(Mat img,
       Point center,
       Size axes,
       double angle,
       double startAngle,
       double endAngle,
       Scalar color)

所以你的代码:

Core.ellipse( mRgba, center,s , 4, 8, 0 ); 

可能只是缺少颜色标量:

 Core.ellipse( mRgba, center,s , 4, 8, 0, new Scalar(B,G,R));

其中B,G,R是每个颜色通道的双倍。