Error using Java OpenCV library with size -
i newly working opencv in java on eclipse , working on eye tracking software, using base code 1 else has created , plan tweek having error few lines of code , can not figure out why. here 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 constructor method public facepanel(){ super(); } /* * converts/writes mat 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 constructor method public facedetector(){ // face_cascade=new cascadeclassifier("./cascades/lbpcascade_frontalface_alt.xml"); //..didn't have not luck 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 blue eclipse around face
the error starts here error code : "the constructor size(double, double, int, int, int, scalar) undefined"
size s = new size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new scalar( 0, 0, 255 )
then here @ ellipse error of : "the method ellipse(mat, rotatedrect, scalar, int, int) in type core not applicable 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 native library. system.loadlibrary(core.native_library_name); //or ... system.loadlibrary("opencv_java244"); //make 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 frame arbitrary size frame.setbackground(color.blue); frame.add(facepanel,borderlayout.center); frame.setvisible(true); //open , read video stream mat webcam_image=new mat(); videocapture webcam =new videocapture(0); if( webcam.isopened()) { thread.sleep(500); /// one-time delay allows webcam initialize while( true ) { webcam.read(webcam_image); if( !webcam_image.empty() ) { thread.sleep(200); /// delay eases computational load .. little performance leakage frame.setsize(webcam_image.width()+40,webcam_image.height()+60); //apply classifier captured image webcam_image=facedetector.detect(webcam_image); //display image facepanel.mattobufferedimage(webcam_image); facepanel.repaint(); } else { system.out.println(" --(!) no captured frame webcam !"); break; } } } webcam.release(); //release webcam } //end main }
any , appreciated
if have @ opencv java api can see defined constructors size , parameters required:
http://docs.opencv.org/java/org/opencv/core/size.html
a size holds 2 values, height , width.
so 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 size holding half rectangle width , half rectangle height.
and again ellipse methods:
http://docs.opencv.org/java/org/opencv/core/core.html
there many variations looks want using following:
public static void ellipse(mat img, point center, size axes, double angle, double startangle, double endangle, scalar color)
so code:
core.ellipse( mrgba, center,s , 4, 8, 0 );
is missing colour scalar:
core.ellipse( mrgba, center,s , 4, 8, 0, new scalar(b,g,r));
where b,g,r doubles each colour channel.