Ask Your Question
0

Wrong Shapes for Given Matrices in OpenCv 3.1 - Java

asked 2017-05-19 06:20:13 -0600

Syed Hasnain gravatar image

I have trained the FaceRecognizer all the images are of same size and passed the grayscale image to the predictor. But i face this error

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (1,150544), size(W) = (37636,5).) in subspaceProject, file /home/**/opencv/modules/core/src/lda.cpp, line 182

Here is the code

if (videoDevice.isOpened()) {
            while (true) {      
                Mat frameCapture = new Mat();
                videoDevice.read(frameCapture);
                MatOfRect faces = new MatOfRect();
                cascadeFaceClassifier.detectMultiScale(frameCapture, faces, 2.0, 5, 0,new Size(),new Size());


                for (Rect rect : faces.toArray()) {
                    Mat resizedCapture = new Mat(frameCapture,rect);
                    Imgproc.cvtColor(resizedCapture, resizedCapture, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
                    Size imageSize = new Size(194,194);
                    Imgproc.resize(resizedCapture,resizedCapture , imageSize);

                    System.out.println(images.get(0).rows());
                    System.out.println(resizedCapture.rows());
                    System.out.println(images.get(0).cols());
                    System.out.println(resizedCapture.cols());
                    try{
                        x = faceRecognizer.predict_label(resizedCapture);
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                    x++;
                    Imgproc.putText(frameCapture, "Face"+x, new Point(rect.x,rect.y-5), 1, 2, new Scalar(0,0,255));
                    Imgproc.rectangle(frameCapture, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),new Scalar(0, 100, 0),3);
                }



                PushImage(ConvertMat2Image(frameCapture));
                //System.out.println(String.format("FACES = %s EYES =  %s detected.", faces.toArray().length,eyes.toArray().length,nose.toArray().length));
            }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-05-19 06:48:42 -0600

berak gravatar image

updated 2017-05-19 06:53:01 -0600

i've seen your SO question, they asked you to cut down your code, but here, now, the important part is missing:

you have to decide on a fixed image size (say, 100x100) for both training & testing, and resize all images to that, before further processing .

also, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE is the wrong constant for cvtColor(). this should be : Imgproc.COLOR_BGR2GRAY

(currently, youre converting to bgra, not to grayscale, so 4x more pixels, than expected.)

edit flag offensive delete link more

Comments

Thanks you so much this really worked. Now i am facing another problem. When i use the function predict_label it shows me a huge number how could i get the exact label which i stored during train.

Syed Hasnain gravatar imageSyed Hasnain ( 2017-05-19 13:30:53 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-05-19 06:19:25 -0600

Seen: 90 times

Last updated: May 19