Ask Your Question

Wrong Shapes for Given Matrices in OpenCv 3.1 - Java

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

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();
                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);

                        x = faceRecognizer.predict_label(resizedCapture);
                    }catch(Exception e){
                    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);

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

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

berak gravatar image

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

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


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 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 36 times

Last updated: May 19