Ask Your Question

pietmau's profile - activity

2018-10-15 10:01:33 -0500 received badge  Famous Question (source)
2018-08-16 22:03:45 -0500 received badge  Popular Question (source)
2017-06-29 11:25:54 -0500 received badge  Notable Question (source)
2016-04-15 07:44:51 -0500 received badge  Popular Question (source)
2015-10-12 04:58:43 -0500 received badge  Notable Question (source)
2014-06-13 06:00:04 -0500 received badge  Popular Question (source)
2013-02-09 14:16:59 -0500 answered a question CascadeClassifier with haarcascade apparently not working

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!!!");
2013-02-08 10:42:01 -0500 commented question CascadeClassifier with haarcascade apparently not working

@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

2013-02-08 10:10:46 -0500 asked a question Haarcascade gives me several Rects, and most are wrong. What to do??

Hi, And thank you for your time.

I am successfully implementing face features recognition using Haarcascade in Java wit OpenCV for Android.

No problem with face and eyes,

but for the nose feature the detectMultiScale method gives me back several Rects (from 5 to 10, and most of them are wrong!

Here is the cascade I am using: link text

Here is the code

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

            byte[] buffer = new byte[4096];
            int bytesRead;
            Log.e("","Non c'è problema PRIMKA della lettura del file");
            while ((bytesRead = is2.read(buffer)) != -1) {
                os2.write(buffer, 0, bytesRead);
            }
            is2.close();
            os2.close();
            Log.e("","Non c'è problema con la lettura del file");
            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,1,2,new Size(18,15),new Size(face.width()/4,face.height()/4));
            outrec=rec.toArray();
            temp= new RectF ((float) outrec[0].x, (float) outrec[0].y, (float)(outrec[0].x+ outrec[0].width), (float)(outrec[0].y+ outrec[0].height) );
            nose=temp;  
            Log.e("","Contiene rettangoli n =" + Integer.toString(outrec.length));

Thanks for any help!!!

2013-02-08 00:07:39 -0500 commented answer CascadeClassifier with haarcascade apparently not working

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

2013-02-07 14:53:31 -0500 received badge  Editor (source)
2013-02-07 14:14:01 -0500 asked a question CascadeClassifier with haarcascade apparently not working

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 !!!

2013-02-07 12:34:15 -0500 commented answer Android: how to use detectMultiScale()

@Alexander Smorkalov thanks alot!

2013-02-07 12:32:42 -0500 commented answer Android: Mat converting to CV_8U

@Mathieu Barnachon Thanks a lot!!!

2013-02-07 12:31:45 -0500 received badge  Scholar (source)
2013-02-07 10:49:05 -0500 asked a question Android: Mat converting to CV_8U

Hi and thanks for your help!

I am trying to use CascadeClassifier.detectMultiScale()

I understand that it takes as an input a Mat of type CV_8U.

Therefore, after using bitmapToMat()

I try to convert the Mat to CV_8U. with

mat.convertTo(mat2, CV_8U);

but apparently Eclipse does not take as an argument CV_8U

image description

Thank you for your help!!

2013-02-07 08:06:28 -0500 received badge  Supporter (source)
2013-02-06 03:17:04 -0500 asked a question Android: how to use detectMultiScale()

Hi and thanks for your help.

Sorry for the poor question.

I want to use detectMultiScale() to detect face features of picture of a face.

I understand that I have to use the detectMultiScale() method of CascadeClassifier class.

I do not understand how the method works:

 public void detectMultiScale(Mat image,MatOfRect objects)

Takes Mat image as an input I understand that.

Now how do I access the returned result????

I do not understand because I see "MatOfRect objects" as an input, not an output of the method.

Sorry again for the possibly silly question and thanks for your help!