Reputation: 3
I got the following error when I ran a c++ function in Matlab.
WARNING: Couldn't read movie file example.avi
Unexpected Standard exception from MEX file.
What()
is:/tmp/A3p1_2964_800/batserve/A3p1/maci64/OpenCV/modules/imgproc/src/color.cpp:3256:
error: (-215) scn == 3 || scn == 4 in function cvtColor
..
Error in rundemo (line 2)
r = facedetection(inputVideo);
Below is the code for facedetection.cpp
#include <iostream>
#include <vector>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "mex.h"
using namespace std;
using namespace cv;
vector<Rect> detect(Mat img, CascadeClassifier face_cascade){
vector<Rect> faces;
Mat img_gray;
cvtColor(img, img_gray, COLOR_BGR2GRAY);
face_cascade.detectMultiScale(img_gray, faces, 1.1, 2);
return faces;
}
void mexFunction(int nlhs, mxArray *plhs[ ],int nrhs, const mxArray *prhs[ ]){
VideoCapture inputVideo(mxArrayToString(prhs[0]));
Mat img;
inputVideo >> img;
string face_cascade_name = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
face_cascade.load(face_cascade_name);
vector<Rect> rects = detect(img,face_cascade);
int numFaces = rects.size();
plhs[0] = (mxArray *) mxCreateNumericMatrix(numFaces,4,mxDOUBLE_CLASS,mxREAL);
double* outPtr = mxGetPr(plhs[0]);
for(int i = 0; i < numFaces; i++){
Rect rect = rects[i];
outPtr[i+0*numFaces] = rect.x;
outPtr[i+1*numFaces] = rect.y;
outPtr[i+2*numFaces] = rect.width;
outPtr[i+3*numFaces] = rect.height;
}
}
I guess there is something wrong in the path I assigned to the face_cascade_name. This code rans on a Windows computer with a different path, and then I changed it to the one shown because I use a Mac. This is the path to haarcascade_frontalface_alt.xml on my computer. Thank you for helping!
Upvotes: 0
Views: 706
Reputation: 10211
First, check that your video is being read in properly. You said the code works on windows. Make sure that your path to the video is correct.
In your mex function, add
Mat img;
inputVideo >> img;
// Add the following lines to check if img is valid
if (img.data == NULL)
{
printf("Video not read in properly\n");
exit(1);
}
Next, check the number of channels for img. If you run cvtColor(img, COLOR_BGR2GRAY), you need the number of channels to be 3.
printf("Number of channels for img: %d\n", img.channels());
If the number of channels is equal to 1, then your img is already single channel, which is why cvtColor is giving an error. So no color conversion is necessary in this case and you can just comment out the line for cvtColor and the error should be gone.
As a side note, to debug this, you might want to display a couple of frames of the video, i.e., display img for a few frames just to check that they look right.
Upvotes: 1