Ask Your Question
0

CascadeClassifier with haarcascade apparently not working

asked 2013-02-07 14:14:01 -0600

pietmau gravatar image

updated 2013-02-09 14:18:03 -0600

Hi there,

First of all thanks for any help !!!

I am trying to detect the nose on a face Bitmap.

For the purpose I use a CascadeClassifier with haarcascade.

Here is the haarcascade: link text

Here is the code:

            //Load image, convert into Bitmap, convert to grayscle
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            bm=toGrayscale(bm);

            // Initialize Mat and MatofRec
            mat = new Mat(); 
            rec = new MatOfRect();              

            //Load Bitmap into Map
            ut.bitmapToMat(bm, mat);
            mat.convertTo(mat2, CvType.CV_8U);
            Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

            //Create CascadeClassifier loading haarcascade from Android Resources
            CascadeClassifier cc= 
            new CascadeClassifier("android.resource://com.example.test/raw/haarcascade_mcs_nose.xml");

            //Call detectMultiScale
            cc.detectMultiScale(mat2,rec,1.1,2,2,new Size(400,400),new Size(400,400));
            outrec=rec.toArray();

But apparently something is not working.

The MatOfRect rec contains no Rect, indeed the Array outrec is empty: outrec.length=0.

What am I doing wrong?

Thanks a lot for any hint !!!

edit retag flag offensive close merge delete

Comments

2

If this topic is solved, then please remove SOLVED from the title and mark an answer (might be your own answer) as "accepted.

SR gravatar imageSR ( 2013-02-08 06:31:59 -0600 )edit

@SR "New users must wait 2 days before answering their own question. You can post an answer tomorrow" This is why was not able to answer my own question. I will clean everything tomorrow. Thanks

pietmau gravatar imagepietmau ( 2013-02-08 10:42:01 -0600 )edit

2 answers

Sort by » oldest newest most voted
1

answered 2013-02-07 15:30:34 -0600

berak gravatar image

updated 2013-02-07 16:14:27 -0600

my guess(more a c++ guy, no idea about andoid ):

mat.convertTo(mat2, CvType.CV_8U); // this is probably not doing, what you want it to, as it's not changing the number of channels in the output img, so, mat2 might still have the original number of channels(4, most likely)

imho, you've got to pass a grayscale(1 channel,8bit) image to detectMultiScale in c++, i'd use

cvtColor(src,dst, CV_RGB2GRAY);

to achieve that, but no idea about the android wrapper here

edit flag offensive delete link more

Comments

@berak Thnaks, but did not work; used Imgproc.cvtColor(mat, mat2, Imgproc.COLOR_RGBA2GRAY in Java. Thanks Anyway!

pietmau gravatar imagepietmau ( 2013-02-08 00:07:39 -0600 )edit
0

answered 2013-02-09 14:16:59 -0600

pietmau gravatar image

ISSUE SOLVED.

what I was doing wrong was loading the HaarCascade in the CascadeClassifier.

I changed it accordingly with FaceDetection sample application on OpenCV4Android SDK.

Here is the updated code:

        //Load image, convert into Bitmap, convert to grayscle
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.face, op);
        Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        bm=toGrayscale(bm);             

        mat = new Mat(); 
        Mat mat2 = new Mat(); 
        rec = new MatOfRect();              
        //Load Bitmap into Map
        ut.bitmapToMat(bm, mat);
        //mat.convertTo(mat2, CvType.CV_8UC1);
        //cvtColor(mat,mat2,CV_RGB2GRAY);
        Imgproc.cvtColor(mat, mat2, Imgproc.COLOR_RGBA2GRAY);
        Log.e("","mat è " +Integer.toString(mat.width())+" "+ Integer.toString(mat.height())+" "+ Integer.toString(mat.type()));

        bmp = Bitmap.createBitmap(480, 600, conf);
        ut.matToBitmap(mat2,bmp);
        iv.setImageBitmap(bmp);
        //Create CascadeClassifier loading haarcascade from Android Resources


        try {
        InputStream is = getResources().openRawResource(R.raw.haarcascade_mcs_nose);
        File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
        File mCascadeFile = new File(cascadeDir, "haarcascade_mcs_nose.xml");
        FileOutputStream os = new FileOutputStream(mCascadeFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        is.close();
        os.close();

        mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
        if (mJavaDetector.empty()) {
            Log.e(TAG, "Failed to load cascade classifier");
            mJavaDetector = null;
        } else
            Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());


        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
        }

        //Call detectMultiScale
        mJavaDetector.detectMultiScale(mat2,rec,1.1,2,2,new Size(10,10),new Size(400,400));
        outrec=rec.toArray();
        Log.e("","Contiene rettangoli n =" + Integer.toString(outrec.length));
        Log.e(TAG, " funziona!!!");
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-02-07 14:14:01 -0600

Seen: 7,029 times

Last updated: Feb 09 '13