2014-01-19 1 views
0

Matlab에서 C++ 함수를 실행할 때 다음 오류가 발생했습니다.Matlab C++에서 OpenCV cvtColor 오류

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

다음은 내가 face_cascade_name에 할당 된 경로에 문제가있는 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; 
    } 
} 

것 같아요 코드입니다. 이 코드는 Windows 컴퓨터에서 다른 경로로 옮긴 다음 Mac을 사용하기 때문에 표시된 경로로 변경했습니다. 내 컴퓨터의 haarcascade_frontalface_alt.xml 경로입니다. 도와 주셔서 감사합니다!

+0

'facedetection.m'의 내용을 표시하십시오. – herohuyongtao

+0

'facedetection.m'이 없습니다. 그것은'facedetection.cpp'입니다. 감사! – heihei

답변

0

먼저 비디오가 제대로 읽혀지고 있는지 확인하십시오. 코드가 창문에서 작동한다고 했잖아요. 비디오의 경로가 올바른지 확인하십시오. 당신의 MEX 기능에서

,

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

다음을 추가 IMG에 대한 채널 수를 확인한다. 당신이 cvtColor (IMG, COLOR_BGR2GRAY)를 실행하는 경우,이 채널의 수는 1 같은 경우 3.

printf("Number of channels for img: %d\n", img.channels()); 

가 될 채널의 번호가 필요합니다, 당신의 IMG는 왜 이미 단일 채널입니다 cvtColor에서 오류가 발생했습니다. 따라서이 경우 색상 변환이 필요하지 않으므로 cvtColor에 대한 행을 주석 처리 할 수 ​​있으며 오류는 없어야합니다.

이 문제를 디버깅하려면 몇 개의 프레임에 대해 img를 표시하는 것만으로 동영상이 제대로 보이는지 확인할 수 있습니다.

+0

감사합니다! 비디오가 읽히지 않습니다. 또한 isOpened()를 확인하고 비디오가 열리지 않습니다. 그러나 비디오 경로가 맞다고 생각합니다. 현재 디렉토리에 있으므로 비디오 파일의 이름을 직접 제공합니다. 내가 놓친 게 있니? – heihei

+0

신경 쓰지 마세요. 절대 경로로 전환하면 작동합니다! – heihei

+0

이 경우 디버그하는 방법에 대한 자세한 설명을 가져 주셔서 감사합니다. 나는 새롭고, 당신의 제안은 정말 도움이됩니다! – heihei