使用KinectV2和OpenCV进行人脸检测

时间:2016-03-21 07:37:23

标签: c++ opencv kinect

最近我一直在尝试学习OpenCV并尝试使用Haar分类器检测面部。

当我从默认网络摄像头获取视频流时,我成功检测到了脸部,但是当我使用Kinect代替默认网络摄像头时,它仍会检测到脸部,但帧速率会大幅下降。

我写的代码是

int main() {
    string haar_face = "F:\haarcascade_frontalface_default.xml";
    CascadeClassifier haar_cascade;
    haar_cascade.load(haar_face);
    if (haar_cascade.empty()) {
        return -1;
    }
    vector<Rect_<int>> faces;
    bool optionKinect = false;
    cout << "Choose Option\n1.) Kinect \n>1.) WebCam\n";
    int choice;
    cin >> choice;
    if (choice == 1) {
        optionKinect = true;
    }   
    if (optionKinect) {
    CKinectStreamsMat* kinectStream = new CKinectStreamsMat();
    kinectStream->initSensor();
        while (true) {
            Mat original, gray;
            Mat face;
            Rect face_i;
            //cap >> original;
            original = kinectStream->getColorFrame();
            if (original.data) {
                cvtColor(original, gray, CV_BGR2GRAY);
                haar_cascade.detectMultiScale(gray, faces);
                int size = faces.size();
                for (size_t i = 0; i < size; i++) {
                    face_i = faces[i];
                    face = gray(face_i);
                    rectangle(original, face_i, CV_RGB(0, 255, 0), 1);
                }
                imshow("original", original);
                if(waitKey(20) == 27){
                    break;
                }
            }
        }
    }
    else {
        VideoCapture cap(0);
        while (true) {
            Mat original, gray;
            Mat face;
            Rect face_i;
            cap >> original;
            //original = kinectStream->getColorFrame();
            if (original.data) {
                cvtColor(original, gray, CV_BGR2GRAY);
                haar_cascade.detectMultiScale(gray, faces);
                int size = faces.size();
                for (size_t i = 0; i < size; i++) {
                    face_i = faces[i];
                    face = gray(face_i);
                    rectangle(original, face_i, CV_RGB(0, 255, 0), 1);
                }
                imshow("original", original);
                if(waitKey(20) == 27){
                    break;
                }
            }
        }

    }
}

这就是我从Kinect获取Color框架的方法。

cv::Mat CKinectStreamsMat::getColorFrame()
{
    HRESULT hr = E_FAIL;
    IColorFrame* frame = NULL;
    IFrameDescription* frameDesc;
    cv::Mat colorImage;
    hr = _color_reader->AcquireLatestFrame(&frame);
    if (SUCCEEDED(hr)) {
        hr = frame->get_FrameDescription(&frameDesc);
        if (SUCCEEDED(hr)) {
            int frameWidth = 0, frameHeight = 0;
            hr = frameDesc->get_Width(&frameWidth);
            if (SUCCEEDED(hr)) {
                hr = frameDesc->get_Height(&frameHeight);
            }
            if (SUCCEEDED(hr)) {
                const int imgSize = frameWidth*frameHeight * 4 * sizeof(unsigned char); //4 Channels(BGRA)
                colorImage = cv::Mat(1080,1920,CV_8UC4);
                hr = frame->CopyConvertedFrameDataToArray(imgSize, reinterpret_cast<BYTE*>(colorImage.data), ColorImageFormat_Bgra);
            }
        }
        SafeRelease(frameDesc);
        SafeRelease(frame);
    }
    return colorImage;
}

我认为低性能的原因可能是WebCam和Kinect提供的帧分辨率的差异,所以我也尝试将Kinect提供的帧缩小到甚至小于帧大小的大小。摄像头。但表现仍然很低。

因为这只是我能想到的,现在我已经没有想法了,所以有人可以告诉我们这种低性能的原因是什么?

0 个答案:

没有答案