0

I am newly working with openCV in java on eclipse and am working on an eye tracking software, i am using a base code some one else has created and plan to tweek it but am having an error with a few lines of code and can not figure out why. here is the entire class

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

the error starts here with error code : "The constructor Size(double, double, int, int, int, Scalar) is undefined"

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

then here at ellipse i get an error of : "The method ellipse(Mat, RotatedRect, Scalar, int, int) in the type Core is not applicable for the arguments (Mat, Point, Size, int, int, int)

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

}
public class Eyes {

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 

}

any and all help would be greatly appreciated

1 Answer 1

1

If you have a look at the OpenCV Java API you can see the defined constructors for Size and what parameters are required:

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

A size holds two values, height and width.

So your code:

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

should be:

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

this creates a size holding half the rectangle width and half the rectangle height.


And again for the Ellipse methods:

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

There are many variations but it looks like you want to be using the following:

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

So your code:

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

is likely just missing the colour scalar:

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

Where B,G,R are doubles for each colour channel.

Sign up to request clarification or add additional context in comments.

7 Comments

ok so I changed my code to what you suggested and it fixed the size error but caused and error with B, G, and R stating B, G, or R cannot be resolved to a variable
You need to substitute B,G,R with values new Scalar(0,0,255) would be red for example.
ok i have completed this and have no errors in the code its self but am getting an error when i run the class. at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at have.Main.main(Main.java:16)
Sounds like a problem importing the OpenCV library, there's a tutorial here about setting everything up in Eclipse.
Thank you very much, this is going to save my tail hahahaha.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.